Emacs-Version Control
Emacs-Version Control
Emacs helps you manipulate and edit files stored in version control. Emacs supports CVS, Subversion,
bzr, git, hg, and other systems, but it offers a uniform interface, called VC, regardless of the version
control system you are using.
Emacs automatically detects when a file you're editing is under version control, and displays something
like this in the mode line: CVS-1.14 to indicate the version control system in use, and the current
version.
M-x vc-next-action or C-x v v commits the current file (prompting you for a log message) if
you've modified it. (Under version control systems that require locking, this command also acquires a
lock for you.)
VC provides other commands for version control-related tasks:
C-x v =
Displays a diff showing the changes you've made to the current file.
C-x v ~
Prompts you for a version number and shows you that version of the current file in another
window.
C-x v g
Displays an annotated version of the file showing, for each line, the commit where that line was
last changed and by whom. On any line you can press L to view the log message for that commit
or D to view the associated diff.
C-x v l
Displays a log of previous changes to the file. When the cursor is on a particular log entry, you
can press d to view the diff associated with that change or f to view that version of the file.
Some special features are enabled when you are looking at a diff (whether from a file, or one produced
by VC). Emacs lets you move around in a diff by hunks or files, apply individual hunks to a file,
reverse a diff, and do other operations useful for reading or manually editing diffs.
See (info "(emacs)Diff Mode") for more information.
Emacs server
Some people like to keep only a single instance of Emacs open and edit all their files in there. Doing
this has a few advantages:
You can kill/yank text between buffers in the same instance of Emacs.
Emacs remembers argument histories (what commands you've used, what files you've opened,
terms you've searched for, etc.), but only within each instance.
If you have many customizations, starting new instances of Emacs is slow.
Alas, when you type emacs in a shell to edit a file (or when $EDITOR is invoked by an external
program), a new instance of Emacs is started. You can avoid this by using emacsclient, which
instead opens a new frame connected to an existing instance of Emacs:
1. In your existing instance of Emacs, type M-x server-start. Or add (server-start)
to your .emacs file to make it do that automatically at startup.
2. To edit a file, type emacsclient -t FILENAME at a prompt. You can also change your
$EDITOR to emacsclient -t if you're using programs that automatically invoke
$EDITOR. (emacsclient -t opens a new frame on the terminal; alternatively,
emacsclient -c opens a new X frame.)
3. When you're done editing, type C-x C-c, which closes the frame.
Major modes
Every buffer has an associated major mode, which alters certain behaviors, key bindings, and text
display in that buffer. The idea is to customize the appearance and features available based on the
contents of the buffer.
Emacs ships with dozens of major modes for editing widely used programming languages, markup
languages, and configuration file formats. These major modes tell Emacs how to:
Indent your code correctly (usually, simply pressing TAB once will make Emacs indent the
current line correctly).
Do syntax highlighting
Identify the boundaries of functions
Invoke interpreters, compilers, or debuggers for your code.
Some commands we've seen, like M-x dired, M-x compile, and M-x shell, in fact use their
own special major modes to make their buffers provide certain features (such as highlighting compile
errors and making them clickable).
The current major mode is displayed in the mode line. The last thing on the mode line should be one or
more items in parentheses, like (Python Narrow). The first of these is the name of the major
mode.
You can switch modes in an existing buffer by using M-x and the name of the mode:
Minor modes
Every buffer can also have any number of minor modes, which are extra pieces of functionality you can
enable, independently of each other and of the major mode. Minor modes are listed in the mode line
after the major mode inside the parentheses. Here are a few commonly used ones:
M-x auto-fill-mode
Wraps your lines automatically when they get longer than 70 characters.
M-x flyspell-mode
M-x follow-mode
If you have a buffer displayed in two windows side by side, follow-mode forces them to scroll
together such that the text displayed in the second window comes right after the text in the first
window, and moving your cursor off the bottom of the left window causes it to appear at the top
of the right window:
Some minor modes are global, i.e. they affect the behavior of the entire editor, rather than just that of a
specific buffer.
M-x icomplete-mode In the M-x prompt (and elsewhere), show completions as you type
M-x iswitchb-mode Show all buffer names when you switch buffers with C-x b
See (info "(emacs)Minor Modes") for more information.
If you need help with a particular mode, C-h m describes the active major and minor modes. The
mode description often lists important commands which are useful in that mode, which is helpful when
you're learning to use a new mode.
The minibuffer
The minibuffer (the space at the bottom of the frame) is where Emacs prompts you for input in most
situations: for a command, when you type M-x; for a file name, within M-x find-file; for an Elisp
expression, within M-x eval-expression, etc. Here are some features common to most
minibuffer prompts:
You can use most buffer editing and movement commands. You can move around in, kill text
from, and yank text to minibuffers.
You can browse previous inputs to the current prompt using M-p and M-n.
Tab completion is often available. For example, the M-x prompt offers tab completion, so you
needn't worry about typing long command names like M-x wdired-change-to-wdired-
mode when M-x wdired-ch TAB suffices.
If your meta key is not working, in order to type a key that contains meta, you can instead type ESC,
then the remaining keys. For example, ESC x is the same as M-x, and ESC C-s is the same as C-M-
s.
Migrating to Emacs
Emacs has a number of options for easing the transition from other editing environments.
You can learn more about CUA mode by typing C-h f cua-mode RET.
Emacs resources
The GNU Emacs Manual (C-h r) is the definitive guide if you want to learn more about Emacs.
Some Emacs features have their own, separate manuals (C-h i d).
The GNU Emacs FAQ (C-h C-f) answers many questions that beginners have about how Emacs
works and how to set it up to do particular things.
EmacsWiki contains information about many downloadable Emacs extensions, as well as tips for using
and customizing Emacs.
You can ask questions on the help-gnu-emacs mailing list.