parsed.org

Tips by tag: emacs

Add a Newline to the End of a File by cygnus on Dec 31, 2005 04:09 PM

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
Add Newline When Saving Files by cygnus on Feb 16, 2005 02:30 PM

Emacs can be configured to add a trailing newline by updating ~/.emacs as follows:

(setq require-final-newline t)
dot-emacseditorselispemacsnewline
Bookmarking by xinu on Jan 12, 2005 10:48 AM

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
Changing Face Colors by cygnus on Mar 06, 2005 07:48 PM

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
Count Page Lines by xinu on Jan 12, 2005 10:54 AM

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
Customization of global shortcut keys by cygnus on Jan 12, 2005 10:30 AM

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
Emacs link by cygnus on Dec 31, 2005 04:04 PM

This is a useful emacs wiki.

editorsemacstips
File Insertion by xinu on Jan 12, 2005 10:56 AM

Use C-x i to insert a file into the current buffer.

editorsemacskeystrokes
Fill Column by xinu on Jan 12, 2005 10:52 AM

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
Filling a Paragraph by cygnus on Jan 12, 2005 10:33 AM

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
Fixing the Backspace Key by cygnus on Nov 14, 2005 11:14 AM

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
Goto-line by cygnus on Jan 12, 2005 10:31 AM

Use this in your ~/.emacs to bind a key to goto-line:

(global-set-key "C-cl" 'goto-line)
configurationdot-emacseditorselispemacskeystrokes
Insert Unicode using Emacs by xinu on Dec 28, 2007 04:41 PM

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
Macros by xinu on Jan 12, 2005 10:50 AM

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
Make Emacs Use Spaces Instead of Tabs by cygnus on Dec 21, 2005 11:01 AM

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
Mouse avoidance mode by cygnus on May 03, 2006 02:59 PM

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 by cygnus on Jan 12, 2005 10:28 AM

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
Open buffer list horizontally by cygnus on Oct 24, 2006 10:17 AM

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
Opening a file into the same buffer by xinu on Jan 12, 2005 10:55 AM

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
Pasting With the Mouse by cygnus on Feb 09, 2006 04:12 PM

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
Pipe a Region to a Shell Command by xinu on Dec 31, 2005 04:05 PM

Use M-| to pipe the selected region of text to a specified shell command.

editorsemacskeystrokespiperegion
Process Substitution by cygnus on Jan 20, 2005 10:16 AM

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
Regional centered alignment by xinu on Jan 12, 2005 10:56 AM

Run M-x center-region to center the selected region of text.

editorsemacs
Regional indentation by cygnus on Jan 12, 2005 10:28 AM

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
'Save As' Functionality by cygnus on Feb 17, 2005 01:30 PM

Use C-x C-w to save a buffer under an alternate name.

bufferemacssave
Setting Modes by xinu on Jan 15, 2005 02:52 PM

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
Window size controls by cygnus on Jan 12, 2005 10:30 AM

Use the commands enlarge-window-horizontally and shrink-window-horizontally to change the size of an emacs window.

editorsemacsuiwindow
RSS