University of Massachusetts Amherst / Office of Information Technologies

The original of this document can be found at: http://www.oit.umass.edu/hds/docs/h/h108b.html

Using EMACS on OITUNIX

Appendix B: Useful Emacs Commands

Handout: 108b
May 1997

Operating System: OITUNIX
Description: EMACS commands


Go to PDF file for printing.

useful EMACS commands
alphabetized list of EMACS commands with keybindings (if any) and descriptions
discussion

ARG: the "universal argument" -- C-u #
numeric arguments -- M #
more on the command line -- M-x
marking a region -- C-@ or C-2
marking a rectangle
buffers and windows
kill vs. delete and the yank buffer
modes and filename extensions
string matching and regular expressions
special characters in regular expressions


useful EMACS commands

Below are two lists of useful EMACS commands. Neither list is complete. For complete documentation refer to the GNU EMACS Manual, which is available in Acadmeic Computing reference areas. The first list is grouped according to functions. The next list is alphabetized by the names of the commands.

      
      EMACS key    ProComm for DOS key     EMACS key    ProComm for DOS key    
      M            Alt-4                   C-\          Alt-1

EMACS commands -- by functions

   
      

getting help

C-h help-command C-h a help-apropos C-h t help-with-tutorial C-h b describe-bindings C-h ? help-for-help C-h C-h help-for-help

leaving EMACS

C-x C-c save-buffers-kill emacs C-z suspend-emacs

error recovery

C-g cancel-command C-x u undo-last-command C-l redraw-garbage-screen

cursor motion

C-a beginning-of-line C-b backward-character C-e end-of-line C-f forward-character C-n next-line C-p previous-line C-o open-line M-f forward-word M-b backward-word

scrolling

C-v scroll-up C-l center-current-line-in window C-x < scroll-left C-x > scroll-right M- < beginning-of-buffer or-file M- > end-of-buffer-or-file M-v scroll-down

marking commands

C-@ set-mark-command or C-2 C-x C-x exchange-point-and-mark

killing and deleting

<X| delete-backward-character (VT320) C-d delete-character C-k kill-line C-w kill-region C-y yank C-x k kill-buffer M-d kill-word M-k kill-sentence M-w copy-region-as-kill M-y yank-pop

incremental search

C-\ incremental-search-forward (OIT only) C-r incremental-search-backward [return] exit-incremental-search

replacing text

M-% query-replace

rectangle commands

C-x r o open-rectangle C-c k kill-rectangle (no key) yank-rectangle C-x r copy-rectangle-to-register

window commands

C-x 2 split-window vertically (1 above other) C-x 3 split-window horizontally C-x 0 delete-current-window C-x 1 delete-other-window C-x o other-window C-x ^ enlarge-window-vertically C-x } enlarge-window-horizontally

file & buffer commands

C-x C-f find-file C-x C-w write-file (prompts for name) C-x i insert-file M-s save-buffer-with-same-name C-x C-b list-buffers C-x b switch-to-buffer

utilities

C-u universal-argument C-c w what-line C-c v overwrite-mode toggle C-x = what-cursor-position M-! shell-command M-g goto-line M-= count-lines-in-region

alphabetized list of EMACS commands with keybindings (if any) and descriptions

