This page is some advanced emacs tips. “Advanced” here means beyond beginner. Tips here are still commonly needed. If you don't know emacs basics, please see: Emacs Intermediate Tips.
To call a emacs command named “xyz”, press 【Alt+x】 then type “xyz”, Enter ↵.
How to search text?
Press 【Ctrl+s】 (
isearch-forward), then type your text. Emacs will
search as you type. To find the next occurrence, press 【Ctrl+s】
again. To go to previous occurrence, press 【Ctrl+r】. To stop, press Enter ↵ to leave the cursor there. Or type 【Ctrl+g】 to
return to the spot before search was started.
This command is also under the menu 〖Edit ▸ Search〗.
To search for the word that is under cursor, type 【Ctrl+s Ctrl+w】. This can save you some typing.
【Ctrl+s】 twice will search your last searched word.
To search text in a directory, see: Emacs: Searching for Text in Files (grep, find).
How to find/replace?
Press 【Alt+%】 (
query-replace). Then, emacs will prompt you for the find string and replace string. Once emacs found a match, you can press y to replace, n to skip, or ! to do all replacement without asking.
If you made a mistake, you can cancel by pressing 【Ctrl+g】. If you want to revert the find/replace you did, you can undo by 【Ctrl+_】.
For regex replace and detail on issues of letter cases, see: Emacs: Find/Replace Tutorial.
How to find/replace for all files in a dir?
dired-jump【Ctrl+x Ctrl+j】 to jump to the dir of current file. (or, call
dired【Ctrl+x d】, then type dir path)
For detail, see: Interactive Find/Replace String Patterns on Multiple Files.
Once in dired, you can find the command under the menu 〖Operate ▸ Query Replace in Files…〗.
How to insert/delete comment?
set-mark-command【Ctrl+Space】 to mark, then move cursor.)
comment-dwim【Alt+;】 to make the region into a comment or uncomment.
How to add a prefix to every line? (such as # or //)
set-mark-command) to mark cursor position.
string-rectangle【Ctrl+x r t】
string-rectangle can be used to insert a vertical column of string across mulitple lines at any column position, not just at the beginning of lines.
For detail, see: Emacs: Manipulate Column Text, string-rectangle, ASCII-Art.
How to delete the first few n chars of every line?
Mark 【Ctrl+Space】 the beginning of first line and move cursor to the last line, and move it to the right n chars. Then type 【Ctrl+x r k】 (
kill-rectangle). This command can be used to delete any rectangular block of text, not just at the beginning of lines.
How to replace unprintable characters such as tab or newline characters in Emacs?
query-replace 【Alt+%】. When you need to insert a tab character, type 【Ctrl+q】 first, then press Tab ↹. Same for inserting a line return.
For detail, see: Emacs: Newline Representations ^M ^J ^L.
How to change file line endings between Windows/Unix/Mac?
set-buffer-file-coding-system, then give a value of “mac”, “dos”, “unix”. For detail, see:
Emacs: Newline Representations ^M ^J ^L.
How to record a sequence of keystrokes?
For detail and examples, see: Emacs Keyboard Macro Examples.
How to move thru camelCaseWords?
You can set emacs so that word moving commands will move cursor into between CamelCaseWords. (word deletion behavior also changes accordingly.)
global-subword-mode. Subword mode is available since Emacs 23.2 (). 〔☛ Emacs 23.2 Features〕
To set it permanently, put this in your emacs init file:
(global-subword-mode 1) ; 1 for on, 0 for off
How to have spell-checker turned on?
flyspell-buffer. This is under the menu 〖Tools ▸ Spell Checking〗.
To have it always on, put the following in your emacs init file:
(defun turn-spell-checking-on () "Turn flyspell-mode on." (flyspell-mode 1) ) (add-hook 'text-mode-hook 'turn-spell-checking-on)
For discussion of problems about spell checking, see Emacs Spell Checker Problems.
How to disable emacs's automatic backup?
Put this code in your emacs init file:
(setq make-backup-files nil) ; stop creating those backup~ files (setq auto-save-default nil) ; stop creating those #auto-save# files
How to stop emacs's backup changing the file's creation date of the original file?
Put this code in your emacs init file:
(setq backup-by-copying t)
Explanation: when emacs does a backup, by default it renames the original file into the backup file name, then create a new file and insert the current data into it. This effectively destroys the creation date of your file. (If a file is created in 2001, and you modified it today, the file's creation date will become today. Note: unixes (including Linux ＆ BSD) do not record file creation date, so this doesn't matter. (ctime is not creation date.) Microsoft Windows and Mac OS X do record file creation date.).
How to set emacs so that all backups are placed into one backup folder? ⁖
How to startup emacs without loading any customization?
To run emacs without loading your personal init file, start emacs like this:
emacs -q. To not load any site-wide startup file, start emacs with
emacs -Q. The site-wide startup file is usually part of your emacs distribution, such as from Carbon emacs, Aquamacs, ErgoEmacs. Starting emacs with “-Q” is like running a bare bone GNU Emacs.
|Don't load your personal init file.|
| same as |