Skip to content

Instantly share code, notes, and snippets.

@gwangjinkim
Last active March 29, 2023 19:18
Show Gist options
  • Save gwangjinkim/2e73001474ecbb55b1b2758d80abd81a to your computer and use it in GitHub Desktop.
Save gwangjinkim/2e73001474ecbb55b1b2758d80abd81a to your computer and use it in GitHub Desktop.
Minimal set of commands for (Common) Lisp Developers in Emacs

My minimal set of useful commands for editing Lisp code in Emacs:

Start Emacs

what do
start emacs session type emacs in your terminal
open existing file C-x C-f <write path to file> RET (file)
open/create new "file" (buffer) C-x C-f <write path to new file> RET (file)
(open new window/Tab)

Multiwindow/Screen View - important for Programmers (View Control)

what do
divide screen horizontally C-x 2
divide screen vertically C-x 3
jump between open screens C-x o (other)
open additional file (buffer) C-x C-f (see above!) (file)
If the new file opens, apparently
the former file disappears ...
don't panic! - relax!:
everything you don't close in emacs
stays open - just it is invisible
at the moment.

Most editors use Tabs which stay visible and which you can click to switch between open files. but emacs has buffers and switching between them you can only through key combinations and commands. Something one has to get used to ... Thus learn:

How to manage Windows/Browsers/Tabs in Emacs (Navigation)

In emacs, you don't have Tabs but buffers: Everytime you open a new file, you create actually a new 'buffer' so learn to manage buffers and to jump between the files/tabs/buffers:

what do
switch to previously opened buffer C-x C-b ;; choose there by mouse
;; or by emacs navigaion
;; e.g. C-p and C-n
;; press RET
switch to recently opened buffer C-x b RET (go to buffer)
switch to buffer by name C-x b <buffer name> RET
close/kill current buffer C-x k (kill)
(but leave window open)
close/kill current window C-x 0 (kill current/here/now - zero)
(but leave all buffers open in the background)
kill all other windows/screens C-x 1 (the complement of zero)
(except the window where cursor is now, however
all buffers will stay open!
buffers are really closed only after the
kill buffer command!) (which is C-x k)
maximize/minimize windows ... (see "Window settings" section below!)
change the area they cover on screen ..

Moving (Browsing)

what do
move in text arrows keys
or the C-... combinations below
(quicker/more convenient!)
scroll page up C-v (view) or PageUp
page down M-v or PageDown
line up C-p (prev line) or arrow key up
line down C-n (next line) or arrow key down

Search for a Word in Emacs (Browsing)

what do
search word forward C-s
search word backward C-r
regex search forward C-M-s (search with -M-)
regex search backward C-M-r
in both searches, once invoked:
cycle through searches C-s and C-r (repeatadly)
search strings are saved in a "kill ring"
cycle through previous search strings (in the "kill ring") M-p and M-n (prev next search)
quit search modus C-g (go away)

How to cancel/abort/undo (Editing Commands)?

You started to press some key combination, but inmidst of pressing keys, you realize "oh, this was wrong!" what to do?

what do
cancel the command! C-g (go away)

Or a key combination exectued something you didn't intended. What to do? Take a deep breath. Don't press in panik further wrong key combinations. Undo the last execution(s).

what do
How to undo? C-x u (undo!)
multiple undo C-x u C-x u etc. ...
(press undo as many times you need - the limit is 60 or so)

Oh, pressed too many undos, how to redo?

what do
change undo-ring direction C-f
and then "undo" (redo) backwards! C-x u C-x u ...

But be careful! Don't rely too much on this last resort. It becomes quickly confusing, turns around and bits you. Because after undoing the undo of the undo, you are lost and won't find the way back. The capacity of our brains is simply too limited.

Moving the Cursor in Emacs (Editing Commands)

Perhaps the most confusing commands and most difficult to learn at the beginning!

what do
back (by character) C-b (back) or arrow left
forward (character) C-f (forward) or arrow right
back (word) M-b or M-arrow left
forward (word) M-f or M-arrow right
back (line) = up (line) C-p (prev line) or arrow up
forward (line) = down (line) C-n (next line) or arrow down
back (page) = up (page) C-v (view) or PageUp
forward (page) = down (page) M-v or PageDown
start (line) C-a
end (line) C-e
start (sentence) M-a
end (sentence) M-e

Mark Words/Regions and copy or delete (kill) them (Editing Commands)

Copying in emacs - how usually taught:

what do
1. set mark C-SPC
2. then move the cursor (see moving cursor above!)
3. then tell emacs what to do with
the marked region/text:
"copy" M-w
"cut out" C-w
"delete" DEL
"insert" previous copy/cut chunks C-y (yank)
or just type what to insert

But here is a more convenient way which I discovered (Using Shift in combination with the movement commands from the Moving the Cursor in Emacs (Editing Commands) section!):

what do
1. move cursor in combination with -S- (Shift)
(move and mark at once!)
2.then tell emacs what to do with
the marked region/text:
"copy" M-w
"cut out" C-w
"delete" DEL
"insert" previous copy/cut chunks C-y (yank) ;; see next section!
or just type what to insert
delete until end of line C-k (kill line)
delete whole line but don't remember it (without killring entry) C-S-DEL (backspace)
delete whole line and remember it(with killring entry) C-S-a C-k
(this goes to start and then
deletes until end of line by remembering it)

Paste (yank) in Emacs (Editing Commands)

