DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Notes about Emacs


Keyboard Setup

On Cherry keyboard, I swap with , and with key on both left and right sides: adding setxkbmap -option caps:swapescape -option ctrl:swap_lwin_lctl -option ctrl:swap_rwin_rctl into ~/.xinitrc file.

Basics

  • install Emacs 24: sudo apt-get install emacs24 (on Mint) sudo apt install emacs (on Ubuntu 16.4) choco install emacs (on Windows)
  • start emacs GUI: emacs; start in terminal: emacs -nw ("NoWindow");
  • quit Emacs: C-x C-c. In Evil :q or :x;
  • ESC is the same with M-. For example, the following actions has the same effect: action A: pressing Alt and x simutaneously; action B: press ESC, release it, then press x
  • show line number: M-x linum-mode
  • run lisp command in emacs: in scratch buffer, input expression, put the cursor at the end of the it, and press C-x C-e;
  • repeat last command:
  • word completion: M-/, if the word completed is not your hoped, press again. (see "3.4. Word Abbreviations" in "Learning GNU Emacs" for more)
  • scroll window: Current window: PgUp/PgDown; Other window: C-M-v/V

Help & manual

  • query shortcut of a command: C-h w, for example: C-h w where-is; query command of a shortcut: C-h k, for example: C-h k C-h k;
  • list all available command of current buffer: C-h b;
  • list all Emacs key shortcuts: M-x describe-bindings
  • read Emacs manual in Emacs: M-x info use C-s to search string, keep pressing C-s to cycle through the results. use q to quit.
  • read man page: M-x man command-name
  • cancel command in mini-buffer: C-g

File & buffer

  • open an existing file or create a new file buffer: C-x C-f, edit remote files: C-x C-f /user@host:file_path
  • save to file: C-x C-s;
  • list buffers: C-x C-b;
  • switch to another (opening) buffer: C-x b;
  • go to previous/next buffer: C-x followed by Left/Right arrow key;
  • kill current buffer: C-x k;

Panes

  • C-x 1: unsplit window
  • C-x 2: split window horizontally
  • C-x 3: split window vertically
  • C-x o: switch to other pane in split window

Editing

  • undo: C-/
  • bookmarks: Set bookmarks: C-x r m; List bookmarks: C-x r l you can edit bookmarks in this window, use q to quit; jump to bookmarks: C-x r b; rename or delete bookmarks: M-x bookmark-...

Navigation

  • M-<: move to the beginning of a buffer
  • M->: move to the end of a buffer
  • C-a: move to the beginning of line
  • C-e: move to the end of line
  • C-f: forward character
  • C-b: backward character
  • C-p: down a line
  • C-n: up a line
  • M-f: forward word
  • M-b: backward word

Visual mode

  • Shift with any of the navigation key: select and move
  • Ctrl-@ (or M-x set-mark-command): set a mark for selection Note: C-space not works on Windows

Cut & paste

  • C-w: cut
  • C-y: paste
  • C-d: delete character ahead
  • M-d: delete word ahead
  • backspace: delete character behind
  • M-backspace: delete word behind
  • C-k: cut from cursor to end of line

Search & replace

  • search: C-s (forward), C-r (backward)
  • find next: C-s or C-r
  • search and replace M-%
  • recursive edit in query-replace: enter recursive mode: C-r, quit recursive mode: C-M-c
  • find files in a directory: M-x find-name-dired, or locate;
  • search text in files under a directory: M-x grep-find;

Text search in current folder

For example when I want to search the definition of wiki in all .hs files in current folder (~/Documents/gitit):

M-x rgrep <RET> ^\s*wiki\s <RET> *.hs <RET> ~/Documents/gitit <RET>

When the current buffer is associated with a .hs file in folder ~/Documents/gitit, and the cursor on the word wiki, rgrep command will provide default value of file type and target folder. You can press key directly:

M-x rgrep <RET> ^\s*wiki\s <RET><RET><RET>

Paredit mode

Navigation

  • C-M-b: move backward a sexp on the same level
  • C-M-f: move forward a sexp on the same level
  • C-M-u: move backward up a level (the key u means up)
  • C-M-n: move forward up a level
  • C-M-p: move backward down a level
  • C-M-d: move forward down a level (the key d means down)

Edit

  • C-M-k: kill sexp under cursor
  • M-S (Alt-Shift-s, or ESC, Shift-s): split sexp at the cursor
  • M-J (Alt-Shift-j): join sexp A and B, the cursor must be put between A and B
  • M-": doublequote the sexp
  • M-(: wrap the sexp with parenthesis
  • M-?: convolute sexp (this is magical~)

Clojure mode

  • M-x cider-jack-in: start repl in a new window;
  • C-c C-k: compile current file;
  • C-x C-e: cider-eval-last-expression, evaluate expression immediately preceding the cursor in the REPL;
  • C-u C-x C-e: prints the result of the evaluation after cursor;
  • C-c M-n: set the namespace of the REPL with the file's namespace;


Published

Jul 21, 2013

Last Updated

Dec 31, 2019

Category

Tech

Tags

  • emacs 22

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor