Use sed to add a newline to the end of a file (this will perform an in-place replacement and create a backup file, MYFILE.bak):
$ sed -i.bak '$G' MYFILE
Some programs, such as Emacs, will omit the trailing newline from a file unless configured to add one. Emacs can be configured to add a newline automatically.
backupcommandsconfigurationeditorsemacsin-placeline-endingsnewlinesedshell
Emacs can be configured to add a trailing newline by updating ~/.emacs as follows:
(setq require-final-newline t)
dot-emacseditorselispemacsnewline
To set a bookmark for the visited file at point:
C-x r m <RET>
To set a named bookmark at point:
C-x r m BOOKMARK <RET>
To open and move to a bookmarked location:
C-x r b BOOKMARK <RET>
To list all bookmarks:
C-x r l
bookmarkseditorsemacskeystrokes
To change the color of a particular face (e.g. font-lock-comment-face), use this elisp (editing where necessary):
(custom-set-faces '(font-lock-comment-face ((t (:foreground "lightblue" (background light)))) ) )
lightblue and other color names are defined in rgb.txt, included with your emacs and X installations.
colorsconfigurationeditorselispemacsx11
You can use M-/ to complete strings that emacs has seen in documents you are editing. It's useful for completing (or cycling through) long variable and function names to prevent misspellings.
autocompleteeditorsemacskeystrokes
Use C-x l to count lines in a page (see the Emacs manual on pages). Without page boundaries, this will count lines in the entire buffer.
editorsemacskeystrokes
To set your own global keybinding to a function, use global-set-key in your ~/.emacs as follows:
(global-set-key "C-cl" 'enlarge-window-horizontally)
configurationdot-emacseditorselispemacskeystrokes
Use C-x i to insert a file into the current buffer.
editorsemacskeystrokes
If you want to change the fill column, do the following:
C-u 60 C-x f
(where '60' is the fill column setting you want to use.)
configurationeditorsemacskeystrokes
Use M-q to "fill" a contiguous body of text according to the mode's rules. This will wrap lines at the standard fill-column.
editorsemacskeystrokeswrapping
On some systems (and for some terminal types), Emacs' use of the backspace key can be confusing. The backspace key may behave like the Delete key. You can fix this either by using this elisp in your ~/.emacs as follows:
(keyboard-translate ?\C-h ?\C-?)
This shell command may work if the elisp does not:
stty erase '^?'
backspacecrapdeletedot-emacseditorsemacsgotchakeystrokesterminal
Use this in your ~/.emacs to bind a key to goto-line:
(global-set-key "C-cl" 'goto-line)
configurationdot-emacseditorselispemacskeystrokes
If you have a particular need to insert ^M, ^C, backspace, delete, or anything else in emacs, you can start it off with C-q.
emacsquoteunicode
To create a macro:
C-x (
Once in macro recording mode, enter a series of commands. When finished:
C-x )
To name the most recently recorded macro:
M-x name-last-kbd-macro
Enter a name for the macro. The name will now be accessible as M-x <name>.
To generate macro elisp suitable for reload, run M-x insert-kbd-macro and enter the name of a defined macro. Paste the resulting code into ~/.emacs.
configurationdot-emacseditorsemacskeystrokesmacros
To get emacs to stop inserting tab characters and insert spaces instead, use this in your ~/.emacs file:
(setq-default indent-tabs-mode nil)
configurationdot-emacseditorsemacsindentationspacestabs
Emacs features a minor mode, mouse-avoidance-mode, which causes the mouse to move away from the Emacs point when the point gets too close. This can be really helpful if you're used to moving your mouse away from the cursor to avoid typing "underneath" the mouse. In your custom-set-variables section in ~/.emacs, add the appropriate elisp:
(custom-set-variables . . '(mouse-avoidance-mode (quote animate) nil (avoid)) . .
The animation parameters of mouse-avoidance-mode can be customized with M-x customize.
configurationeditorselispemacsmouseneat
Narrowing is the act of making only a certain region of text visible. To narrow your view to a given textual region, set the mark (C-Spc) at the beginning of the desired area and move the point to the end of the area.
To narrow the text to the region you've marked:
C-x n n
To show the entire buffer:
C-x n w
editorsemacskeystrokesnarrowing
By default, the emacs buffer list appears in a vertical split when you type C-x C-b (or run M-x list-buffers). I use a widescreen monitor which means a horizontal split would be much nicer. Since I couldn't find a Customize setting to change this behavior, I wrote this keyboard macro in my ~/.emacs which does the trick:
(fset 'my-list-buffers [?\C-x ?1 ?\C-x ?3 ?\M-x ?l ?i ?s ?t ?- ?b ?u ?f ?f ?e ?r ?s return ?\C-x ?o]) (global-set-key "\C-x\C-b" 'my-list-buffers)
bufferseditingemacsmacros
It will kill the buffer you have open and visit a different file instead (for mistakes, etc), use 'C-x C-v' or 'M-x find-alternate-file'.
editorsemacskeystrokes
If you use middle-clicking to paste into Emacs windows but don't like how the paste occurs at the click location and would rather paste at point, use this in your ~/.emacs:
(setq mouse-yank-at-point t)
Thanks to Kevin Turner for pointing this out.
configurationdot-emacseditorselispemacsmousepointyank
Use M-| to pipe the selected region of text to a specified shell command.
editorsemacskeystrokespiperegion
Zsh can run a command and let you do things with a temporary file with the resulting output:
$ emacs -nw =(ps aux)
This will create a temporary file with the output of ps aux and let you edit it in Emacs. Or:
$ diff =(ls) =(ls -F)
Will run diff on the output of the two commands.
diffemacsprocesssubstitutionzsh
Run M-x center-region to center the selected region of text.
editorsemacs
Run M-x indent-region or C-M-\ to indent the selected region of text.
editorsemacs
Use this to remove all characters from the point to the specified character: M-z <CHAR>.
editorsemacskeystrokes
Use C-x C-w to save a buffer under an alternate name.
bufferemacssave
If you wish for a particular file to be handled with a mode that isn't already associated with its extension, you may put a header like this anywhere in the file:
-*- mode: outline; mode: auto-fill -*-
Alternatively, you can update your ~/.emacs to use modes based on file extension or filename. For example, to use html-mode for files ending in .tpl:
(setq auto-mode-alist
(cons '("\\.tpl$" . html-mode) auto-mode-alist))
You can also configure Emacs to use a specific major and minor mode together for a given file extension. This example defines my-mode to load outline-mode (a major mode) and auto-fill-mode (a minor mode) for files ending in .foo:
(defun my-mode ()
(outline-mode)
(auto-fill-mode))
(setq auto-mode-alist
(append '(("\\.foo$" . my-mode))
auto-mode-alist))
configurationdot-emacseditorselispemacs
Use this elisp snippet (in your ~/.emacs) to instruct your Emacs python-mode to use the proper python executable on a system with multiple pythons installed:
(setq py-python-command "/usr/bin/python2.3")
dotemacselispemacsinterpreterinvocationlanguagesprogrammingpython