Text Editor vi Linux  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Linux
Written by Phil Harrison   
Wednesday, 13 December 2006

{mos_sb_discuss:41}

One advantage of the vi/vim text editor is that it is installed both in the rescue and main SUSE installed system by default. The vim editor is relatively lightweight on system resources, but extremely powerful at the same time. Incorporating syntax highlighting and regular expression handling, vim is an all-around nice guy of the text-editing world.



By default, the vim editor is installed (vi improved). It adds extra functionality to the traditional vi text editor.

One of the first things that may stump you when you first start using vi is the fact that you cannot enter any text when you just type vi at the command line. This is one of the reasons that a lot of people do not like vi and move to emacs.

However, before you move on, let us explain what’s happening: vi/vim uses a command mode and a text mode. In command mode, you can manipulate the text with commands, save and quit files, and open files without “entering” text into your document. To actually edit text with the traditional methods (insert, delete, and so on), you need to move out of command mode.

This may seem quite alien at first, but we hope that with some examples you will see that it is a quite powerful way to do things, and for people who work more quickly on the command line, it can dramatically speed up your text-editing needs.

As soon as vim has loaded, it is automatically in command mode. To move into insert mode, press the
i key. If you want to insert a new line at the current position, use the o key. This inserts a new line and puts you in insert mode.

In the bottom-left corner of the screen, you will see the word INSERT. This signifies you are in insert mode. You can now type text until your heart is content. One of the great things about vi is that it can be used pretty much anywhere. If you are on an old terminal, and you have access to alphanumeric characters only, you can control the cursor with the k, h, l, and j  keys (up, left, right, and down, respectively) to navigate the screen (as opposed to the cursor key we have come to rely on so much).

In most cases, the Backspace key will enable you to delete characters. If your terminal (an xterm, telnet session, or ssh session) is not capable of using the common keys you are accustomed to, you will have to use other methods to edit your text.

It may seem backward to not use the backspace and cursor keys to edit your text, but vim is very good at adapting (or should we say, being adapted) to any situation you throw at it. This is an extremely powerful feature that will help you if you are in a tight spot with connectivity issues.
 

Using Command Mode

We briefly touched on the INSERT mode of vim, which is where most things happen because it’s where the addition of text occurs. After all, that is why you use a text editor.

However, apart from the traditional editing features, we want to talk about the command mode editing features of vim as well. To enter the command line, press the Escape key. The INSERT keyword in the bottom-left corner of the screen disappears.

You are now in the realm of the vi command mode. You can use the cursors (or the k, h, l, and j keys) to move around the text, but you cannot insert anything.

The next sections discuss some basic keys that you can use in command mode that prove very useful.
 

Moving Around the Text

To speed up your text editing, you can use shortcuts to move quickly to blocks of text, the start and end of a file, and to the start and end of a line of text.
 

Moving to the Start and End of a File

To move to the end of a file (and this applies to quite a few text-based applications in Linux such as man and less), press Shift+g. To move to the start of the file, pre ssg+g. You can also go to a specific line in the file by entering the number of the line that you want, followed by g+g.

For example, 15g+g would take you to line 15 of the file that you are editing.

Moving Around a Line of Text

To move around a line of text, you can use w to move to the next word, $ to move to the end of the line, and Shift+a to move the cursor to the end of the line and enter append mode.

It is very useful to combine the end-of-line operation with the append operation to add text to the end of the line.

Keep an eye on the location coordinates at the bottom-right corner of the screen to see how the Shift+g and Shift+a operations affect the cursor.

To move to the start of the current line, use the zero (0) key or the Home key. 

Deleting Text

To remove a character from a string of text, press the x key. A comparison of Figures 11-4 and 11-5 shows you the results.

You can see in the figures that the s in insert was removed. The x key in command mode can be thought of as a replacement for the Backspace key. You will find after repeated use of vi that you will not use the Backspace key at all. We have even used the x command in Word as we are in the mindset that we are editing text and we should use the x key to remove text.
 

Deleting More Than One Character at a Time

Often you want to remove whole lines of text, and vi  enables you to do this very quickly with the command.

The d command can be used to remove a whole line, a word, part of a word, multiple lines, and multiple words.
To remove a word of text (text surrounded by a space), move the cursor to the start of the word and press d+w sequentially. If you wanted to remove the part of a word, position the cursor at the character you want to remove to the end of the word and use the d+w command.