See the discussion of ARG and other mysterious phrases in the following list. If there is no keybinding, you enter M-x to move to the command line and enter the command.

   
      keys     commands                       descriptions
   
               append-to-file                 append contents of region to end
                                              of a file.  Prompts for filename.
   
      C-b      backward-char                  Move cursor left ARG characters 
                                              (right if ARG is negative).
   
      M- <     beginning-of-buffer            Move cursor to the beginning of 
                                              the buffer; leave mark at 
                                              previous position.
    
      C-a      beginning-of-line              Move cursor to the beginning 
                                              of the current line
   
      C-h a    command-apropo                 Prompts for a string, and lists 
                                              all EMACS commands that contain
                                              that string, with  brief 
                                              description.
   
               copy-file                      Copy FILE to new name.  Prompts
                                              for old file name and new file
                                              name.
   
      C-x r    copy-rectangle-to-register     Copy rectangular area into a 
                                              register.  Prompts for register
                                              name which must be one character.
   
      M- w     copy-region-as-kill            Save the region as if killed, but
                                              don't kill it.
   
      M- =     count-lines-region             Display number of lines in 
                                              the region.
   
               count-matches                  Display number of matches for a 
                                              string (REGEXP) following cursor.
   
      C-d      delete-char                    Delete the following ARG 
                                              characters (previous with 
                                              negative ARG)
  
               delete-file                    Delete specified file.  Prompts 
                                              for a file name.
   
      C-x 1    delete-other-windows           Make current window fill the 
                                              screen
   
               delete-rectangle               Delete (don't save) text in 
                                              rectangle with cursor and mark 
                                              as corners.
   
      C-x 0    delete-window                  Remove current window from the 
                                              display.
   
      C-h b    describe-bindings              Show a list of all defined 
                                              keys, and their definitions.
   
      C-h k    describe-key                   Display documentation of the 
                                              function which KEY invokes. 
   
      M->      end-of-buffer                  Move cursor to the end of the 
                                              buffer; leave mark at previous 
                                              position.
   
      C-e      end of line                    Move cursor to end of current 
                                              line.
   
      C-x^     enlarge-window-vertically      Make current window ARG lines 
                                              bigger.
   
      C-x}     enlarge-window-horizontally    Make current window ARG columns
                                              wider.
   
      C-x C-x  exchange-point-and-mark        Put the mark where cursor is now,
                                              and cursor where mark is now.
   
      M-x      execute-extended-command       Prefix for typing any 
                                              command from the command line.
     
      C-x C-f  find-file                      Edit file FILENAME.
   
      C-f      forward-character              Move cursor right ARG charaters 
                                              (left if ARG is negative).
   
      M-g      goto-line                      Go to line ARG, counting from 
                                              line 1 at beginning of buffer;
                                              prompts for line number.
   
      C-h      help-command
   
      C-h?     help-for-help                  You have typed C-h, the help 
                                              character.  Type a Help option,
                                              or c-h for a description of the 
                                              options.
   
      C-h t    help-with-tutorial             Select the EMACS learn by 
                                              doing tutorial.
   
      C-x i    insert-file                    Insert contents of a file into 
                                              buffer after cursor.
   
      C-x g    insert-register                Insert contents of a register.  
                                              Prompts for a name of register 
                                              (one character).
   
      C-\      isearch-forward                Do incremental search forward.  
                                              OIT version of EMACS only; cannot
                                              be used with ProComm for DOS.
   
      C-r      isearch-backward               Do incremental search backward.
   
               keep-lines                     Delete all lines except those 
                                              containing a given string 
                                              (REGEXP)
      
      C-g      keyboard-quit                  Restart a sequence of command 
                                              keys.
    
      C-x k    kill-buffer                    Get rid of the specified buffer. 
                                              Prompts for name of buffer.
   
      C-k      kill-line                      Kill the rest of the current
                                              line. If ARG is given, kills next
                                              ARG lines.
   
      C-w      kill-region                    Kill between cursor and mark.
   
      C-c k    kill-rectangle                 Remove rectangle with corners 
                                              at cursor and mark; (save in yank
                                              buffer).
   
      C-x C-b  list-buffers                   Display a list of names of 
                                              existing buffers.
    
      C-x C-d  list-directory                 Display a list of files in 
                                              a directory, as with ls.
   
               list-matching-lines or occur   Show all lines following cursor 
                                              containing a match for a given 
                                              string (REGEXP).
   
      C-n      next-line                      Move cursor verticaly down ARG 
                                              lines.
   
               occur                          Show all lines that follow 
                                              current line that contain a match
                                              for a given string (REGEXP).
   
               occur-mode-goto-occurrence     Goes to line in buffer where 
                                              occurrence was found.  Must be
                                              entered from the window that
                                              shows the occurrences.
      
      C-c o    open-rectangle                 Blank out rectangle with corners 
                                              at cursor mark, shifting text
                                              right.
   
      C-x o    other-window                   Select the ARG'th different 
                                              window.
   
      C-c v    overwrite-mode                 Toggle overwrite mode.
    
      C-p      previous-line                  Move cursor vertically up ARG 
                                              lines.
   
      M- %     query-replace                  Replace some occurrences of 
                                              FROM-STRING with TO-STRING.
                                              possible responses for Query-
                                              replace are:
                                              (SPC =yes; Del =no; ! =replace 
                                              all; M  means quit)
   
      C-^      quoted-insert                  Read next input character and 
                                              insert it.
   
      C- l     redraw-display                 Clear the screen and display
                                              again what is supposed to appear
                                              on it.
   
               replace-string                 Replace all occurrences of 
                                              FROM-STRING with TO-STRING.
   
      M- s     save-buffer                    Save current buffer in file 
                                              (if modified).
   
      C-x C-c  save-buffers-kill-emacs        Offer to save each buffer, then 
                                              quit EMACS.
   
      M- v     scroll-down                    Scroll text of current window 
                                              downward (backwards).  ARG lines 
                                              or full screen if no ARG is
                                              given.
   
      C-x <    scroll-left                    Scroll display ARG columns left, 
                                              or full screen if no ARG is
                                              given.
   
      C-x >    scroll-right                   Scroll display ARG columns right,
                                              or full screen if no ARG is 
                                              given.
   
      C-v      scroll-up                      Scroll text of current window 
                                              upward (forward) ARG lines; or
                                              full screen if no ARG.
   
      C-@ or C-2  
               set-mark-command               Set mark at current cursor 
                                              location.
   
               set-variable                   Set the value of a VARIABLE; 
                                              prompts for variable name and 
                                              value.
   
               shell                          Start a "shell" for running unix 
                                              commands without leaving EMACS.
   
      M- !     shell-command                  Execute a unix command from 
                                              inside EMACS
   
               shrink-window                  Make current window ARG lines 
                                              smaller.
   
      C-x 3    split-window-horizontally      Split current window into two
                                              windows side by side.
   
      C-x 2    split-window-vertically        Split current window into two 
                                              windows; one above the other.
   
      C-z      suspend-emacs                  Stop EMACS and return to superior
                                              process.  You can resume EMACS at
                                              the same place using %emacs.
   
      C-x b    switch-to-buffer               Select a different buffer in the 
                                              current window. Prompts for 
                                              buffer name.
   
      C-x C-u  upcase-region                  Convert the region to upper case.
                                              In programs, wants two arguments.
   
      C-x u    undo                           Undo some previous changes.
   
      C-u      universal-argument             Enter a numeric argument for the 
                                              following command.
   
               view-register                  Display what is contained in a 
                                              register. Prompts for 1 character
                                              register name.
   
      C-x =    what-cursor-position           Display information on Cursor 
                                              Position (on screen and within 
                                              Buffer).
   
      C-c w    what-line                      Display the line number (within 
                                              the buffer) of the cursor.
   
      C-x C-w  write-file                     Write current buffer to a file.
   
               write-region                   Write current region to a file.
   
      C-y      yank                           Reinsert the last stretch of 
                                              killed text.
   
      M- y     yank-pop                       Replace just-yanked stretch of 
                                              killed-text with previously 
                                              killed text.
   
               yank-rectangle                 Insert the last killed rectangle 
                                              with upper left corner at cursor.

