02 VI ELEC462
02 VI ELEC462
Dukyun Nam
HPC Lab@KNU
1
About vi
● vi (pronounced as distinct letters, /ˌviːˈaɪ/)
○ A screen-oriented text editor
○ The standard Unix editor
○ It is short for “visual”
○ There are lots of clones (vim, Elvis, nvi, etc.)
< ADM-3A terminal keyboard layout > < ADM-3A terminal with keypad >
* https://en.wikipedia.org/wiki/Vi 2
Entering the vi Editor
Command Description
vi filename Edits an existing filename or creates a new one if
filename does not exist.
vi –r filename Edits filename using .filename.swp as the input .
The file .filename.swp must be deleted, using:
rm .filename.swp, after use.
vi –R filename Edits filename in read-only mode does not allow
changes to filename.
vi +/pattern filename Edits filename at the line containing the pattern.
3
File Interaction in the vi Editor
Command Description
:w Write to the file that your are editing without exiting the editor.
Good for intermediate saves.
:w def Write to a new file def, but stay in the existing file.
:w! def Write to an existing file def, but stay in the existing file.
:r def add (read) file def into the current file
:f Provides information on the file currently editing, including the
current line number, if it has been modified etc. <Ctrl+g>
4
Exiting the vi Editor
Command Description
:q Exit (quit) without changes. Assumes no changes have been
made.
:q! Exit (quit) without changes. Assumes changes have been made.
ZZ or :x Save the file only if changes have been made, then exit the
editor
:wq Save the file and exit the editor. This command will save the file
even if no changes are made.
5
vi Modes
● Command mode:
○ The mode you are in when you start (default mode)
○ The mode in which commands are given to move around in the file, to make
changes, and to leave the file
● Insert (or Text) mode
○ The mode in which text is created
○ There is more than one way to get into insert mode but only one way to
leave: return to command mode by pressing <Esc>
6
vi Modes (cont.)
7
vi Modes (cont.)
8
From Command Mode to Insert Mode
Key Operation
9
Command Mode
Key Operation
● Basic cursor movement
k Up one line
○ h, j, k, l j Down one line
10
Command Mode (cont.)
● General command syntax
○ The general format for commands is
ncm
○ Where:
■ n is an optional multiplier value
■ c is the command
■ m is an optional scale modifier
○ Examples:
■ 3dw – delete 3 special character delimited words
■ dW – delete a single, space, delimited word
11
Command Mode (cont.)
● Delete commands
d Delete this line
12
Command Mode (cont.)
● Splitting and joining lines
○ If you wish to split a line between words, position yourself on a space between the words to be
split, then use the replace (r) followed by the return key. This will replace the space with a
carriage return forming a new line.
○ If you wish to join or combine two lines go to the upper of the two lines to be joined, then key in
the uppercase “J” for join.
● Example:
○ We wish to split the line below between the word “should” and “be”.
“This line should be split.”
1. Position the cursor here
2. Key in “r” followed by “enter”
○ To join the lines:
“This line should 1. Position the cursor anywhere on this line
2. Key in “J”
be split.”
13
Command Mode (cont.)
● Search commands
/text Search for text forward (wraps to beginning)
14
Command Mode (cont.)
● Copy/Paste commands
[n]Y or Copy (yank) n lines
[n]ym Copy (yank) a portion of the file determined by the measurement
(e.g. 2yW will yank 2 space separated words.
P,p Paste yanked or deleted data before (P), after (p) cursor position
■ Note: to cut and paste, use the dm commands to delete and the p or P to paste
15
Command Mode (cont.)
● Some “nice to know” features
○ To turn line numbers on and off use:
■ :set nu and :set nu!
○ To execute a single Unix command from inside the editor use:
■ :!cmd
○ To go to linenum
■ :linenum
○ To temporarily return to the shell use:
■ :sh (type “exit” to return to editor)
○ To repeat a colon command or to go back to earlier colon commands use:
■ : ↑ or↓(up arrow or down arrow)
○ To show the name of the current file use:
■ :f
● Resource: https://www.fprintf.net/vimCheatSheet.html
16