Emacs logo
Source: gnu.org
Basic Functionality
  • Open an existing file by entering emacs in the shell followed by the name of the file. This will default to running Emacs in a GUI, but it can also be run within the shell (emacs -nw). Note that to use the GUI with a remote connection such as Stampede2, you must use X11 forwarding (covered in the Remote Connections section), otherwise the emacs command will open within the shell. If you want to run the GUI and keep the shell free, you can open and background Emacs with emacs &. Use Ctrl-x f to open a file from within Emacs.
  • Create a new file in the same way as opening a file by specifying the new filename. The new file will not be saved unless specified.
  • Save a file that is currently open by entering the Ctrl-x Ctrl-s command.
  • Quit by entering Ctrl-x Ctrl-c.
  • Save and Quit is the same command as quitting, except that when you have unsaved files it will ask if you would like to save each one. To save, enter y.
  • Edit a file by simply entering and removing text.
  • Cancel a command before completely entering it or a command that is executing with Ctrl-g or by hitting Esc 3 times.
Navigation

Similar to Vim, navigation in Emacs has shortcuts and extra features. Basic movement can be done using the arrow keys or using the letter keys in the following table:

Keys for basic movement.
MoveCommand
Ctrl-b
Ctrl-n
Ctrl-p
Ctrl-f

The benefits of using the alternate keys is that you do not have to move your hand back-and-forth to the arrow keys, and can more effectively enter Emacs commands (once you are practiced). Some other examples of navigation shortcuts include:

  • Move to the next screen view: Ctrl-v
  • Move to the previous screen view: Alt-v
  • Move to the next word: Alt-f This can also be used with a number to move multiple words at once (i.e. Alt-5f moves 5 words forward).
  • Move to the previous word: Alt-b This can be used with a number in the same way to move multiple words at once.
  • Move to the beginning of the line: Ctrl-a
  • Move to the end of the line: Ctrl-e
  • Move to the beginning of a sentence: Alt-a
  • Move to the end of a sentence: Alt-e

Note that the more customary keys Page Up, Page Down, Home, and End all work as expected.

Editing Features

Here are some important commands to know:

  • Undo the previous command with the command Ctrl-x u
  • Redo the previous command (after undo) by performing a non-editing command (such as Ctrl-f), and then undo the undo with Ctrl-x u
  • Delete or Cut characters, words, or lines (that can then be pasted elsewhere):
    • Backspace to delete a single character before the cursor
    • Ctrl-d to delete a single character after the cursor
    • Alt-Backspace to delete the word before the cursor
    • Alt-d to delete the word after the cursor
    • Ctrl-k to delete from the cursor to end of the line
    • Alt-k to delete from the cursor to end of the sentence
  • Paste characters, words, or lines:
    • Ctrl-y pastes the most recent deleted text
    • Alt-y pastes the deleted text before the most recent
  • Copy characters, words, or lines: The easiest way to copy is actually to cut the text and then paste it back where it was. Then it can be pasted in a new location also.
  • Search for strings throughout a file and optionally replace:
    • Ctrl-s starts a forward search that is incremental (each character you enter updates the search). Entering Ctrl-s again skips to the next occurrence. Enter ends the search.
    • Ctrl-r starts a backwards search that behaves similarly to the forward search.
    • Search and replace has many options, but one example is to find all occurrences of "foo" in the file and replace them with "bar" with the command: Alt-x replace-string foo Enter bar
    • You can use tab-completion for entering commands after typing Alt -x. For example, type Alt-x, then rep, then hit Tab twice to see a list of matching commands. Since similar commands are named similarly, you will find other useful related commands, such as replace-regexp.
  • Split the screen vertically or horizontally to view multiple files at once in emacs:
    • Ctrl-x 3 will split the screen horizontally
    • Ctrl-x 2 will split the screen vertically
    • Ctrl-x 1 closes all panes except the active one
    • Ctrl-x 0 closes a pane
Highlighting Mode

This mode enables you to highlight words in sections to perform commands on them, such as copy or delete. For example, if you want to copy a few words from a single line and paste them on another line:

  1. Navigate to the first character of the first word you want to copy
  2. Enter highlighting mode by Ctrl-Space
  3. Navigate to the last character of the last word you want to copy (this should highlight all the words you want)
  4. Enter Alt-w to copy the words
  5. Navigate to where you want to paste the words
  6. Enter Ctrl-y to paste
Emacs and the Shell

There are several options for running shell commands from Emacs. To execute a single shell command while in Emacs, use the command Alt-! followed by the shell command and hit Enter. The output of the command will display in a portion of the screen called an echo area. There are several more features for running shell commands, including running an interactive shell inside Emacs (we recommend ansi-term). For more about these features, please see the Emacs documentation on the topic. Alternatively, you can suspend Emacs with the command Ctrl-z. As with suspending Vim, you can execute commands in the shell, and then return to Emacs with the foreground command fg.

Customization

Emacs is customizable in many ways including changing the key bindings for commands, the color scheme (themes), and more. Due to the breadth of options, we refer you to existing documentation on customization.

A Hands-On Tutorial

One of the most effective ways to learn Emacs is through the built-in hands-on tutorial that can be accessed by opening Emacs without any filename input. It will walk you through all the major functionalities of Emacs as well as a few useful tips and tricks. If you plan to use Emacs even occasionally, it is a great resource. Furthermore, the above list of features and commands is not exhaustive, and the interested new Emacs user should certainly explore the man pages and online resources to discover more Emacs features. In particular, buffers are a useful concept to understand when using Emacs, but are not covered here.

Additionally, you may want to consider looking into spacemacs if you are familiar with Vim key bindings or would like to continue using emacs with more customization. Users can install spacemacs to their local directory on Stampede2 using git.

 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement