3Monkeys on Emacs: Part 3 - Buffers and Windows

Previous in this series : Part 2 - Basic Navigation and Editing

Buffers and Windows

All editing in Emacs occurs within one or more buffers. The key to that statement is “one or more“. It is possible, and usually likely, that multiple editing buffers will be available in a given emacs session. There are other types of buffers, such as the scratch, completion, and shell buffers which serve different purposes other than editing. Let us consider the case of % emacs foo.txt bar.csv, that was introduced in Part 1 of this series. When emacs is started two buffers are created, foo.txt and bar.csv, if those files exist on disk then the contents of those files are populated to their respective buffers. In most implementations of Emacs, the result will be an instance of Emacs with two windows, one for each buffer being displayed, as shown below.

Two windows, Two Buffers
We notice each window contains its own mode line however, there remains only one echo area.

Windows

Windows can be created, removed and navigated with a few simple commands. If you wish to remove the current window use the C-x 0 command. To remove all windows except the current one, use C-x 1. C-x o will shift the focus from window to window in the order they were created. Want to create a window? Use C-x 2 to split the current window horizontally, and C-x 3 to split it vertically. To shrink or enlarge the current window you can use the commands shrink-window, shrink-window-horizontally, enlarge-window, enlarge-window-horizontally. To enter a verbose command use M-x then type with possible tab-completion the command name. If you find yourself using commands frequently, such as these you can bind them to a keystroke command sequence. Binding keystrokes will be covered in a later article.

Buffers

It is not uncommon to have many editing buffers open in a single Emacs session. Consider if you were programming in C, you will likely want to edit both a .c and a .h file concurrently. I have dozens of buffer (files) open at any given time when working on anything but the simplest project. Using windows it is easy to display a class definition while coding its implementation. Switching buffers is a common task when editing in this type of environment.

To switch the current window to another buffer, there are two basic methods, the direct method and the select method. In the direct method use the C-x b command and type the name of the buffer in the echo area. By default the target buffer is the last visited buffer, press return to visit this buffer. Tab-completion, as in most input cases, is supported. If you tab-complete and the buffer name is incomplete a completion buffer is displayed to either help you narrow your choices or allow you to select from. The completion buffer is a subset of the select method. In the select method, use the command C-x C-b, this will open a buffer list buffer. You can navigate to the buffer list window using the C-x o command described above. Navigate to the buffer you wish to visit and press return. The buffer list shows additional information regarding the buffers, such as its modification status, size, mode and associated file.

Buffer List

Finally, you will want to remove buffers or kill them. Use the command C-x k, to kill a buffer. The default buffer is the currently selected buffer, otherwise you can specify another buffer in the echo area. When you kill a buffer associated with a file and it has been modified, Emacs will prompt you to save it or not.

Quiting Emacs

Well it is now the third article in this series, you maybe wondering how to quit emacs. The command C-x C-c will cause Emacs to terminate, it will inform you of any modified buffers and allow you to save them on your way out if you wish.

Next in this series: Part 4 - Search

Until next time.

-3Monkeys

Popularity: 20% [?]

  • description
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google
  • Live
  • Propeller
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 9 out of 10)
Loading ... Loading ...

One Response to “3Monkeys on Emacs: Part 3 - Buffers and Windows”

  1. austin Says:

    Thanks, this is really helpful. Also, to shrink and enlarge windows, C-x ^ enlarges vertically while C-x { and C-x } shrink and enlarge horizontally.

Leave a Reply