17.3 Modes

Every buffer in Emacs has an associated mode. This mode is very different from the idea of modes in vi: a mode tells you what kind of buffer you are in. For example, there is text-mode for normal text files, but there are also modes such as c-mode for editing C programs, sh-mode for editing shell scripts, latex-mode for editing LaTeX files, mail-mode for editing email and news messages, etc. A mode provides special customizations and functionality that is useful for the kind of file you are editing. It is even possible for a mode to redefine keys and key commands. For example, in Text mode, the Tab key simply jumps to the next tab stop, but in many programming language modes, the Tab key indents the current line according to the depth of the block that line is in.

The modes mentioned above are called major modes. Each buffer has exactly one major mode. Additionally, a buffer can have one or more minor modes. A minor mode provides additional features that may be useful for certain editing tasks. For example, if you hit the INSERT key, you invoke overwrite-mode, which does what you'd expect. There is also an auto-fill-mode, which is handy in combination with text-mode or latex-mode: it causes each line that you type to be automatically wrapped once the line reaches a certain number of characters. Without auto-fill-mode, you have to type M-q to fill out a paragraph. (Which you can also use to reformat a paragraph after you've edited some text in it and it is no longer nicely filled out.)

17.3.1 Opening files

To open a file in Emacs, type

C-x C-f

Emacs will ask you for the name of the file, filling in some default path for you (which is usually ~/ ). After you type the filename (you can use Tab completion) and hit ENTER , Emacs will open the file in a new buffer and display that buffer on the screen.


Emacs will automatically create a new buffer, it will not load the file into the current buffer.

In order to create a new file in emacs, you cannot just go typing right away. You first have to create a buffer for it, and come up with a filename. You do this by typing C-x C-f and typing a filename, just as if you were opening an existing file. Emacs will notice that the file you typed doesn't exist, and will create a new buffer and report “(New file)” in the minibuffer.

When you type C-x C-f and then enter a directory name instead of a filename, Emacs will create a new buffer in which you will find a list of all the files in that directory. You can move the cursor to the file that you are looking for and type , and Emacs will open it. (There are in fact a lot more actions you can perform here, such as deleting, renaming and moving files, etc. Emacs is now in dired-mode, which is basically a simple file manager.)

When you have typed C-x C-f and suddenly change your mind, you can type C-g to cancel the action. C-g works almost everywhere where you want to cancel an action or command that you've started but don't want to finish.