discussion

ARG: the "universal argument" -- C-u #

The "universal argument" is used to enter a numeric argument or repeat factor for an EMACS command. In the descriptions of EMACS commands, the word ARG is used to refer to the universal argument. The universal argument may also be negative (to cause the command to move or act backwards through the buffer).

The universal argument must precede the command to which it is to be applied.

Examples:
a) C-d deletes the character the cursor is on. If you need to delete 10 characters, starting at the cursor, you could press C-d 10 times, or you could use the universal argument, and type C-u 10 C-d.

b) Without numeric argument, C-v scrolls up (forward) a screen. Using universal argument, you can scroll forward a specific number of lines: C-u 50 C-v scrolls forward 50 lines of text.

c) to insert 33 asterisks into your file at the current cursor location, type: C-u 33 *

If you do not precede one of these commands by the universal argument, a default number will be used: a screenful for the scrolling commands; 1 for most other commands.

numeric arguments -- M #

The numeric argument is similar to the "universal argument": any EMACS command may be given a numeric argument that indicates how many times the command should execute. Numeric arguments may also be negative (to cause the command to move or act backwards through the buffer).

Examples:
a) M5 C-d deletes the character the cursor is on plus the next 4 characters.

b) M10 C-v scrolls up (forward) 10 lines.
M-10 C-v scrolls down (backward) 10 lines.

more on the command line -- M-x

Some commands do not have a key binding. These commands must be entered on the command line (also referred to as the mini- buffer). For commands that you don't use very often, you may find it easier to remember the name of the command than its binding. Press M-x, then you can type any EMACS command.

Examples:

The command "replace-string" does not have a key binding. To use it, press M-x then type replace-string.

The "copy-region-as-kill" command has the key binding M-w. If you want to use this command, but don't remember the binding press M-x and type copy-region.

When you are typing commands on the command line, you only need to type enough of the command to make each word unique; EMACS will fill in the rest. For example to enter "replace string", you can just type repl and press the space bar. EMACS completes the first word, replace-, and waits for you to type the second word. For the second word, it's enough to type st and press <return>. If you do not type enough to make a word unique, EMACS will show you a list of all possibilities. You must then enter additional letters until your choice is clear. In the previous example, if you had only typed rep [for replace] and pressed space, EMACS would show you a list of commands beginning with the words repeat and replace. When you add the "l", it completes the first word.

marking a region -- C-@ or C-2

Many EMACS commands operate on only a selected part of your file. For example you might want to copy, move, or delete a section of text. To select the part you want to do something with, you need to put a "mark" at the beginning of the chosen section, and the cursor at the end (or the cursor at the beginning of the section, and a "mark" at the end.)

The "mark" is invisible. You set it by typing C-@ or C-2 at the place you want marked. Once the mark is set, it remains in the same place until you set it somewhere else. In addition to the C-2 command, the beginning-of-buffer (M-<) and end-of-buffer (M->) commands also reset the mark.

