HOME


DSP         Embedded Systems         GNU Tutorials         SW Development        


GDB Tutorial
GNU Make Tutorial
Library with GCC
Vi Tutorial

GDB Tutorial         GNU Make Tutorial         Library with GCC         Vi Tutorial    

Introduction

        VI is a text editor available under Linux Environment. If you are new to Linux and are wondering what is a text editor, you can draw analogy from notepad under Window environment. VI has two modes of operation - text insert mode and command mode. At any given point of time you are in one of these two modes. While in insert mode, any key pressed will enter text to the current file. While in command mode, any key pressed (or a key combination), will issue a command (and appropriate action will be taken). There are some other variant of VI, e.g. VIM (modified VI) and GVIM (graphic VIM). But, VI has the most basic features and it is widely used. Discussion under this tutorial focuses on VI.

Firing up VI

        The very first step is to fire up the VI. There are two options available - Open an existing file, Open a new file. To open a new file, issue the command "vi FILENAME" from the linux command prompt". To open an existing file issue the command "vi FILENAME" from the linux command prompt. In case the given file does not exist in the current folder, you need to give the complete path of the file under "FILENAME" or the editor will assume that you are trying to open a new file in current directory. In case you forget to enter the filename, and just issue "vi" command - do not worry, this will take you to the vi manual.

Two modes of VI

        As we discussed before there are two modes of vi - command mode and insert mode (some people also call it as text mode). It is very easy to figure out, which mode you are in. While in insert mode, you can see "insert" status on left-bottom corner of VI. If you do not see this status message, then you are in command mode. When you just fire up the VI you are in command mode. Transition from one mode to other mode is easy. In order to enter the insert mode (from command mode), press the key "i" (to make it easy to remeber you can assume that "i" stands for "insert"). This will take you to insert mode (you can see the "insert" status on left-bottom of your vi screen now). To go back to the command mode, just enter the "ESC" (escape) key, and you will reach back to the command mode. If you are in insert mode, and you press "i" again, it will enter the text "i" to current file. If you are in command mode and press "ESC" again, nothing will happen, you will just hear a beep sound (as a joke, some people call the command mode as beep mode). We just mentioned that the key "i" can be used to make a transition from command mode to insert mode. There are two more keys which can do the same job - "a" and "o". The only difference is the relative position of cursor after entering the insert mode. Command "i" keeps the cursor to the same character, whereas command "a" takes the cursor to the next character (in addition to invoking the insert mode). Command "o" inserts a blank line, next to the current line, and take the cursor to the begining of the new line. Just give it a shot and figure out the difference yourself.

Moving around the file

        Once you fire up the vi, moving across the file is very easy. You can use the "arrow keys" to move left-right, up-down. If you key-board does not provide the arrow keys, you can use (only in command mode) "h, j, k and l" keys (it does the same what arrow keys do - left, down, up, right). While in command mode, "CTRL-f" takes you one page forward and "CTRL-b" takes you one page back. While in command mode, you can use keys "w" and "b" to move one word forward and one word backwards. "$" can be used to move the cursor to the end of the current line, and "0" will move the cursor to the start of current line. While in commad mode, pressing "G" (followed by return key) takes you to the end of file". "N G" (where N is a valid integer) takes you to the line number "N" in current file.

