Archive for the 'Emacs' Category

3Monkeys on Emacs: Part 3 - Buffers and Windows

Friday, July 6th, 2007

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: 15% [?]

  • description
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google
  • Live
  • Propeller

3Monkeys on Emacs: Part 2 - Basic Navigation and Editing

Friday, July 6th, 2007

Previous in this series : Part 1 - Introduction

Cursor Movement

Unlike Vi, except when executing a command, whenever you type in Emacs, text is inserted or replaced at the current cursor location. Emacs has a variety of commands to move and position the cursor within the buffer. Most of these commands can be executed with single keystroke commands. As seen in the previous article, keystroke commands are executed by holding down the Control (Ctrl) or Meta (Esc) key and pressing another key. The notation for this is C-x or M-x where C- indicates the Control key, M- indicates the Meta key and x indicates any arbitrary key. For ease of reference, I have compiled the most common cursor movement commands below in table format.

Command Action
C-b Go back one character
C-f Go forward one character
M-b Go back one word
M-f Go forward on word
C-p Go to the previous line
C-n Go to the next line
C-a Go to beginning of line
C-e Go to end of line
M-a Go to beginning of sentence
M-e Go to end of sentenced
M-[ Go to beginning of paragraph
M-] Go to end of paragraph
M-< Go to beginning of buffer
M-> Go to end of buffer
C-v Go forward one screen
M-v Go back on screen
C-x [ Go back one page
C-x ] Go forward one page
M-m Go to first non-blank character

Most, if not all, current distributions of Emacs bind most of these commands to the keyboard controls keys such as left-arrow, right-arrow, page-up and page-down among other. Most people familiar with similar editing applications should have no difficulty in using certain navigation commands they are already familiar with. Mouse actions are similar to that of most other applications as well, with the exception of certain mouse button click sequences. It is best for the user to experiment with these alternate navigation methods to determine exactly how the behave on their system.

Other cursor movement commands can be executed by verbose commands the most useful of these commands is M-x goto-line, which prompts the user for a line number to move to within the file. If a number larger than the last line in the file is supplied, the cursor is move to the end of the buffer.

Deleting Text

There are several commands to delete text in Emacs. The basic deletion commands are C-d to delete the character at the cursor and del (rubout or backspace) to delete the character before the cursor. Again these are frequently mapped to the modern keyboard keys delete and backspace respectively. C-k (kill) deletes to the end of line, but does not delete the newline unless that is the only character to be deleted. To delete an entire non-blank line use the following keystrokes, C-a C-k C-k, which move to the beginning of the line (C-a), deletes up to the newline (first C-k), and finally deletes the newline (second C-k).

At times you may want to delete a region of text, other editor application may call regions selected text. To create a region you must first create a mark. Regions in Emacs are defined relative to a mark and the cursor. To create a mark use the keystroke command C-Space or C-@. Then to create a region simply use the cursor navigation commands to define your region. In some versions of Emacs the region may not be highlighted. If this is the case and you wish to identify the start and end of your region, you can use the command C-x C-x, which swaps the mark and the cursor. This may also sometimes be used as a method of moving around in the buffer. Once a region is defined you can kill (delete) it by issuing the C-w command. For example if you wanted to delete the current sentence, you could issue the following command sequence, M-a C-Space M-e C-w.

Undo

At times you will want to correct a mistake, or copy text to a location other than where you deleted it from. The commands C-x u and C-_ will issue a single undo command. By repeatedly issuing either command you may undo multiple operations including insertions and deletions among others. Some versions Emacs limit the number of undo operations allowed.

The Kill Ring

Each time you delete text, through any of the methods described above, the text is inserted into the kill ring. To retrieve the text from the kill ring, position you cursor where you wish the text to be inserted and issue the C-y (yank) command. Repeated use of C-y will insert the text more times. Alternatively you may wish to copy text into the kill ring without deleting it from the buffer; use M-w to achieve this function.

