Contents
- Introduction
- 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
- Summary
Introduction
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 vi
.
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.
vi
is 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 thatvi
be present.vi
is lightweight and fast. For many tasks, it’s easier to bring upvi
than it is to find the graphical text editor in the menus and wait for its multiple megabytes to load. In addition,vi
is designed for typing speed. As we will see, a skilledvi
user 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 vi
.
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 vim
.
Starting And Stopping vi
To start 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):
:q
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.
:q!
Tip: If you get “lost” in vi
, try pressing the esc key twice to find your way again.
Editing Modes
esc
- Command modei
- Insert modev
- Visual modeV
- Line modectrl+v
- Block mode
Basic Commands
:w
- Save:q
- Quit:q!
- Quit without saving:sav filename
- Save file as filename:e filename
- Open filename for edition.
- Repeats the last change made in normal mode
Basic Editing
o
- The line below the current lineO
- The line above the current linex
- Delete a character3x
- The current character and the next two charactersdd
- Delete the current line5dd
- The current line and the next four linesdw
- Delete a single word:%d
- Delete all line:4,7d
- Range delete from line 4 to line 7D
- Delete everything after the cursorp
- Pasteyy
- Copy the current line5yy
- Copy the current line and the next four linesyG
- Copy from the current line to the end of the filey20G
- Copy from the current line to the twentieth line of the fileu
- Undoctrl+r
- Redo
Moving In The File
l
or right arrowh
or left arrowj
or down arrowk
or up arrow0
- 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.ctrl-F
orpage down
- Down one page.ctrl-B
orpage 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 fileL
- Move the cursor to the bottom of the screen&
- Move cursor to matching parenthesis[[
- Jump to function startt+w<any word>
- Move the cursor to the w wordf+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 clipboardp
- Paste clipboard contentsdd
- Cut current lineyy
- Copy current liney$
- Copy to end of lineD
- 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
File Explorer
: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 tabgt
- Switch to the next tabgT
- Switch to the prev tab:tabfirst
- Show first tab:tablast
- Show last tab:tabs
- List all tabs
Window Spliting
:e filename
- Edit filename in current window:split filename
- Split the window and open filename:sp
- Split horizontally:vs
- Split verticallyctrl-w up arrow
- Puts cursor in top windowctrl-w ctrl-w
- Puts cursor in next window10 + ctrl + w + +/-
- Increase/decrease height10 + ctrl + w + >/<
- Increase/decrease widthctrl-w_
- Maximize current window verticallyctrl-w|
- Maximize current window horizontallyctrl-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)
Summary
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.