Quitting VI

        You can quit vi (you need to be in command mode for this) by issuing the command ":q" (it means press :q followed by return key - similar logic will apply to any command which we talk about in rest of the document). In case you have made some changes to the file (either deleted or added some text to the file), ":q" command alone will not work. You have two options. One option is to save the changes and quit - use "wq" (write and quit" command) for this. Other option is to quit VI without saving the changes - use ":q!" (quit without save" command for this. It is also important that while you are editing the file, you keep saving the changes in between (this will make sure that you do not loose any work in case of system failure - as unsaved changes might be lost). You can save the current contents of a file using the ":w" (write) command.

Deleting Text"

        While in command mode, you can use the key "x" to delete the current character (character where the cursor is). "Backspace" key should work for deleting the character just before the cursor. "dw" and "db" can be used to delete one word at a time. "dw" deletes the current word (pointed by cursor) and "db" deletes the word just before the current word. Pressing "dd" deletes the (entire) current line. You can also use "N dw" "N db" and "N dd" (you should not enter space between N and d - the space here is just for the sake of clarity), to repeat the given command N (where N is a valid integer) times.

Pasting a section of text

        If you are familiar with copy-paste, cut-paste operations under windows, you can get the VI to do similar things for you. In case you have never worked on windows, you need not worry, just read the next few lines. When you delete a text (for example using "N dd" or "dd") the deleted text gets copied to a buffer. The same text can be pasted to anywhere in the current file, by moving the cursor to desired position and then pressing "p" (this should be done under command mode - I assume that by now you have understood that any commands to editor should be issued only in command mode, so I will not be mentioning it explicitly any more in rest of the document). The command "N dd" deletes N number of lines, which can be pasted to another section using "p" command. However, some times you might just want to copy (without deleting) a given number of lines. This can be done using "yy" (called yanking) command. As with the other commands, "N yy" also works fine.

Copying a section of text

        In the previous section we discussed about yanking a given number of lines. Some times you may want to yank a given section of text (starting from a given line till end of another line). This can be done using markers. To set the marker on a given section of text, place the cursor at begining of the section and press "ma" (this marks begining of the text). Now move the cursor to end of section and press "zy'a". This will yank the text from given cursor location to the earlier marked location to buffer "z". Instead of "z" you can use any buffer name (a-z or A-Z). Now move the cursor to a position where you want to insert the text and use "zp command (quote is needed here). This pastes the contents of register "z". If you had used some other buffer name (instead of "z"), you will need the same buffer name while pasting the text. Buffers can exist in parallel (e.g. copying data to buffer "x" will not destroy the data copied to buffer "y").

Opening another file

        If you want to open another file in VI (when one file is already open), one way to do that will be to first quit vi, and then issue a command to open the second file. However if you use ":e filename" command, you can avoid the need of quitting vi. This will directly open the desired file.

Managing multiple windows

        Some times you might want to edit multiple files in parallel. To achieve this you can issue the command "Ctrl-W n" (the space is not needed). "Ctrl-w" signal that you are issuing a command related to "window" and "n" signals that you want a new window. Now in the new window, you can open any file using ":e filename" command (this command was discussed previously).

Searching a given pattern

        To search (forward) for a given pattern in a file, use command "/pattern". To find the next ocurrence of same pattern press "n" (note that "N" will give you take you to the occurence of patter in reverse direction). If you want to search in backward direction, use "?pattern". In both the above cases, when you reach the last occurence of pattern, the search rolls back to begining (or end in case of backward search) of file. If you want to disable the rolling back of the search, use the command ":set nowrapscan". To enable the option again, use command ":set wrapscan".

Replacing a given pattern

        In the previous section we discussed how we can search for a given pattern under vi. Options are also available which combine search and replace operation, i.e. search for a pattern and replace it with a new pattern. The "<start>,<finish>s/<find?/<replace>/g" command can do this job. "start" and "finish" mark the section of the file in which search/replace has to be carried out. These values can be integers (mentioning line numbers). For example "1,15s/old/OLD/g" will replace the occurence of "old" with "OLD" in the lines 1-15 of the file. if you want to search in entire file then use the option "%". e.g. "%s/old/OLD/g". If you use "gc" modifier (instead of "g"), then vi will ask (confirm with) you (every time a pattern is find) before replacing it.

Some other useful tips

        While using VI to edit C/C++ files, you might want to use auto indentation. For this you can issue the command ":set indent". To turn off the indentation, issue the command ":set noindent". Also, to highlight the keywords (of C/C++ language), issue command ":syntax on" (":syntax off" can be used to turn it off). Some times you might want to issue some linux commands without quitting Vi (e.g. you might want to issue "ls" to view the contents of current directory). This is easy. You can issue ":! LinuxCommand". Where LinuxCommand is any valid shell command (e.g. ":! ls").

Summary

        By now you have learnt the most basic commands of VI. These are all what you need during day-to-day use of VI. However, vi has many more features, but you may not need them very often.



















HOME

Copyright : Kunal Singh

Content of this site shall not be reused without my written permission

This page is XHTML Certified