what do
paste sth already copied/cut C-y
get sth previously copy/cutted (cycle through the killring) C-y M-y
(or un-emacs-ish) F10 (Menue)
> (C-f or arrow right) Edit
> (C-n or arrow down until)
"Paste from Kill Menu"
(kill ring contains ~60
recently cut/copied text pieces)

Move/copy/cut/paste procedure is the most difficult thing to get used to in emacs!! So just leave this site as a cheat sheet open and use it whenever you need to remember!

Search and replace in Emacs (Editing Commands)

what do
invoke simple 'search and replace' M-% (which is: M-S-5) and then:
<type search string> RET and then:
<type replace string> RET
cycle through occurrences by:
replacing SPC
skipping DEL
replace and stop .
replace all without asking !
return to previously replaced text ^
what do
simple search & replace M-x replace-string RET and then:
(without query for each match case) <search string> RET and then:
<replace string> RET
regex search and replace M-x query-replace-regexp RET and then:
<type regexpr> RET and then:
<type replace str> RET
cycle through occurrences by:
replacing SPC
skipping DEL
replace and stop .
replace all without asking !
return to previously replaced text ^
what do
regex search and replace M-x replace-regexp RET and then:
(without query for each match case) <type search regexpr> RET and then:
remarks to replace strings: <type replace string> RET
if different replace string
for each case, use: \? as <replace string>
emacs is written in elisp: replace strings for search replace
can be lisp expressions!
(which will be evaluated!)
Examples:
M-x replace-regexp RET and then:
^.\{0,72\}$ RET and then:
\,(format "%-72sABC%05d" \& \;;) RET
;; lisp format expression!
useful references in replace string
\& means entire match
\1, \2, ... regex references to matches
(groups)
M-x replace-regexp RET and then:
\(x\)|y RET and then:
\,(if \1 "y" "x") RET
;; lisp if-form

Undo and Redo (Editing Commands)

what do
by all this editing, don't forget:
cancel command inmidst key input C-g (get out!!)
undo / redo C-x u (undo) ... / C-f C-x u ...
(a little bit unintuitive)

Cave! C-u (without x) makes sentence Uppercase! So if you -- while C-x u-ing -- get all of a sudden an uppercased sentence, you know that something went awry (you pressed C-u and not C-x u) this is bad. Either you can C-x u (undo) back or if you introduced after undoing additional unintended changes, you can't C-x u (undo) or C-f C-x u (redo) any more back to your very original point ... (You can get easily trapped in an undesired killring branch ...) thus avoid this mistake! ...

what do
prevent this C-u trap by: adding to your ~/.emacs.d/init.el file:
(put 'upcase-region 'disabled t) !

Save and/or quit Emacs

what do
save/write the buffer to the file C-x C-s (save)
close all windows/ leave emacs C-x C-c (generally close emacs)
(it asks if some buffer not saved)

Window settings (View Control)

what do
window width wider C-x C-} (actually C-x C-S-])
narrower C-x C-{
taller C-x C-^
shrink C-x C--
(if buffer smaller than window)
make all Tabs in the window the same size C-x + (M-x balance-windows)
kill-buffer-and-window (the current one) C-x 4 0

Text font size (View Control)

what do
increase C-x C-+
decrease C-x C--

Shell in Emacs (Utility)

what do
start shell process M-x shell RET
stop shell process $ exit
and then kill buffer by C-x k RET
in the shell previous/after command C-arrowUp/Down ;; or M-p/M-n
go to start of the line and C-c C-a

Common-Lisp-specific Commands for Coding

Any file ending with ".lisp" will activate the lisp-mode of emacs. But to write&run&try Common Lisp code in emacs interactively, you have to activate slime-mode:

what do
start slime-mode M-x slime RET ;; opens an extra window for slime below
;; starting a server which sends its result to the slime window
stop slime server M-x slime-quit-lisp or
C-x o to slime window C-x k RET (kill buffer)
execute/evaluate s-expression put cursor behind any lisp formula and
send to running slime and evaluate by C-x C-e
(by this, I submit function by function)
(variable by variable...)
send and compile entire file to slime C-c C-k
close all open parentheses C-c C-]

There is still alot to learn (see: https://common-lisp.net/project/slime/doc/slime.pdf)

If you have useful commands which you love and which are not mentioned here: Please comment!

Clojure-specific Commands for Coding

Any file ending with .clj will activate clojure-mode of Emacs. But to write and run and edit clojure code in emacs, you have to activate clojure-nrepl server:

what do
start a clojure-nrepl server session M-x cider-jack-in
to stop the REPL session do later M-x cider-quit
change namespace of repl C-c M-n <namespace name>``RET

While a clojure-nrepl is running, you can use the clojure-specific commands in your file;

How to send code to the nrepl from your coding file:

what do
compile entire file (send it to repl) C-c C-k ;; or: M-x cider-load-buffer
compile pre-cursor s-expression C-c C-e ;; or: M-x cider -eval-last-sexp
Run tests C-c C-t t
or using lein in shell $ lein test
show docstring of function cursor in function name and then: C-c C-d d
;; or in repl: (clojure.repl/doc <functionname>)
;; or in repl after: (use 'clojre.repl)
;; calling: (doc <functionname>)
clear REPL when too cluttered M-x cider-repl-clear-buffer
display previous/later entered expressions C-arrowUp/C-arrowDown M-p/M-n
jump to function source code M-.
pop stack and return to previous M-,
list all definitions in file M-x imenu ; and jump to one
in repl, close all remaining open parentheses C-RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment