Table of Contents
vi is a screen-oriented text editor originally created for the Unix operating system.
The original code for vi was written by Bill Joy in 1976, as the visual mode for a line editor called ex that Joy had written with Chuck Haley.Bill Joy’s ex 1.1 was released as part of the first BSD Unix release in March, 1978. It was not until version 2.0 of ex, released as part of Second Berkeley Software Distribution in May, 1979 that the editor was installed under the name vi (which took users straight into ex’s visual mode), and the name by which it is known today.
Trivial : A 2009 survey of Linux Journal readers found that vi was the most widely used text editor among respondents, beating gedit, the second most widely used editor by nearly a factor of two (36% to 19%).
(loosely based on this VI tutorial)
- Start using VI : To start vi just type vi at the operating system prompt. You will see a screen with a column of tildes (~) down the left side of the screen. This signifies an empty workspace. To edit a file, just include the filename after it, e.g. vi filename1. You will see the text of the file you included. Vi is now in command mode. The most basic command to enter insert mode is i which lets you insert text to the left of the cursor.
- Insertion mode : Let’s begin with a brief description of the insert mode because it is very straight forward. In insert mode the characters you type are inserted into your document. You can use the backspace key to delete any typing mistakes you have made on the current input line. The escape key (<esc>) takes you out of insert mode and back to the command mode. If you are ever in doubt about what mode you are in, just press <esc> a few times until vi starts complaining. You will then know that you are in the command mode.
- Command mode : This is where you do everything that isn’t done in insert mode. In command mode the same keys that caused letters to appear on your screen in insert mode now represent totally different functions. Rather than go into a detailed discussion of the 100 or so commands, this section contains a list of the more popular commands. A more comprehensive list of vi commands may be found in the Vi reference section.
- Visual mode : The visual mode can be handy from times to times. To enter the visual mode just press the “v” key.
2) Commands examples
The following examples are to be used in command mode.
2.1 Options while opening a file
- Open <file> directly at a line identified by its number
Note that if you omit the <line_number> the file will be open straight to its last line.
vi +<line_number> <file>
- Open <file> directly at a line identified by a <pattern>
vi +/<pattern> <file>
- Print the current file name and information
[CTRL] + [g]
- General syntax
- Substitute a string matching
<pattern2>, globally (every occurence within the current file)
or using the
- Not sure about the changes, want to confirm before any changes ? use the c option
the c option also works with the
2.3 View or filter
With those tips just type “:q” in command mode to return to the original file (with no filter)
- View “only uncommented” lines
or the equivalent to g!
- View only “not empty” lines
or with the same as above (“v” instead of “g!”)
- General syntax
- Using a <pattern>
- Using a line number
- Using a range(from line n°5 to 9 here) of <line_number>
- Delete every commented line (when lines are commented using the dash character “#”)
- Delete every empty lines
Note : To get only the “uncommented” and “not empty” lines, just combine the above two commands. Also you may want to put the result of these two commands in a new file, the w <new_file_name> may do this:
- Search for a string matching <pattern> within the actual file
- Search for the exact word currently under the prompt (by exact i mean “whole word“)
Tabs navigation may dramaticaly improve yiour vi habits, giveit a try and you will love it for sure! you xill find in this section basics and less basics commands to use tabs navigation under vi/vim.
- Open <file_1> and <file_2>in vi using tab mode
vi -p <file_1> <file_2>
- Close a tab
:tabc # c for close
Note : you can also close a tab by exiting the edited file using
if required or
- Navigate through opened tabs
:tabn # go to next tab (n for next) :tabp # go to previous tab (p for previous) :tabn <N> # jump to next tab number <N> :tabr # jump to firdt tab (r for rewind) :tabl # jump to last tab (l for last)
- Show a list of tabs (with the corresponding file)
To close a tab, use :tabc. To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast). Finally, :tabs will give you a list of open tabs.
When it comes to coding, the indentation is nothing but a life or death question to me, and vi can be very powerful in this domain : But i am still learning, i will share here every new tips i find useful.
- In normal mode :
>>to indent the current line or
- you can add an “address” in front of the
>>operator, to apply the move to certain lines :
5>>would indent 5 lines at once
- don’t forget the
.which repeat the last command, using this you can indent more :
5>>..would indent 5 lines to 3 levels of indentation
- In insertion mode
- [Ctrl] + T indent
- [Ctrl] + D unindent
3) VI moves tips
Please note that all the following command are to be used in command mode.
3.1 Move to first line of file
There are quite some different way to directly go to the first line of a file within vi, here a list of my favourite :
3.2 Move to last line of file
Same as above but for the last line, the best one is :
More “vi” posts
- About ANSI control codes
- How-to : NTOP install on RHel/CentOS 6