Thursday, September 10, 2009

The vim Editor

If you’re working in Command Line mode, you may want to become familiar with at least one text editor that operates directly within the command line environment. The vi editor is the original text editor used on UNIX systems. It uses the Console Graphics mode to emulate a text editor window, allowing you to see the lines of your text file, move around within the file, and insert, edit, and replace text. Although vim may possibly be the most complicated editor in the world (at least in the opinion of those who hate it), it provides many features that have made it a staple for UNIX administrators for decades. When the GNU Project ported the vi editor to the open-source world, they chose to make some improvements to it. Because it no longer resembled the original vi editor, they renamed it vim (which stands for vi improved).

Ubuntu installs vim by default. It also creates an alias program named vi to point to vim so that you can use either command, vi or vim, to start the vim editor from the command line prompt.

The Basics of vim
The vim editor works with data in a memory buffer. To start the vim editor, just type the vim command (or the vi alias) and the name of the file you want to edit:

$ vim mytext.txt

If you start vim without a filename, or if the file doesn’t exist, vim opens a new buffer area for editing. If you specify an existing file on the command line, vim will read the entire contents of the file into a buffer area, and makes it ready for editing.

The vim editor operates in Full-Screen mode, using the entire console window for the editor area. The initial vim editor window shows the contents of the file (if any), along with a message line at the bottom of the window. If the file contents don’t take up the entire screen, vim places a tilde on lines that are not part of the file.

The message line at the bottom provides information about the edited file, depending on the status of the file, and the default settings in your vim installation. If the file is new, the message [New File] appears.
The vim editor has two modes of operation:
• Normal mode
• Insert mode

When you first open a file (or start a new file) for editing, the vim editor enters Normal mode. In Normal mode the vim editor interprets keystrokes as commands.

In Insert mode, vim inserts every key you type at the current cursor location in the buffer. To enter Insert mode, press the i key. To get out of Insert mode and return to Normal mode, press the Esc key on the keyboard.

In Normal mode you can move the cursor around the text area using the arrow keys (as long as your terminal type is detected properly by vim). If you happen to be on a flaky terminal connection that doesn’t have the arrow keys defined, hope is not lost. The vim commands include keyboard commands for moving the cursor:
• h to move left one character
• j to move down one line (the next line in the text)
• k to move up one line (the previous line in the text)
• l to move right one character


Moving around within large text files line by line can get tedious. Fortunately, vim provides a few commands to help speed things along:
• Page Down (or Ctrl–f) to move forward one screen of data
• Page Up (or Ctrl–b) to move backward one screen of data
• G to move to the last line in the buffer
• num G to move to line number num in the buffer
• gg to move to the first line in the buffer

The vim editor has a special feature within Normal mode called Command Line mode. Command Line mode provides an interactive command line where you can enter additional commands to control the actions in vim. To get to Command Line mode, hit the colon character from Normal mode. The cursor moves to the message line and a colon appears, waiting for you to enter a command.

Within the Command Line mode are several commands for saving the buffer to the file
and for exiting vim:
• q to quit if no changes have been made to the buffer data
• q! to quit and discard any changes made to the buffer data.
• w filename to save the file under a different filename
• wq to save the buffer data to the file and quit

After seeing just a few basic vim commands you might understand why some people absolutely hate the vim editor. To be able to use vim to its fullest, you must know plenty of obscure commands. However, once you get a few of the basic vim commands down, you can quickly edit files directly from the command line, no matter what type of environment you’re in.

Source of Information : Wiley Ubuntu Linux Secrets

No comments: