[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Common requests

5.1 How do I set up a `.emacs' file properly?  
5.3 How do I debug a `.emacs' file?  
5.2 How do I get colors and syntax highlighting on a TTY?  
5.4 How do I make Emacs display the current line (or column) number?  
5.5 How can I modify the titlebar to contain the current file name?  
5.6 How do I turn on abbrevs by default just in mode mymode?  
5.7 How do I turn on auto-fill-mode by default?  
5.8 How do I make Emacs use a certain major mode for certain files?  
5.9 How do I search for, delete, or replace unprintable (eight-bit or control) characters?  
5.10 How can I highlight a region of text in Emacs?  
5.11 How do I control Emacs's case-sensitivity when searching/replacing?  
5.12 How do I make Emacs wrap words for me?  
5.13 Where can I get a better spelling checker for Emacs?  
5.14 How can I spell-check TeX or *roff documents?  
5.15 How do I change load-path?  
5.16 How do I use an already running Emacs from another window?  
5.17 How do I make Emacs recognize my compiler's funny error messages?  
5.18 How do I change the indentation for switch?  
5.19 How to customize indentation in C, C++, and Java buffers?  
5.20 How can I make Emacs automatically scroll horizontally?  
5.21 How do I make Emacs "typeover" or "overwrite" instead of inserting?  
5.22 How do I stop Emacs from beeping on a terminal?  
5.23 How do I turn down the bell volume in Emacs running under X?  
5.24 How do I tell Emacs to automatically indent a new line to the indentation of the previous line?  
5.25 How do I show which parenthesis matches the one I'm looking at?  
5.26 In C mode, can I show just the lines that will be left after #ifdef commands are handled by the compiler?  
5.27 Is there an equivalent to the . (dot) command of vi?  
5.28 What are the valid X resource settings (i.e., stuff in .Xdefaults)?  
5.29 How do I execute ("evaluate") a piece of Emacs Lisp code?  
5.30 How do I change Emacs's idea of the TAB character's length?  
5.31 How do I insert `>' at the beginning of every line?  
5.32 How do I insert "_^H" before each character in a region to get an underlined paragraph?  
5.33 How do I repeat a command as many times as possible?  
5.34 How do I make Emacs behave like this: when I go up or down, the cursor should stay in the same column even if the line is too short?  
5.35 How do I tell Emacs to iconify itself?  
5.36 How do I use regexps (regular expressions) in Emacs?  
5.37 How do I perform a replace operation across more than one file?  
5.38 Where is the documentation for etags?  
5.39 How do I disable backup files?  
5.40 How do I disable auto-save-mode?  
5.41 How can I go to a certain line given its number?  
5.42 How can I create or modify new pull-down menu options?  
5.43 How do I delete menus and menu options?  
5.44 How do I turn on syntax highlighting?  
5.45 How can I force Emacs to scroll only one line when I move past the bottom of the screen?  
5.46 How can I replace highlighted text with what I type?  
5.47 How can I edit MS-DOS files using Emacs?  
5.48 How can I tell Emacs to fill paragraphs with a single space after each period?  
5.49 Why do I get these strange escape sequences when I run  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 How do I set up a `.emacs' file properly?

See Info file `emacs', node `Init File'

In general, new Emacs users should not have `.emacs' files, because it causes confusing non-standard behavior. Then they send questions to help-gnu-emacs@gnu.org asking why Emacs isn't behaving as documented.

Beginning with version 20.1, Emacs includes the new Customize facility, which can be invoked using M-x customize RET. This allows users who are unfamiliar with Emacs Lisp to modify their `.emacs' files in a relatively straightforward way, using menus rather than Lisp code. Not all packages support Customize as of this writing, but the number is growing fairly steadily.

While Customize might indeed make it easier to configure Emacs, consider taking a bit of time to learn Emacs Lisp and modifying your `.emacs' directly. Simple configuration options are described rather completely in See Info file `emacs', node `Init File', for users interested in performing frequently requested, basic tasks.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 How do I get colors and syntax highlighting on a TTY?

As of Emacs 21.1, colors and faces are supported in non-windowed mode, i.e. on Unix and GNU/Linux text-only terminals and consoles, and when invoked as `emacs -nw' on X and MS-Windows. (Colors and faces were supported in the MS-DOS port since Emacs 19.29.) Emacs automatically detects color support at startup and uses it if available. If you think that your terminal supports colors, but Emacs won't use them, check the termcap entry for your display type for color-related capabilities.

The command M-x list-colors-display pops up a window which exhibits all the colors Emacs knows about on the current display.

Syntax highlighting is usually turned off by default; see 5.44 How do I turn on syntax highlighting?, for instructions how to turn it on.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 How do I debug a `.emacs' file?

Start Emacs with the `-debug-init' command-line option. This enables the Emacs Lisp debugger before evaluating your `.emacs' file, and places you in the debugger if something goes wrong. The top line in the `trace-back' buffer will be the error message, and the second or third line of that buffer will display the Lisp code from your `.emacs' file that caused the problem.

You can also evaluate an individual function or argument to a function in your `.emacs' file by moving the cursor to the end of the function or argument and typing C-x C-e (M-x eval-last-sexp).

Use C-h v (M-x describe-variable) to check the value of variables which you are trying to set or use.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4 How do I make Emacs display the current line (or column) number?

To have Emacs automatically display the current line number of the point in the mode line, do M-x line-number-mode. You can also put the form

 
(setq line-number-mode t)

in your `.emacs' file to achieve this whenever you start Emacs. (Line number display is on by default, unless your site-specific initialization disables it.) Note that Emacs will not display the line number if the buffer's size in bytes is larger than the value of the variable line-number-display-limit.

As of Emacs 20, you can similarly display the current column with M-x column-number-mode, or by putting the form

 
(setq column-number-mode t) 

in your `.emacs' file.

The "%c" format specifier in the variable mode-line-format will insert the current column's value into the mode line. See the documentation for mode-line-format (using C-h v mode-line-format RET) for more information on how to set and use this variable.

Users of all Emacs versions can display the current column using the `column' package written by Per Abrahamsen. See section 8.3 Where can I get Emacs Lisp packages that don't come with Emacs?, for instructions on how to get it.

None of the vi emulation modes provide the "set number" capability of vi (as far as we know).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.5 How can I modify the titlebar to contain the current file name?

The contents of an Emacs frame's titlebar is controlled by the variable frame-title-format, which has the same structure as the variable mode-line-format. (Use C-h v or M-x describe-variable to get information about one or both of these variables.)

By default, the titlebar for a frame does contain the name of the buffer currently being visited, except if there is a single frame. In such a case, the titlebar contains Emacs invocation name and the name of the machine at which Emacs was invoked. This is done by setting frame-title-format to the default value of

 
(multiple-frames "%b" ("" invocation-name "@" system-name))

To modify the behavior such that frame titlebars contain the buffer's name regardless of the number of existing frames, include the following in your `.emacs':

 
(setq frame-title-format "%b")


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.6 How do I turn on abbrevs by default just in mode mymode?

Put this in your `.emacs' file:

 
(condition-case ()
   (quietly-read-abbrev-file)
  (file-error nil))

(add-hook 'mymode-mode-hook
          (lambda ()
           (setq abbrev-mode t)))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.7 How do I turn on auto-fill-mode by default?

To turn on auto-fill-mode just once for one buffer, use M-x auto-fill-mode.

To turn it on for every buffer in a certain mode, you must use the hook for that mode. For example, to turn on auto-fill mode for all text buffers, including the following in your `.emacs' file:

 
(add-hook 'text-mode-hook 'turn-on-auto-fill)

If you want auto-fill mode on in all major modes, do this:

 
(setq-default auto-fill-function 'do-auto-fill)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.8 How do I make Emacs use a certain major mode for certain files?

If you want to use a certain mode foo for all files whose names end with the extension `.bar', this will do it for you:

 
(setq auto-mode-alist (cons '("\\.bar\\'" . foo-mode) auto-mode-alist))

Otherwise put this somewhere in the first line of any file you want to edit in the mode foo (in the second line, if the first line begins with `#!'):

 
-*- foo -*-

Beginning with Emacs 19, the variable interpreter-mode-alist specifies which mode to use when loading a shell script. (Emacs determines which interpreter you're using by examining the first line of the script.) This feature only applies when the file name doesn't indicate which mode to use. Use C-h v (or M-x describe-variable) on interpreter-mode-alist to learn more.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.9 How do I search for, delete, or replace unprintable (eight-bit or control) characters?

To search for a single character that appears in the buffer as, for example, `\237', you can type C-s C-q 2 3 7. (This assumes the value of search-quote-char is 17 (i.e., C-q).) Searching for all unprintable characters is best done with a regular expression (regexp) search. The easiest regexp to use for the unprintable chars is the complement of the regexp for the printable chars.

To type these special characters in an interactive argument to isearch-forward-regexp or re-search-forward, you need to use C-q. (`\t', `\n', `\r', and `\f' stand respectively for TAB, LFD, RET, and C-l.) So, to search for unprintable characters using re-search-forward:

M-x re-search-forward RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET

Using isearch-forward-regexp:

M-C-s [^ TAB LFD C-q RET C-q C-l SPC -~]

To delete all unprintable characters, simply use replace-regexp:

M-x replace-regexp RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET RET

Replacing is similar to the above. To replace all unprintable characters with a colon, use:

M-x replace-regexp RET [^ TAB C-q LFD C-q RET C-q C-l SPC -~] RET : RET


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.10 How can I highlight a region of text in Emacs?

You can cause the region to be highlighted when the mark is active by including

 
(transient-mark-mode t)

in your `.emacs' file. (Also see 5.44 How do I turn on syntax highlighting?.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.11 How do I control Emacs's case-sensitivity when searching/replacing?

For searching, the value of the variable case-fold-search determines whether they are case sensitive:

 
(setq case-fold-search nil) ; make searches case sensitive
(setq case-fold-search t)   ; make searches case insensitive

Similarly, for replacing, the variable case-replace determines whether replacements preserve case.

To change the case sensitivity just for one major mode, use the major mode's hook. For example:

 
(add-hook 'foo-mode-hook
          (lambda ()
           (setq case-fold-search nil)))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.12 How do I make Emacs wrap words for me?

Use auto-fill-mode, activated by typing M-x auto-fill-mode. The default maximum line width is 70, determined by the variable fill-column. To learn how to turn this on automatically, see 5.7 How do I turn on auto-fill-mode by default?.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.13 Where can I get a better spelling checker for Emacs?

Use Ispell. See section 9.7 Ispell -- spell checker in C with interface for Emacs.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.14 How can I spell-check TeX or *roff documents?

Use Ispell. Ispell can handle TeX and *roff documents. See section 9.7 Ispell -- spell checker in C with interface for Emacs.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.15 How do I change load-path?

In general, you should only add to the load-path. You can add directory /dir/subdir to the load path like this:

 
(setq load-path (cons "/dir/subdir/" load-path))

To do this relative to your home directory:

 
(setq load-path (cons "~/mysubdir/" load-path)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.16 How do I use an already running Emacs from another window?

emacsclient, which comes with Emacs, is for editing a file using an already running Emacs rather than starting up a new Emacs. It does this by sending a request to the already running Emacs, which must be expecting the request.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.17 How do I make Emacs recognize my compiler's funny error messages?

The variable compilation-error-regexp-alist helps control how Emacs parses your compiler output. It is a list of triplets of the form: (regexp file-idx line-idx), where regexp, file-idx and line-idx are strings. To help determine what the constituent elements should be, load `compile.el' and then type C-h v compilation-error-regexp-alist RET to see the current value. A good idea is to look at `compile.el' itself as the comments included for this variable are quite useful--the regular expressions required for your compiler's output may be very close to one already provided. Once you have determined the proper regexps, use the following to inform Emacs of your changes:

 
(setq compilation-error-regexp-alist
      (cons '(regexp file-idx line-idx)
         compilation-error-regexp-alist))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.18 How do I change the indentation for switch?

Many people want to indent their switch statements like this:

 
f()
{
  switch(x) {
    case A:
      x1;
      break;
    case B:
      x2;
      break;
    default:
      x3;
  }
}

The solution at first appears to be: set c-indent-level to 4 and c-label-offset to -2. However, this will give you an indentation spacing of four instead of two.

The real solution is to use cc-mode (the default mode for C programming in Emacs 20 and later) and add the following line to yoyr `.emacs':

 
(c-set-offset 'case-label '+)

There appears to be no way to do this with the old c-mode.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.19 How to customize indentation in C, C++, and Java buffers?

The Emacs cc-mode features an interactive procedure for customizing the indentation style, which is fully explained in the CC Mode manual that is part of the Emacs distribution, see section `Customization Indentation' in The CC Mode Manual. Here's a short summary of the procedure:

  1. Go to the beginning of the first line where you don't like the indentation and type C-c C-o. Emacs will prompt you for the syntactic symbol; type RET to accept the default it suggests.

  2. Emacs now prompts for the offset of this syntactic symbol, showing the default (the current definition) inside parentheses. You can choose one of these:

    0
    No extra indentation.
    +
    Indent one basic offset.
    -
    Outdent one basic offset.
    ++
    Indent two basic offsets
    --
    Outdent two basic offsets.
    *
    Indent half basic offset.
    /
    Outdent half basic offset.

  3. After choosing one of these symbols, type C-c C-q to reindent the line or the block according to what you just specified.

  4. If you don't like the result, go back to step 1. Otherwise, add the following line to your `.emacs':

     
    (c-set-offset 'syntactic-symbol offset)
    

    where syntactic-symbol is the name Emacs shows in the minibuffer when you type C-c C-o at the beginning of the line, and offset is one of the indentation symbols listed above (+, /, 0, etc.) that you've chosen during the interactive procedure.

  5. Go to the next line whose indentation is not to your liking and repeat the process there.

It is recommended to put all the resulting (c-set-offset ...) customizations inside a C mode hook, like this:

 
(defun my-c-mode-hook ()
  (c-set-offset ...)
  (c-set-offset ...))
(add-hook 'c-mode-hook 'my-c-mode-hook)

Using c-mode-hook avoids the need to put a (require 'cc-mode) into your `.emacs' file, because c-set-offset might be unavailable when cc-mode is not loaded.

Note that c-mode-hook runs for C source files only; use c++-mode-hook for C++ sources, java-mode-hook for Java sources, etc. If you want the same customizations to be in effect in all languages supported by cc-mode, use c-mode-common-hook.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.20 How can I make Emacs automatically scroll horizontally?

In Emacs 21 and later, this is on by default: if the variable truncate-lines is non-nil in the current buffer, Emacs automatically scrolls the display horizontally when point moves off the left or right edge of the window.

In Emacs 20, use the hscroll-mode. Here is some information from the documentation, available by typing C-h f hscroll-mode RET:

Automatically scroll horizontally when the point moves off the left or right edge of the window.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.21 How do I make Emacs "typeover" or "overwrite" instead of inserting?

M-x overwrite-mode (a minor mode). This toggles overwrite-mode on and off, so exiting from overwrite-mode is as easy as another M-x overwrite-mode.

On some systems, Insert toggles overwrite-mode on and off.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.22 How do I stop Emacs from beeping on a terminal?

Martin R. Frank writes:

Tell Emacs to use the visible bell instead of the audible bell, and set the visible bell to nothing.

That is, put the following in your TERMCAP environment variable (assuming you have one):

 
... :vb=: ...                    

And evaluate the following Lisp form:

 
(setq visible-bell t)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.23 How do I turn down the bell volume in Emacs running under X?

On X Window system, you can adjust the bell volume and duration for all programs with the shell command xset.

Invoking xset without any arguments produces some basic information, including the following:

 
usage:  xset [-display host:dpy] option ...
  To turn bell off:
      -b                b off               b 0
  To set bell volume, pitch and duration:
       b [vol [pitch [dur]]]          b on


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.24 How do I tell Emacs to automatically indent a new line to the indentation of the previous line?

Such behavior is automatic in Emacs 20 and later. From the `etc/NEWS' file for Emacs 20.2:

 
** In Text mode, now only blank lines separate paragraphs.  This makes
it possible to get the full benefit of Adaptive Fill mode in Text mode,
and other modes derived from it (such as Mail mode).  TAB in Text
mode now runs the command indent-relative; this makes a practical
difference only when you use indented paragraphs.

As a result, the old Indented Text mode is now identical to Text mode,
and is an alias for it.

If you want spaces at the beginning of a line to start a paragraph, use
the new mode, Paragraph Indent Text mode.

If you have auto-fill-mode turned on (see section 5.7 How do I turn on auto-fill-mode by default?), you can tell Emacs to prefix every line with a certain character sequence, the fill prefix. Type the prefix at the beginning of a line, position point after it, and then type C-x . (set-fill-prefix) to set the fill prefix. Thereafter, auto-filling will automatically put the fill prefix at the beginning of new lines, and M-q (fill-paragraph) will maintain any fill prefix when refilling the paragraph.

If you have paragraphs with different levels of indentation, you will have to set the fill prefix to the correct value each time you move to a new paragraph. To avoid this hassle, try one of the many packages available from the Emacs Lisp Archive (see section 8.3 Where can I get Emacs Lisp packages that don't come with Emacs?.) Look up "fill" and "indent" in the Lisp Code Directory for guidance.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.25 How do I show which parenthesis matches the one I'm looking at?

As of version 19, Emacs comes with `paren.el', which (when loaded) will automatically highlight matching parentheses whenever point (i.e., the cursor) is located over one. To load `paren.el' automatically, include the line

 
(require 'paren)

in your `.emacs' file. Alan Shutko reports that as of version 20.1, you must also call show-paren-mode in your `.emacs' file:

 
(show-paren-mode 1)

Customize will let you turn on show-paren-mode. Use M-x customize-group RET paren-showing RET. From within Customize, you can also go directly to the "paren-showing" group.

Alternatives to paren include:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.26 In C mode, can I show just the lines that will be left after #ifdef commands are handled by the compiler?

M-x hide-ifdef-mode. (This is a minor mode.) You might also want to try `cpp.el', available at the Emacs Lisp Archive (see section 8.3 Where can I get Emacs Lisp packages that don't come with Emacs?).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.27 Is there an equivalent to the . (dot) command of vi?

(. is the redo command in vi. It redoes the last insertion/deletion.)

As of Emacs 20.3, there is indeed a repeat command (C-x z) that repeats the last command. If you preface it with a prefix argument, the prefix arg is applied to the command.

You can also type C-x ESC ESC (repeat-complex-command) to reinvoke commands that used the minibuffer to get arguments. In repeat-complex-command you can type M-p and M-n (and also up-arrow and down-arrow, if your keyboard has these keys) to scan through all the different complex commands you've typed.

To repeat a set of commands, use keyboard macros. (See Info file `emacs', node `Keyboard Macros'.)

If you're really desperate for the . command, use VIPER, a vi emulation mode which comes with Emacs, and which appears to support it. (See section 9.4 VIPER -- vi emulation for Emacs.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.28 What are the valid X resource settings (i.e., stuff in .Xdefaults)?

See Info file `emacs', node `Resources X'.

You can also use a resource editor, such as editres (for X11R5 and onwards), to look at the resource names for the menu bar, assuming Emacs was compiled with the X toolkit.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.29 How do I execute ("evaluate") a piece of Emacs Lisp code?

There are a number of ways to execute (evaluate, in Lisp lingo) an Emacs Lisp form:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.30 How do I change Emacs's idea of the TAB character's length?

Set the variable default-tab-width. For example, to set TAB stops every 10 characters, insert the following in your `.emacs' file:

 
(setq default-tab-width 10)

Do not confuse variable tab-width with variable tab-stop-list. The former is used for the display of literal TAB characters. The latter controls what characters are inserted when you press the TAB character in certain modes.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.31 How do I insert `>' at the beginning of every line?

To do this to an entire buffer, type M-< M-x replace-regexp RET ^ RET > RET.

To do this to a region, use string-insert-rectangle. Set the mark (C-SPC) at the beginning of the first line you want to prefix, move the cursor to last line to be prefixed, and type M-x string-insert-rectangle RET. To do this for the whole buffer, type C-x h M-x string-insert-rectangle RET.

If you are trying to prefix a yanked mail message with `>', you might want to set the variable mail-yank-prefix. Better yet, use the Supercite package (see section 9.2 Supercite -- mail and news citation package within Emacs), which provides flexible citation for yanked mail and news messages; it is included in Emacs since version 19.20. See section 12.1 How do I change the included text prefix in mail/news followups?, for additional information.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.32 How do I insert "_^H" before each character in a region to get an underlined paragraph?

Mark the region and then type M-x underline-region RET.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.33 How do I repeat a command as many times as possible?

Use C-x ( and C-x ) to make a keyboard macro that invokes the command and then type M-0 C-x e.

Any messages your command prints in the echo area will be suppressed.

If you need to repeat a command a small number of times, you can use C-x z, see 5.27 Is there an equivalent to the . (dot) command of vi?.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.34 How do I make Emacs behave like this: when I go up or down, the cursor should stay in the same column even if the line is too short?

M-x picture-mode.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.35 How do I tell Emacs to iconify itself?

C-z iconifies Emacs when running under X and suspends Emacs otherwise. See Info file `emacs', node `Misc X'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.36 How do I use regexps (regular expressions) in Emacs?

See Info file `emacs', node `Regexps'.

The or operator is `\|', not `|', and the grouping operators are `\(' and `\)'. Also, the string syntax for a backslash is `\\'. To specify a regular expression like `xxx\(foo\|bar\)' in a Lisp string, use `xxx\\(foo\\|bar\\)'.

Note the doubled backslashes!


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.37 How do I perform a replace operation across more than one file?

The "tags" feature of Emacs includes the command tags-query-replace which performs a query-replace across all the files mentioned in the `TAGS' file. See Info file `emacs', node `Tags Search'.

As of Emacs 19.29, Dired mode (M-x dired RET, or C-x d) supports the command dired-do-query-replace, which allows users to replace regular expressions in multiple files.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.38 Where is the documentation for etags?

The etags man page should be in the same place as the emacs man page.

Quick command-line switch descriptions are also available. For example, `etags -H'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.39 How do I disable backup files?

You probably don't want to do this, since backups are useful, especially when something goes wrong.

To avoid seeing backup files (and other "uninteresting" files) in Dired, load dired-x by adding the following to your `.emacs' file:

 
(add-hook 'dired-load-hook
          (lambda ()
           (load "dired-x")))

With dired-x loaded, M-o toggles omitting in each dired buffer. You can make omitting the default for new dired buffers by putting the following in your `.emacs':

 
(add-hook 'dired-mode-hook 'dired-omit-toggle)

If you're tired of seeing backup files whenever you do an `ls' at the Unix shell, try GNU ls with the `-B' option. GNU ls is part of the GNU Fileutils package, available from `ftp.gnu.org' and its mirrors (see section 8.5 Where can I get other up-to-date GNU stuff?).

To disable or change the way backups are made, See Info file `emacs', node `Backup Names'.

Beginning with Emacs 21.1, you can control where Emacs puts backup files by customizing the variable backup-directory-alist. This variable's value specifies that files whose names match specific patters should have their backups put in certain directories. A typical use is to add the element ("." . dir) to force Emacs to put all backup files in the directory `dir'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.40 How do I disable auto-save-mode?

You probably don't want to do this, since auto-saving is useful, especially when Emacs or your computer crashes while you are editing a document.

Instead, you might want to change the variable auto-save-interval, which specifies how many keystrokes Emacs waits before auto-saving. Increasing this value forces Emacs to wait longer between auto-saves, which might annoy you less.

You might also want to look into Sebastian Kremer's auto-save package, available from the Lisp Code Archive (see section 8.3 Where can I get Emacs Lisp packages that don't come with Emacs?). This package also allows you to place all auto-save files in one directory, such as `/tmp'.

To disable or change how auto-save-mode works, See Info file `emacs', node `Auto Save'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.41 How can I go to a certain line given its number?

Are you sure you indeed need to go to a line by its number? Perhaps all you want is to display a line in your source file for which a compiler printed an error message? If so, compiling from within Emacs using the M-x compile and M-x recompile commands is a much more effective way of doing that. Emacs automatically intercepts the compile error messages, inserts them into a special buffer called *compilation*, and lets you visit the locus of each message in the source. Type C-x ` to step through the offending lines one by one. Click Mouse-2 or press RET on a message text in the *compilation* buffer to go to the line whose number is mentioned in that message.

But if you indeed need to go to a certain text line, type M-x goto-line RET. Emacs will prompt you for the number of the line and go to that line.

You can do this faster by invoking goto-line with a numeric argument that is the line's number. For example, C-u 286 M-x goto-line RET will jump to line number 286 in the current buffer.

If you need to use this command frequently, you might consider binding it to a key. The following snippet, if added to your `~/.emacs' file, will bind the sequence C-x g to goto-line:

 
 (global-set-key "\C-xg" 'goto-line)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.42 How can I create or modify new pull-down menu options?

Each menu title (e.g., `File', `Edit', `Buffers') represents a local or global keymap. Selecting a menu title with the mouse displays that keymap's non-nil contents in the form of a menu.

So to add a menu option to an existing menu, all you have to do is add a new definition to the appropriate keymap. Adding a `Forward Word' item to the `Edit' menu thus requires the following Lisp code:

 
(define-key global-map           
  [menu-bar edit forward]        
  '("Forward word" . forward-word))

The first line adds the entry to the global keymap, which includes global menu bar entries. Replacing the reference to global-map with a local keymap would add this menu option only within a particular mode.

The second line describes the path from the menu-bar to the new entry. Placing this menu entry underneath the `File' menu would mean changing the word edit in the second line to file.

The third line is a cons cell whose first element is the title that will be displayed, and whose second element is the function that will be called when that menu option is invoked.

To add a new menu, rather than a new option to an existing menu, we must define an entirely new keymap:

 
(define-key global-map [menu-bar words]
  (cons "Words" (make-sparse-keymap "Words")))

The above code creates a new sparse keymap, gives it the name `Words', and attaches it to the global menu bar. Adding the `Forward Word' item to this new menu would thus require the following code:

 
(define-key global-map
  [menu-bar words forward]
  '("Forward word" . forward-word))

Note that because of the way keymaps work, menu options are displayed with the more recently defined items at the top. Thus if you were to define menu options `foo', `bar', and `baz' (in that order), the menu option `baz' would appear at the top, and `foo' would be at the bottom.

One way to avoid this problem is to use the function define-key-after, which works the same as define-key, but lets you modify where items appear. The following Lisp code would insert the `Forward Word' item in the `Edit' menu immediately following the `Undo' item:

 
(define-key-after
  (lookup-key global-map [menu-bar edit])
  [forward]
  '("Forward word" . forward-word)
  'undo)

Note how the second and third arguments to define-key-after are different from those of define-key, and that we have added a new (final) argument, the function after which our new key should be defined.

To move a menu option from one position to another, simply evaluate define-key-after with the appropriate final argument.

More detailed information--and more examples of how to create and modify menu options--are in the Emacs Lisp Reference Manual, under "Menu Keymaps". (See section 3.4 Where can I get documentation on Emacs Lisp?, for information on this manual.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.43 How do I delete menus and menu options?

The simplest way to remove a menu is to set its keymap to `nil'. For example, to delete the `Words' menu (see section 5.42 How can I create or modify new pull-down menu options?), use:

 
(define-key global-map [menu-bar words] nil)

Similarly, removing a menu option requires redefining a keymap entry to nil. For example, to delete the `Forward word' menu option from the `Edit' menu (we added it in 5.42 How can I create or modify new pull-down menu options?), use:

 
(define-key global-map [menu-bar edit forward] nil)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.44 How do I turn on syntax highlighting?

font-lock-mode is the standard way to have Emacs perform syntax highlighting in the current buffer. With font-lock-mode turned on, different types of text will appear in different colors. For instance, if you turn on font-lock-mode in a programming mode, variables will appear in one face, keywords in a second, and comments in a third.

Earlier versions of Emacs supported hilit19, a similar package. Use of hilit19 is now considered non-standard, although `hilit19.el' comes with the stock Emacs distribution. It is no longer maintained.

To turn font-lock-mode on within an existing buffer, use M-x font-lock-mode RET.

To automatically invoke font-lock-mode when a particular major mode is invoked, set the major mode's hook. For example, to fontify all c-mode buffers, add the following to your `.emacs' file:

 
(add-hook 'c-mode-hook 'turn-on-font-lock)

To automatically invoke font-lock-mode for all major modes, you can turn on global-font-lock-mode by including the following line in your `.emacs' file:

 
(global-font-lock-mode 1)

This instructs Emacs to turn on font-lock mode in those buffers for which a font-lock mode definition has been provided (in the variable font-lock-global-modes). If you edit a file in pie-ala-mode, and no font-lock definitions have been provided for pie-ala files, then the above setting will have no effect on that particular buffer.

Highlighting a buffer with font-lock-mode can take quite a while, and cause an annoying delay in display, so several features exist to work around this.

In Emacs 21 and later, turning on font-lock-mode automatically activates the new Just-In-Time fontification provided by jit-lock-mode. jit-lock-mode defers the fontification of portions of buffer until you actually need to see them, and can also fontify while Emacs is idle. This makes display of the visible portion of a buffer almost instantaneous. For details about customizing jit-lock-mode, type C-h f jit-lock-mode RET.

In versions of Emacs before 21, different levels of decoration are available, from slight to gaudy. More decoration means you need to wait more time for a buffer to be fontified (or a faster machine). To control how decorated your buffers should become, set the value of font-lock-maximum-decoration in your `.emacs' file, with a nil value indicating default (usually minimum) decoration, and a t value indicating the maximum decoration. For the gaudiest possible look, then, include the line

 
(setq font-lock-maximum-decoration t)

in your `.emacs' file. You can also set this variable such that different modes are highlighted in a different ways; for more information, see the documentation for font-lock-maximum-decoration with C-h v (or M-x describe-variable RET).

You might also want to investigate fast-lock-mode and lazy-lock-mode, versions of font-lock-mode that speed up highlighting. These are the alternatives for jit-lock-mode in versions of Emacs before 21.1. The advantage of lazy-lock-mode is that it only fontifies buffers when certain conditions are met, such as after a certain amount of idle time, or after you have finished scrolling through text. See the documentation for lazy-lock-mode by typing C-h f lazy-lock-mode (M-x describe-function RET lazy-lock-mode RET).

Also see the documentation for the function font-lock-mode, available by typing C-h f font-lock-mode (M-x describe-function RET font-lock-mode RET).

For more information on font-lock mode, take a look at the font-lock-mode FAQ, maintained by Jari Aalto at

ftp://cs.uta.fi/pub/ssjaaa/ema-font.gui

To print buffers with the faces (i.e., colors and fonts) intact, use M-x ps-print-buffer-with-faces or M-x ps-print-region-with-faces. You will need a way to send text to a PostScript printer, or a PostScript interpreter such as Ghostscript; consult the documentation of the variables ps-printer-name, ps-lpr-command, and ps-lpr-switches for more details.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.45 How can I force Emacs to scroll only one line when I move past the bottom of the screen?

Place the following Lisp form in your `.emacs' file:

 
(setq scroll-step 1)

See Info file `emacs', node `Scrolling'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.46 How can I replace highlighted text with what I type?

Use delete-selection-mode, which you can start automatically by placing the following Lisp form in your `.emacs' file:

 
(delete-selection-mode t)

According to the documentation string for delete-selection-mode (which you can read using M-x describe-function RET delete-selection-mode RET):

When ON, typed text replaces the selection if the selection is active. When OFF, typed text is just inserted at point.

This mode also allows you to delete (not kill) the highlighted region by pressing DEL.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.47 How can I edit MS-DOS files using Emacs?

As of Emacs 20, detection and handling of MS-DOS (and Windows) files is performed transparently. You can open MS-DOS files on a Unix system, edit it, and save it without having to worry about the file format.

When editing an MS-DOS style file, the mode line will indicate that it is a DOS file. On Unix and GNU/Linux systems, and also on a Macintosh, the string `(DOS)' will appear near the left edge of the mode line; on DOS and Windows, where the DOS end-of-line (EOL) format is the default, a backslash (`\') will appear in the mode line.

If you are running a version of Emacs before 20.1, get crypt++ from the Emacs Lisp Archive (see section 8.3 Where can I get Emacs Lisp packages that don't come with Emacs?). Among other things, crypt++ transparently modifies MS-DOS files as they are loaded and saved, allowing you to ignore the different conventions that Unix and MS-DOS have for delineating the end of a line.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.48 How can I tell Emacs to fill paragraphs with a single space after each period?

Ulrich Mueller suggests adding the following two lines to your `.emacs' file:

 
(setq sentence-end "[.?!][]\"')}]*\\($\\|[ \t]\\)[ \t\n]*")
(setq sentence-end-double-space nil)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.49 Why do I get these strange escape sequences when I run

ls from the Shell mode?

This happens because ls is aliased to `ls --color' in your shell init file. You have two alternatives to solve this:


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by (Blade) GNU s/w Owner on November, 2 2001 using texi2html