Emacs Tips and Howtos
With Emacs, I feel happy. I love the rich functions of Emacs, such as compiling, quickly jumping to the lines with compilation error and debugging with gdb, and more. I ever wrote small tips posts about Emacs before. But it is a good idea to put them together and keep adding new ones. Here comes this post on Emacs tip and howtos, more or less like a cheat sheet and FAQ. This post is kept being updated with new tips and howtos.
1. What is your Emacs configuration file?
First thing first. I share my Emacs configuration file (‘~/.emacs’) on github. Please find it here: Zhiqiang’s Emacs configuration file.
2. Emacs tutorials
Emacs itself provides a tutorial to start with. To view it, execute this command in Emacs:
Many Emacs tutorials are online. I highly recommend the A Tutorial Introduction to GNU Emacs by Keith Waclena.
3. Several Emacs cheat sheets
Here are several Emacs cheat sheets online:
Emacs Basics: http://doors.stanford.edu/~sr/computing/emacs.html
Emacs Commands List: http://lpn.rnbhq.org/tools/xemacs/emacs_ref.html
GNU Emacs Reference Card: http://refcards.com/docs/gildeas/gnu-emacs/emacs-refcard-a4.pdf
4. Emacs Write to Read-only Files
Emacs has the power of writing to read-only files as Vim (with “w!”). This is how to do it:
We can clear the buffer text’s read-only flag with
5. Emacs Remove ^M From Files
‘^M’ characters are usually generated by a editor in Windows. This post introduces how to remove these ‘^M’ characters in Emacs.
First go to top of buffer, then:
M-x replace-string C-q C-m RET
M-%can be used for
The control-q will allow typing control characters into the input buffer.
6. Generate and regenerate ETAGS in Emacs
Add the following lines to ‘~/.emacs’
;; =================== etags ===================== (defvar tags-cmd "etags -R ./* 2>/dev/null") (defun regen-tags () "Regenerate the tags file for the current working directory" (interactive) (let ((tag-file (concat default-directory "TAGS"))) (shell-command tags-cmd) (visit-tags-table tag-file))) ;; =================== end etags =====================
To regnerate ETAGS in the current directory, simply run these command in Emacs:
7. Auto completion
Auto Complete Mode is the most intelligent auto-completion extension. Auto Complete Mode provides visual interfaces, reduced overhead of completion by using statistic method and extensibility. A Demo is available.
On Fedora Linux, we can install Auto Complete Mode by:
# yum install emacs-auto-complete emacs-auto-complete-el
Auto Complete Mode is easy to use — just type your code!
You can use ‘TAB’ to expand and select. You can also use ‘M-p’ and ‘M-n’ to select previous or next possible choice, and ‘RET’ to complete your selection. This should complete most of the auto completion usage. For more usage, please refer to Auto Complete Mode User Manual.
8. Goto a specific line
Emacs supports jumping to a specific line:
Then enter the line number and RET.
9. Jump to next and previous error
In the compile mode, you can jump to the next/previous error in the compile buffer by:
10. Jump to beginning and end of a function
The functions beginning-of-defun and end-of-defun works with most programming modes in Emacs that jumps to the beginning and end of the function/class definition.
The commands are
This will make editing code much easier.
11. Moving in the Parenthesis Structure
Move over groupings delimited by parentheses (or whatever else serves as delimiters in the language you are working with):
Move forward over a parenthetical group (forward-list).
C-M-pMove backward over a parenthetical group (backward-list).
C-M-uMove up in parenthesis structure (backward-up-list).
C-M-dMove down in parenthesis structure (down-list).
Move between a brackets pair:
C-M-n for forward match and C-M-u for backward match.
12. Operating rectanglesRectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines.
To specify a rectangle for a command to work on, set the mark at one corner and point at the opposite corner. If point and the mark are in the same column, the region-rectangle is empty. If they are in the same line, the region-rectangle is one line high.
C-x r k
Kill the text of the region-rectangle, saving its contents as the “last killed rectangle” (kill-rectangle).
C-x r dDelete the text of the region-rectangle (delete-rectangle).
C-x r yYank the last killed rectangle with its upper left corner at point (yank-rectangle).
C-x r oInsert blank space to fill the space of the region-rectangle (open-rectangle). This pushes the previous contents of the region-rectangle to the right.
C-x r NInsert line numbers along the left edge of the region-rectangle (rectangle-number-lines). This pushes the previous contents of the region-rectangle to the right.
C-x r cClear the region-rectangle by replacing all of its contents with spaces (clear-rectangle).
M-x delete-whitespace-rectangleDelete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
C-x r t stringReplace rectangle contents with string on each line (string-rectangle).
M-x string-insert-rectangle stringInsert string on each line of the rectangle.
13. Spell checking
M-x ispellCheck and correct spelling of all words in the buffer. If the region is active, do it for all words in the region instead.
Enable Flyspell mode, which highlights all misspelled words.
Enable Flyspell mode for comments and strings only.
Skip this word—continue to consider it incorrect, but don’t change it here.
Accept the incorrect word—treat it as correct, but only in this editing session.
14. Undo and redoEmacs has a powerful undo system. It allows you to recover any past state of a buffer.
redo after a undo
Performing some commands after a sequence of undo, all the undos are pushed to the operation stack, and the next undo undoes the last command. Multiple C-_ to redo what have been undone by C-_.
15. Display line numbersDisplay line numbers:
To enable it by default
16. Position the screen
We can position the screen by
Invoking ‘C-l’ multiple times makes Emacs position the current line at the middle, top and bottom of the screen iteratively.
17. Replace in EmacsInteractive find and replace
18. Set Emacs’s font sizeTo set the font size to 12pt, put this line into ‘~/.emacs’:
(set-face-attribute 'default nil :height 120)
The value is in 1/10pt.
Increase/decrease current instance of Emacs’s font size:
19. Hide Emacs’ toolbar / menubarTo hide the toolbar completely, put this line into ‘~/.emacs’:
To hide the menubar:
You can also toggle it by
20. Kill a line without moving the cursor position1. Using 2 ‘C-k’ with ‘C-a’ first can kill the current line. But there are easier methods as follows.
- Use ‘kill-whole-line’
C-S-backspace (kill-whole-line) kills a whole line and its newline, no matter where the point within the line is.
However, many text terminals will prevent you from typing the key sequence C-S-backspace.
- Define your own key bindings
I prefer ‘C-c C-x’ to kill a whole line by putting this line into ‘~/.emacs’:
(global-set-key "\C-c\C-x" 'kill-whole-line)
21. Duplicate a line in EmacsMethod 1. Using the keyboard
First, kill the current line (the line to duplicate)
Then, yank it at the position where it should be duplicated
Method 2. Define a function and bind it to ‘C-c C-k’:
(defun copy-line (arg) "Copy lines (as many as prefix argument) in the kill ring" (interactive "p") (kill-ring-save (line-beginning-position) (line-beginning-position (+ 1 arg))) (message "%d line%s copied" arg (if (= 1 arg) "" "s"))) ;; optional key binding (global-set-key "\C-c\C-k" 'copy-line)Script reference: http://emacswiki.org/emacs/CopyingWholeLines