Archive for the 'Software' 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: 14% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • 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: 25% [?]

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

OpenOffice Calc Easter Egg

Thursday, July 5th, 2007

Put =GAME("StarWars") into any cell and press ENTER. This starts a playable version of Space Invaders. Although if you try to play it again, the message “oh no, not again” appears. To play again, you must fully close OpenOffice.org.

More OpenOffice Calc Easter Eggs

Until next time…

-3Monkeys.

Popularity: 9% [?]

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