Archive for the 'Programming' Category

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

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

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

Introducing DZone 2.0

Tuesday, April 3rd, 2007

After months of development we are proud to introduce DZone 2.0. There are many new features in this our first large scale site update. Among the first new features you might notice is the ability to vote a story up or down. What might not be as obvious about this feature is that you can actually change your vote or remove it all together. We’ve altered the layout as well, the default layout is what we have dubbed our “Reader” theme. Try scrolling to the bottom of the article list. You should notice that it will continue to add items to the end of the list, thus eliminating the need to navigate multiple pages of articles.

We’ve also added new ways for you to bookmark and share links. You can now save a link, share a link or email a link. These options are available on both the main listing and individual article summaries. Saved links are your own personal collection of links that you may wish to refer to in the future. Shared links are links that you may wish to share publicly with others, they are both viewable through your profile page as well as through a RSS feed. Emailing links provides a quick way to email a link to a friend or group of friends.

DZone now allows OpenID logins. Though users logged in under a OpenID will have certain restrictions, they will be able to interact with most of the site including voting and commenting.

A few more features quickly:

  1. New larger hi-res avatars up to 80×80 pixels may be uploaded and used now.
  2. Queue voting available in the mini view.
  3. Improved Spy feature

I’ll be covering all of these features in more detail this week on the official DZone blog. For now, again, welcome to DZone 2.0.

-3Monkeys

Popularity: 7% [?]

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