In summary, C-w, M-w and C-y are similar to Cut, Copy and Paste in other applications respectively, however there are some important differences that will be covered in later articles.

Next in this series: Part 3 - Buffers and Windows

Until next time.

-3Monkeys

Popularity: 24% [?]

  • description
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google
  • Live
  • Propeller

3Monkeys on Emacs: Part 1 - Introduction

Wednesday, July 4th, 2007

Introduction

I have been an Emacs user for the better part of 17 years or so. Even so, there are quite a few things I’ve learned and forgotten along the way and perhaps even more that I never learned. I am writing the series of articles to not only provide a tutorial for the beginning Emacs user, but to also dig in and uncover features that I haven’t used for some time and features I never knew about. For the time being, I will be discussing just Emacs and not XEmacs, if it becomes necessary or requested to cover XEmacs, I will consider it at that time.

Vi - Well any time you mention Emacs, there is inevitably a Vi/Vim/GVim user in the room, and just inevitably a Holy War between the two breaks out. I really do not want to start a Holy War here, therefore, I am inviting one or more Vi advocates to step up and be a guest author and contribute similar articles from a Vi perspective. If you are willing to do this please contact me.

Some Fundamentals

In any editor there are a few fundamental operations you must be able to perform, create a new file, edit an existing file, and save a file. Emacs provides several ways to do these, the most common being command keystrokes within Emacs itself, other methods are issuing the verbose command or in an XWindows session selecting from a menu.

  • Creating or Opening a File
    More often referred to as visiting a file or finding a file, can be accomplished by simply specifying one or more filenames on the command prompt when starting Emacs.
    For example:

    	% emacs foo.txt bar.csv

    This command will start emacs and open both foo.txt and bar.csv into editing buffers. If either file does not exist on disk, emacs will still create a new buffer for the specified file. It is important to realize the all editing in emacs is done in buffers and not on files directly. To have changes to a buffer reflected in the file on disk, you must save the buffer.

    Perhaps the most common way of visiting (opening) a file, is to use the keystroke command sequence C-x C-f. This is a two-key or extended command. The C- indicates that you must hold down the Control (Ctrl) key then press the following key, in the case of C-x that would be x. Many keystroke commands are of the extended command type and begin with C-x. When this command is executed you are prompted for a filename in the echo area, a small usually one line area at the bottom of the Emacs screen. Simply enter the files name and press return to visit the file and load a buffer with its contents. Emacs supports tab completion, so as you type the name of the file you can press tab to either complete unique portions of the filename, or if there are no unique portions Emacs will open a completions buffer listing the legal possibilities. Again if the file you specify does not exist a new buffer associated with that filename will be created.

    You can also choose to verbosely run the command. To run a verbose command, start by entering command mode by issuing the keystroke command M-x, where M-x indicates to press and hold down the Meta (Esc) key and then press x. This will allow you to execute Emacs commands directly from the echo area. The command for opening a file is find-file, simply type this in the echo area using tab completion if desired and press return. This will provide the same prompt in the echo area as the Cx C-f keystroke command described above.

    Finally, if your system supports it, you can select the visit file menu item from the Emacs menu. This operation is strongly tied to the type of system you happen to be running on so I will not cover menu items in detail.

  • Saving a File
    Saving a file is generally performed with the keystroke command, C-x C-s. This will save the current buffer to disk if the buffer was associated with a file to begin with. Certain buffers are not associated with on disk files however, if you save a buffer of this type, Emacs will prompt you for a file name in the echo area. Such buffers could be scratch or shell buffers. The verbose command for saving a buffer is save-buffer. If you wish to save the buffer to file of a different name, use the C-x C-w keystroke command. Emacs will prompt you for the new name in the echo area. The verbose command for saving a buffer to a new filename is write-file.

The next article in this series will cover navigating through a buffer.

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

Until next time…

-3Monkeys

Popularity: 15% [?]

  • description
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google
  • Live
  • Propeller