EmacsCrashTips

This is general advice for text editing and how they translate to emacs. Mainly taken from Seven habits of effective text editing, effective emacs and emacs playboy.

Learn to touch-type

This is the most basic, but most useful advice that can be given to a beginner. Use your 10 fingers, learn which finger type which key. No peek and hunt. The most common typing methods advice to let the fingers rest on homerow and stretch, one at a time, to reach other keys. Keys far from the homerow should be avoided as much as possible. This translates to :

 Swap the Caps Lock and control keys

This is system specific. See MovingTheCtrlKey for detail.

 C-h is backspace

This shortcut is available in many apps. This can be done by putting in your InitFile (keyboard-translate ?\C-h ?\C-?) . See also the variable normal-erase-is-backspace-mode and the emacs faq. Use F1 for the help.

Awkward shortcuts should be remapped. Which one to remap precisely depends on your keyboard layout. Remap to Key sequences of the form C-c followed by letters, since these are reserved for users. For example, the shortcut C-M-% is executed by pressing simultaneously four keys on my keyboard, each on a different side of the keyboard. Here is how to remap the associated command to C-c %.

     (global-set-key (kbd "C-c %") 'query-replace-regexp)

An alias could have been used instead. Put the above line in your InitFile in order to call the previous command with M-x qrr.

     (defalias 'qrr 'query-replace-regexp)

Along the same lines, avoid using the mouse or the arrow keys. Use shortcuts instead.

Learn the shortcuts

Learn them one at a time. Learn them when you need them. Make a cheatsheet with your favorite ones. Take a few minutes to practice when you learn a new one and add it to the sheet. Ask other emacs users which one they use and how.

There is a certain logic to the emacs keybindings. This is a bit explained in the emacs tutorial. Command to move around are bound to the Control prefix if these moves are character-based. They are bound to the Meta prefix if they deal with syntax (e.g. move forward one word). Control Meta keys are arcane shortcuts, which deal with move associated to regular expressions, parenthesis matching etc. The list of keybindings is obtained by typing

     F1 b

Meta shortcuts are as important as Control shortcuts, and easier to learn after you grab the symmetry of the layout. So, practice them. If backspace (sorry, delete-backward-char) has been remapped to C-h, you may want to bind backward-kill-word to M-h.

But M-hruns the command mark-paragraph, which is also a very useful command. – XueFuqiao

It’s usually faster to kill a misspelled word and retype it instead of going back to the typo and deleting one character at a time.

Customize your own bindings

Don’t overwrite the default shortcuts even if they seem useless. You may find them pretty useful in the future, after some practice. Instead rebind the corresponding commands to C-c letters shortcuts. These are reserved to users, you won’t be overwriting any preexistent shortcuts. You can see a reminder of all commands you already bound to the C-c prefix by typing

   C-c C-h

I suggest also to use M-c as a prefix to bind additional commands. This executes by default the capitalize-word command, which is not too useful, and that can be bound to M-c c anyway. This will allow you to keep the symetry between the Control and Meta key. Note also that Meta-Shift is not used by the default shortcuts.

Depending on your keyboard layout, you may wand to rebind the M-x prefix. Note however that the Meta key is often side by side with the spacebar, and is pressed with the thumb. This is one of the best position for a modifier key.

Finally, many users want C-x/C-c/C-v cut/paste. Get them by putting in your InitFile

  (cua-mode t)

You will have to look for another prefix for your own defined shortcuts. C-i defaults to TAB, you probably don’t need it for that purpose. See Xahlee website for a detailled study of the most common keyboard layouts and shortcuts.

Use the search and replace commands

Incremental search (C-s, C-r) is often the fastest way to move around your file. Try also C-M-s and C-M-k. These are pretty useful to deal with parenthesis groups.

The replace-regexp is a versatile command that can save you a lot of tedious work. It is not bound by default. C-c r seems to be the right shortcut.

 (global-set-key (kbd "C-c r") 'replace-regexp)

For example, to delete trailing whitespaces at end of lines, you could use M-x delete-trailing-whitespace. Or instead use replace-regexp : C-c c RET SPC+$. Faster. Starting with emacs 22, arbitrary lisp code can be specified to compute the replacement string. This makes this command even more powerful. See ReplaceRegexp and RegularExpression for many examples.

Automate tasks

When you need to repeat a task several times, take a break and try to think how to automate the process. Take a few minutes to see if there is not a new shortcut that may help.

Have a look at the wiki. Emacs have many features you may not be aware of. Common tasks are listed at the top of this page. You will probably have to deal with them at some point. The wiki often provides unique information on these topics. As an example, you may want to look at the Alignment section, which is not documented in the emacs manual.

Keyboard macros need a bit of practice, but they are very handy, once mastered. They are used to record key sequences and play them again as needed. See KeyboardMacros for a tutorial.

Learn a bit of EmacsLisp

EmacsLisp is the language that is used to customize emacs. The emacs InitFile is written in emacs Lisp. There is no need to know a lot of emacs Lisp to be able to tweak the default emacs configuration to your own needs.

EmacsLisp is a functional language, which means that you can make complete programs just by nesting short functions. If you have some practice with an imperative programming language, you may be used to the following syntax

  f(x,y)

where f is the name of the function, and x and y his arguments. The corresponding syntax in Lisp is

  (f x y)

For example, type

 (+ 2 3) C-x C-e

You will get 5 in the minibuffer, which is the expected result. If you know some emacs shortcuts, then you know a bunch of functions that you can use in a straightforward way. To get the function activated by a shortcut, together with its syntax, just type F1 k, followed by the shortcut. Try e.g.

  F1 k M-<

Now, let us look at a short example. The code given below defines a command that remove DOS carriage return (code “\r”) at end of lines, so as to obtain a unix compatible file. Since this effect can be obtained with the use of the replace-regexp command, you may want to read the documentation of this function for some hints.

 (defun dos2unix ()
   (interactive)
   (beginning-of-buffer)
   (while 
       (search-forward "\r") 
     (replace-match "")))

The parentheses () in the first line just indicate that our function does not take any argument. They are mandatory. The interactive function that follows says that our function is in fact a command that can be called by typing

     M-x dos2unix

The next instruction puts the cursor at the beginning of buffer. This is the command called by the M-< shortcut.

The search-forward command moves cursor to the next carriage return. Since it is in a while loop, this is repeated until it fails, that is until the last carriage return has been found. Each match is replaced by the empty string, thus removing the carriage return.

This command is far from perfect, yet it works and should show you that it’s not difficult to write short useful commands in EmacsLisp.

Another example is worked out on SteveYegge’s blog save-excursion. See also his entry on emergency lisp for a short introduction to emacs Lisp.

A final remark: you can see the code of any command just by calling help on that command and cliking at the end of the first line, on the .el file name. You will be presented with the actual code that is executed when the command is called. This is a great way to learn EmacsLisp.