103.8 Perform basic file editing operations using vi
Candidates should be able to edit text files using vi. This objective includes vi navigation, basic vi modes, inserting, editing, deleting, copying and finding text.
- Navigate a document using vi.
- Use basic vi modes.
- Insert, edit, delete, copy and find text.
- /, ?
- i, o, a
- c, d, p, y, dd, yy
- ZZ, :w!, :q!, :e!
vi is a great tool! The best editor ever (some say after Emacs) and it is installed on all linux systems. Some say it is difficult to use and abnormal and some say it is the most natural editor there can be. Lets see.
vi can be used with simplest keyboards and over network on ssh terminals
In many systems,
vi command is a link / alias to
vim and there are many versions of vi. Check with
$ vi --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct 20 2014 16:08:47)
Included patches: 1-273
Modified by [email protected]
Compiled by buildd@
Small version without GUI. Features included (+) or not (-):
+acl -farsi -mouse_sgr -tag_old_static
-arabic -file_in_path -mouse_sysmouse -tag_any_white
-autocmd -find_in_path -mouse_urxvt -tcl
-balloon_eval -float -mouse_xterm +terminfo
To edit a file with vi, just give the file name to it:
$ vi file.txt
vi has 2 different modes:
- Command mode is where you go around the file, search, delete text, copy paste, replace, ... and give other commands to the vi. Some commands start with a
:and some are only a keypress.
- Insert mode is where what you type, goes into the file at the cursors position.
If you want to go to the Command mode from the Insert mode, press ESC key. There are several ways to go to the Insert mode from Command mode (including the 'i' key).
Moving the cursor
If you need to move around, use these keys:
|One character to the left (only current line)
|One line down
|One line up
|One character to the right (only current line)
|Next word on the current line
|Next end of word on the current line
|Previous beginning of the word on the current line
|Scroll forward one page
|Scroll backward one page
you can type a number before most commands and that command will be repeated that many times (i.e.
6hwill go 6 characters to the left)
|With no number, will jump to the end & 10G will jump to line 10
|5H will go to the 5th line from the top of the screen
|3L will move the cursor to the 3rd line to the last line of the screen
These command during the command mode will help you enter, edit, replace, .. test:
|Enter the insert mode
|Enter the insert mode after the current position of the cursor
|replace only one character
|open a new line below the cursor and go to the insert mode
|open a new line above the cursor and go to the insert mode
|clear to a location and go to the insert mode the replace till there and then normal insert (
cw will overwrite the current word)
|delete. you can mix with w (
dw) to delete a word. Same as cw but dw does not to to the insert mode
|Delete the current line
|Delete character at the position of the cursor
|Paste the last deleted text after the cursor
|Paste the last deleted text before the cursor
|swaps the character at the cursor position with the one on its right
|Search forward (
/happiness will find the next happiness)
|repeat previous search. You can also use
? without any parameters)
Search wraps around to the top once the bottom of file is reached
It is always funny when you see someone entering to the vi and now knowing how to exit! Learn these and prevent the laughter:
|Quit editing without saving = runaway after any mistake
|Write the file (whether modified or not). Attempt to overwrite existing files or read-only or other unwritable files
|Write to a new name
|Exit and save the file if modified
|Reload the file from disk
|Run a shell command
Entering colon (
:) during command mode will move the cursor to the bottom of the screen and vi will wait for your commands. Press ESC to return back to the normal command mode.
The exclamation mark in most commands will say "I know what I'm doing" and will write on read-only files if you have access and will exit without asking
You can always ask for help with
:help subject. This way vi will open a help text which you can use / search just like any other text. Close it with