- Why We Should Learn vi
- A Little Background
- Starting And Stopping vi
- Editing Modes
- Basic Commands
- Basic Editing
- Moving In The File
- Cut, Copy, And Paste
- Search And Replace
- File Explorer
- Interact With Unix
- Tabs And Windows
- Window Spliting
Learning the Linux command line, like becoming an accomplished pianist, is not something that we pick up in an afternoon. It takes years of practice. In this chapter, we will introduce the
vi (pronounced "vee eye") text editor, one of the core programs in the Unix tradition.
vi is somewhat notorious for its difficult user interface, but when we see a master sit down at the keyboard and begin to "play," we will indeed be witness to some great art. We won’t become masters in this chapter, but when we are done, we will know how to play the equivalent of "Chopsticks" in
Why We Should Learn vi
In this modern age of graphical editors and easy-to-use text-based editors such as
nano, why should we learn
vi? There are three good reasons.
viis almost always available. If we have a system with no graphical interface, such as a remote server or a local system with a broken X configuration, this can be a lifesaver.
nano, while increasingly popular, is still not universal. POSIX, a standard for program compatibility on Unix systems, requires that
viis lightweight and fast. For many tasks, it’s easier to bring up
vithan it is to find the graphical text editor in the menus and wait for its multiple megabytes to load. In addition,
viis designed for typing speed. As we will see, a skilled
viuser never has to lift their fingers from the keyboard while editing.
- We don’t want other Linux and Unix users to think we are cowards.
Okay, maybe two good reasons.
A Little Background
The first version of
vi was written in 1976 by Bill Joy, a University of California at Berkeley student who later went on to cofound Sun Microsystems.
vi derives its name from the word "visual," because it was intended to allow editing on a video terminal with a moving cursor. Previous to visual editors, there were line editors that operated on a single line of text at a time. To specify a change, we tell a line editor to go to a particular line and describe what change to make, such as adding or deleting text. With the advent of video terminals (rather than printer-based terminals like teletypes), visual editing became possible.
vi actually incorporates a powerful line editor called ex, and we can use line editing commands while using
Most Linux distributions don’t include real
vi; rather, they ship with an enhanced replacement called
vim (which is short for "vi improved") written by Bram Moolenaar.
vim is a substantial improvement over traditional Unix
vi and is usually symbolically linked (or aliased) to the name
vi on Linux systems. In the discussions that follow, we will assume that we have a program called
vi that is really
Starting And Stopping vi
vi, we simply enter the following:
[user@linux ~]$ vi
To exit, we enter the following command (note that the colon character is part of the command):
The shell prompt should return. If, for some reason,
vi will not quit (usually because we made a change to a file that has not yet been saved), we can tell
vi that we really mean it by adding an exclamation point to the command.
Tip: If you get "lost" in
vi, try pressing the esc key twice to find your way again.
esc- Command mode
i- Insert mode
v- Visual mode
V- Line mode
ctrl+v- Block mode
:q!- Quit without saving
:sav filename- Save file as filename
:e filename- Open filename for edition
.- Repeats the last change made in normal mode
o- The line below the current line
O- The line above the current line
x- Delete a character
3x- The current character and the next two characters
dd- Delete the current line
5dd- The current line and the next four lines
dw- Delete a single word
:%d- Delete all line
:4,7d- Range delete from line 4 to line 7
D- Delete everything after the cursor
yy- Copy the current line
5yy- Copy the current line and the next four lines
yG- Copy from the current line to the end of the file
y20G- Copy from the current line to the twentieth line of the file
Moving In The File
lor right arrow
hor left arrow
jor down arrow
kor up arrow
0- To the beginning of the current line.
^- To the beginning of the current line.
$- To the end of the current line.
A- To the end of the current line.
w- To the beginning of the next word or punctuation character.
W- To the beginning of the next word, ignoring punctuation characters.
b- To the beginning of the previous word or punctuation charater.
B- To the beginning of the previous word, ignoring punctuation characters.
page down- Down one page.
page up- Up one page.
numberG- To line number. For example,1G moves to first line of the file.
G- To the last line of the file.
gg- Move the cursor to the begining of the file
L- Move the cursor to the bottom of the screen
&- Move cursor to matching parenthesis
[[- Jump to function start
t+w<any word>- Move the cursor to the w word
f+w<any word>- Move the cursor to the w word
~- Changes a letter to uppercase or lowercase
Cut, Copy, And Paste
y- Copy the selected text to clipboard
p- Paste clipboard contents
dd- Cut current line
yy- Copy current line
y$- Copy to end of line
D- Cut to end of line
Search And Replace
/word- Search word from top to bottom
/\cstring- Search STRING or string, case insensitive
/jo[ha]n- Search john or joan
:bufdo /searchstr/- Search in all open files
:noh- No highlighting
:%s/old/new/g- Replace all occurences of old by new in file
:%s/onward/forward/gi- Replace onward by forward, case unsensitive
:e .- Open integrated file explorer
:Sex- Split window and open integrated file explorer
:Sex!- Same as :Sex but split window vertically
:browse e- Graphical file explorer
:ls- List buffers
:cd ..- Move to parent directory
:args- List files
::args \*.php- Open file list
Interact With Unix
:ter- Open a shell
:!pwd- Execute the pwd Unix command, then returns to Vi
:sh- Temporary returns to Unix
$ exit- Retourns to Vi
Tabs And Windows
:tabnew- Creates a new tab
:tab ball- show each buffer in a tab
gt- Switch to the next tab
gT- Switch to the prev tab
:tabfirst- Show first tab
:tablast- Show last tab
:tabs- List all tabs
:e filename- Edit filename in current window
:split filename- Split the window and open filename
:sp- Split horizontally
:vs- Split vertically
ctrl-w up arrow- Puts cursor in top window
ctrl-w ctrl-w- Puts cursor in next window
10 + ctrl + w + +/-- Increase/decrease height
10 + ctrl + w + >/<- Increase/decrease width
ctrl-w_- Maximize current window vertically
ctrl-w|- Maximize current window horizontally
ctrl-w=- Gives the same size to all windows
:hide- Close current window
:nly- Close all windows, excepted current
Go To File
gf - open in the same window (open the file name under the cursor) <ctrl-w>f - open in a new window (open the file name under the cursor) <ctrl-w>gf - open in a new tab (open the file name under the cursor)
With this basic set of skills, we can now perform most of the text editing needed to maintain a typical Linux system. Learning to use
vim on a regular basis will pay off in the long run. Since vi-style editors are so deeply embedded in Unix culture, we will see many other programs that have been influenced by its design.
less is a good example of this influence.