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-bracket
at Emacs: Move Cursor to Bracket ๐xah-extend-selection
at Emacs: Extend Selection ๐backward-sexp
,forward-sexp
, in emacs. ใsee Emacs: How to Edit Lisp Codeใxah-insert-paren
at Emacs: Insert Brackets by Pair ๐xah-elisp-prettify-root-sexp
at Emacs: Xah Elisp Mode ๐ฆxah-delete-backward-char-or-bracket-text
at 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 ] โฎhttps://twitter.com/dias_jorgeโฏ.