To remove a full line of text, press d+d sequentially. The double d removes the whole line of text, until it finds the end of the line. It may be that you cannot see the entire text on the line if it is longer than your terminal display, so be careful when you remove a line.

To remove all text from the cursor position to the end of the current line, press d and then $ sequentially.

Undoing and Redoing

The vim editor also features an undo command that proves very helpful. If you have made a mistake (for example, removing a line you didn’t mean to), pressing u while in command mode will undo the last operation you made. Pressing u again will undo the previous operation before this and so on. To redo an operation you have undone, press the Ctrl+r key (redo).

Removing Multiple Times

To remove multiple times, you can specify a number to work with the previous commands. For example, to remove five lines of text, press 5+d+d sequentially.
The operation 5+d+d has been used to remove Line 3 through Line 7.
You can use this operation to remove characters (number+x), lines (number+d+d), and also to remove characters to the end of a line (it will remove all text from the subsequent lines).
 

Copying and Pasting

Entering copious amounts of text into a file is never a fun thing, and the copy-andpaste idea has helped to speed up repetitive text entry. In most graphical user interface (GUI) applications, a simple right-click for the text menu enables you to copy and paste text. When you are working on the command line, this is not possible, and you have to do it a little bit differently.

In vim, you call a copy a yank (as in, you are yanking the text). With this in mind, you may be able to guess what you use to yank the text, a y+y combination. To copy a line of text, place your cursor on the line you want to copy and press y+y.

This copies the text into the buffer. To paste the line to another place in the file, press the p key (for paste).

If you wanted to paste multiple copies of the line, you can use the multiplier. For example, to paste a line five times, use 5+p. 

Inserting and Saving Files

If you are editing a file and you realize that you want to pull in text from another file, you can use the :r command in vi command mode.

For example, if you want to read the contents of the file /tmp/myfile into the current document at the current cursor position, you enter command mode with the Escape key and type :r /tmp/myfile.

To save a file, you use the :w command.

To save a file you just edited to /home/ justin/mynewfile, you enter :w /home/justin/mynewfile.

Searching and Replacing

To search for a string in your text, you can use the forward slash (/) and question mark keys (?).
To search from your current position forward in the file, use the / key.

For example to search for the word apples from the current cursor position to the end of the file, enter /apples and press Enter in command mode.

To search backward, to the start of the file, use the ? key. To search for apples from the current cursor position to the start of the file, enter ?apples and press Enter in command mode.

If you are looking for more than one occurrence of the word apples in the text, press the n key to move to the next occurrence.

Replacing text globally in a file is quite easy to do and is very powerful, if you know what you are doing. To replace text in the whole document, you need to use the substitution command, :s.
For example, to replace the word “apples” with “pears” in the current document, enter :%s/apples/pears/g.

The :%s command is quite powerful in its ability to search and replace. In the example command, we used % to tell vim to check every line of the document for the occurrence of “apples”. Adding the g tells it to replace all occurrences of “apples” on a line with “pears”.

If you are worried that you could be replacing text you do not want to replace, you can add the c command onto the g to get vim to ask for confirmation of a replace.


Using the vim Initialization File

If you want to customize how vim works, you can add startup commands to the file .vimrc in your home directory. This file is used to set the profile for how vim works for you and is a very useful file.

One popular feature of vim is its syntax highlighting. If you are editing C, or maybe Perl, vim can colorize your text so it is easier to read. Open the .vimrc file (it may not exist, which means you’ll have to create it) and add the following to the file:

            syntax on

It is usually nice to be able to use the Backspace key to delete characters for us folks who like to be able to edit interactively.

            set backspace=2

This tells vim that when it is in insert mode, the Backspace key can be used to delete text as you can in Windows Notepad, for example.

And finally for programmers out there, it is useful to indent your code when typing so that you can structure your code; vim can be told that it should remember the current place you are indented to by setting the autoindent parameter in your startup file:

            set autoindent

Now, when you press Enter for a new line, vim returns to the column you are indented to (using the Tab key).

Exiting vim

To exit vim, you need to use the :q command. This will quit the current session as long as you have saved your work (that is, all text buffers are written to disk).

If you want to quit and save the current file to disk, use :wq. This works only if you have assigned a filename to the file you are working with. If you have not, you will see an error message. To remedy this, you can pass the name of the file you want to save with :wq filename. 

To exit vim without saving the file, you can use :q!. This will not ask for confirmation and will exit vim immediately. Use with caution.

User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Friday, 20 July 2007 )
 
< Prev   Next >