LPI Linux Certification/Perform Basic File Editing Operations Using Vi
(LPIC-1 Version 5.0)
Candidates should be able to edit text files using vi. This objective includes vi navigation, vi modes, inserting, editing, deleting, copying and finding text. It also includes awareness of other common editors and setting the default editor.
Key Knowledge Areas:
- Navigate a document using vi.
- Understand and use vi modes.
- Insert, edit, delete, copy and find text in vi.
- Awareness of Emacs, nano and vim.
- Configure the standard editor.
The following is a partial list of the used files, terms and utilities:
- /, ?
- i, o, a
- d, p, y, dd, yy
- ZZ, :w!, :q!
When using X-Windows, you can use mouse oriented editors such as xedit. In a cross-development environment, users use their favorite editor. On a non-windowing system, you only need a keyboard editor such as vi. The vi editor on Linux is the same as on any Unix systems. vi has two modes:
- Command mode: Anything you type will be interpreted as a command
- Input mode: Anything you type will be inserted into the file
To transition from Command mode to Input mode, use the i, I, a, A, o, and O keys. To transition from Input Mode to Command Mode, use the ESC key.
The default starting mode is the Command mode.
The file configuration .exrc can be created in your home directory to set up some vi behaviors.
set ignorecase # vi will not be case-sensitive set tabs=3 # each tab will be three spaces long set ai # auto indent set nu # show line numbers
To perform basic file editing using vi, use the following keys:
- Move cursor
- l one space right
- h one space left
- j one line down
- k one line up
- $ end of line
- ^ start of line
- w next word
- e end of word
- Enter Input Mode
- i before cursor
- I at start of line
- a after cursor
- A at end of line
- o open line below
- O open line above
- dw delete word
- dd delete line
- D delete to end of line
- x delete char at cursor
- Use vi from any directory to begin editing an empty buffer.
- Enter a few lines of text into this buffer.
- Save the contents of the buffer to the directory.
- Open the file again with vi.
- Create a new line beneath what you typed earlier. (Without using i from command mode.)
- Exit vi without saving these changes.