Having marked one end of the section you want, move the cursor to the other end. Everything starting at the mark, up to (but not including) the cursor is called a "region." Any EMACS command with the word region in it will apply to just that portion of your file. Examples:

      
      copy-region      will copy the selected text to the yank buffer
      kill-region      will delete the selected text
      write-region     will write the selected text to a file

marking a rectangle

Rectangles are marked in exactly the same way as regions: put the mark in one corner of the rectangle and the cursor in the diagonally opposite corner. Any command that operates on rectangles will affect only the rectangular area demarcated by the mark and cursor. Examples:

      
      open-rectangle      will shift everything right, to leave a 
                          blank rectangle
      kill-rectangle      will delete the marked rectangle
When a mark is set, the area between the mark and cursor could be used either as a region or a rectangle.

buffers and windows

A "buffer" is a temporary holding area. When you edit a file using EMACS, the file is loaded into an EMACS "buffer." The buffer has the same name as the file that is loaded into it. You edit the "buffer" copy of the file but the original is not changed until you save the buffer.

EMACS lets you keep many files open for editing at the same time. Each open file is loaded into a different buffer. Each time you open a file with find-file (C-x C-f), you are opening a new buffer. In addition, you could also have a "shell" buffer, which allows you to execute unix commands without leaving EMACS. However, you don't usually see all your buffers on the screen. The active or current buffer fills the whole screen; the others (if there are any) are "behind" the current buffer, so you can't see them. The name of the current buffer is displayed on the formation line near the bottom of the screen. When you want to work on a different buffer, you use the switch-to-buffer (C-x b) command. If you can't remember the names of the buffers you have opened, use the list-buffers (C-x C-b) command.

"Windows" are partitions of the screen. You can split your screen into 2 or more "windows," either horizontally or vertically, using the split-window commands. Then you can look at a different buffer in each window. The name of the buffer is listed at the bottom of each window.

kill vs. delete and the yank buffer

The delete commands of EMACS remove parts of text and throw them away. The only way to retrieve something you have deleted is with undo (C-x u), which will put it back in the same place.

The kill commands also remove parts of text, but they are saved in the "kill" buffer. You can take text from the kill buffer and insert it anywhere in your file. Furthermore, when you "insert" something from the kill buffer, it is still available in the kill buffer, so you can yank it again. You can use the kill buffer to duplicate a section of text in many different places in your file.

When you use the "yank" (C-y) command, you always get back the most recently killed text. If you need to insert something that was killed earlier, use yank-pop (M-y) following the yank. This will remove the text that was just yanked, and replace it with the next previously killed text. You can use yank-pop several times until you get back to the text you want to insert. See the OIT tutorial section of this handout for examples of kill and yank.

modes and filename extensions

EMACS has special commands for features that are based on the syntax of particular programming languages and certain applications. The indicator to EMACS is the filename extension, if any. The available EMACS modes and their associated extensions include Fortran, Lisp, C, LaTex, etc. For a list of the modes and their extensions, do a help on variable auto-mode-alist:

      
         C-h   v
         auto-mode-alist

string matching and regular expressions

The descriptions of many EMACS commands use the words "regular expression," or REGEXP for short. A regular expression is just a string of characters; however, some characters have special meaning which can be used to make searches more general.

For example, the list-matching-line command lists all lines in the current EMACS buffer which contain matches for a regular expression. Suppose you asked EMACS to list- matching-lines for the regular expression "out". It would list all lines in which any of the following words occurred: out, shout, outside, and many others.

special characters in regular expressions

The period (.) can be used as a "wild card" to replace a single character. In the above example, if you asked to list-matching-lines for the regular expression "o.t" EMACS would list any lines containing words such as loiter and boats, in addition to out, shout, etc. It would not match doubt, however; the period can only replace one character.

Now suppose you are working with a file that contains decimal numbers, and you need to look at those numbers that end with ".5". If you use ".5" as the regular expression in the search, the decimal point will be interpreted as the wildcard, and EMACS will match it with numbers such as 15.0, and 35.2, as well as 10.5. You can use the backslash (\) before any special character to make it lose its meaning as a special character. So the regular expression \.5 will get you what you want- just numbers like 1.5, 33.5, etc.

There are other special characters and regular expressions can get quite complex. Here we will merely mention what the other special characters are so you will not accidentally use them in a search string. The other special characters are: $, ^, *, +, ?, [, and ]. If you need to use a search string that includes one of these characters, be sure to precede that character with a backslash (\) so it will lose its special meaning.


Back to the OIT Help Desk Printed and On-line Documentation list
Back to the Subject Index for OIT documents
Back to the beginning of this document

the previous section of this handout


OIT Home HDS Home Online Help Desk

UMass Logo Copyright © 2000.

This is an Official Page/Publication of the University of Massachusetts Amherst Campus Office of Information Technologies.

campbell@oit.umass.edu