Vi and Vim
vi is the first full-screen text editor written for Unix.
vim, or vi improved, is an updated version- is upward compatible with
vi - all the following
viinfo also applies tovim
- is upward compatible with
- excels at editing program files
Modes
- 3 modes
- Command Mode
- accepts commands, usually entered as single letters
- aka normal mode
- e.g.,
ienters insert modeoopens a new line below current one
- Ex Mode
- used to manipulate files, save files, and run outside programs
- aka colon commands
- enter ex mode by typing a colon
:- then type ex mode command
- after command, returns automatically to command mode
- Insert Mode
- used to insert text
- aka edit mode or entry mode
- enter insert mode by typing
i Escexits insert mode back to command mode
- Command Mode
Check
vi/vimEditor PackageA Linux distro may not have the full
vipackage installed. To check:
type vito get package locationreadlink -f {location}to determine if the package is linked to another package
- may get:
/usr/bin/vim.tinyor/usr/bin/vimif itsvim
- check the package that provided the program:
sudo dpkg -S /usr/bin/vim
Basic Text Editing
vi filenameto create and open a fileito enter insert mode--INSERT--appears at the bottom of the window
Escto exit insert mode:to enter ex modewqwrites the buffer contents to the file and quits- some systems display a line of
~down the left side to indicate the end of the file - loading an existing file shows a status line at the bottom with the name, number of lines, and number of characters
- use arrow keys to move
Oopens a new line above current line and enters insert modeoopens a new line below current line and enters insert mode- Copy and paste
yyto copy (aka yank) a line- precede with a number for multiple lines (e.g.,
3yy) - move to line before where you want to paste
ppastes the buffer to the following line
- precede with a number for multiple lines (e.g.,
ddto delete a lineZZsaves and quits- same as
:wq
- same as
~changes case of highlighted characteruto undo/to forward search for text?to backward search for textccto change text of entire linecwto change text of current wordG+numbermoves to a specified line numberHbrings cursor to home (top of file)Lbrings cursor to bottom of file- replace all occurrences of a string with another
:%s/original/replacement/g- change
%to a starting line number, comma, ending line number
:wto save changes:e filepathedits a new file- first file must be saved first or forced with
:e!
- first file must be saved first or forced with
:rappends contents of an old file in an existing one:!executes a specified external command- e.g.,
:!lsrunsls
- e.g.,
:qquits- only works if changes have been saved or if forced
:q!
- only works if changes have been saved or if forced