Emacs: ParEdit, Smartparens, Lispy, and ErgoEmacs, xah-fly-keys
For programing lisp in emacs, there are several packages to help you.
- ParEdit http://www.emacswiki.org/emacs/ParEdit
- smartparens https://github.com/Fuco1/smartparens
- lispy https://github.com/abo-abo/lispy
Here's a method, i think is simpler and can compete with packages similar to paredit in efficiency.
- have 1 key to move cursor backward to any left bracket
- have 1 key to move cursor forward to any right bracket.
- have 1 key to select current lisp expression unit. When on open bracket, it selects the whole. Press again to expand selection to parent branch.
- have 1 key to move to the corresponding open/close bracket.
- have 1 key insert bracket pairs, place cursor in between. If there's a selection, bracket that selection.
- have 1 key to indent current sexp tree up to current root. (all lines) This is critical.
The gist, is to never treat lisp code as lines. In particular, never manually indent/format by line. 〔see Emacs: Why I Don't Use paredit〕
if you want to try, the commands are:
xah-backward-left-bracket,xah-forward-right-bracketat Emacs: Move Cursor to Bracket 📜xah-extend-selectionat Emacs: Extend Selection 📜backward-sexp,forward-sexp, in emacs. 〔see Emacs: How to Edit Lisp Code〕xah-insert-parenat Emacs: Insert Brackets by Pair 📜xah-elisp-prettify-root-sexpat Emacs: Xah Elisp Mode 📦xah-delete-backward-char-or-bracket-textat Emacs: Delete Brackets by Pair 📜
The above are general, is extremely useful to me in any programing mode too, such as those with {} [] ().
You can pull those commands out from above, and set keys for them yourself. 〔see Emacs Keys: Define Key〕
Or, you can just use Xah Fly Keys and Xah Emacs Lisp Mode. All the commands are there, with keys setup.
These commands are also in ergoemacs-mode at http://ergoemacs.github.io/, except “xah-elisp-prettify-root-sexp”.
Bracket Highlight
You should also turn on bracket highlight.
〔see Emacs Init: Highlight Brackets〕
Always Insert Brackets in Pairs
You should ALWAYS insert/ remove brackets in pairs. Make this a habit.
Use xah-insert-paren
〔see Emacs: Insert Brackets by Pair 📜〕
Navigating Brackets
Moving cursor to brackets is one of the most important navigation command. Because, all programing language source code, use brackets (){}[] extensively as a unit.
Use • xah-backward-left-bracket • xah-forward-right-bracket
〔see Emacs: Move Cursor to Bracket 📜〕
Select Lisp Expression
To select a lisp expression, xah-extend-selection once or twice.
Move Lisp Expression
Select it first, then just press 1 key to cut. Then paste it elsewhere.
Delete Lisp Expressions
put cursor on the right of a bracket, then xah-delete-backward-char-or-bracket-text.
Always delete Brackets in Pairs
To delete bracket pairs (not including inner text),
put cursor on the right of a bracket, then
Ctrl+u then xah-delete-backward-char-or-bracket-text.
Remove Empty Lines; Compact Ending Brackets
To remove empty lines:
In xah-elisp-mode, press Tab with cursor before a word.
The command is xah-elisp-prettify-root-sexp.
〔see Emacs: Xah Elisp Mode 📦〕
Or see:
Pretty Format Lisp Code
In xah-elisp-mode, press Tab with cursor before a word.
The command is xah-elisp-prettify-root-sexp.
It will re-indent the extire lisp expression the cursor is in.
〔see Emacs: Xah Elisp Mode 📦〕
If you are not using xah-elisp-mode, then you can select the whole, then Alt+x indent-region 【Ctrl+Alt+\】
Thanks to [ Jorge Dias ]