VI/Vim Editor Tutorial for Linux

The best way to learn Vi is to create a new file and try it out for yourself. Vi is a powerful, ubiquitous text editor found on all Unix/Linux systems. Vim (Vi IMproved) is an enhanced version with more features. Both are modal editors with different modes for different operations.

Starting vi Editor:

CommandDescription
$ vi filenameOpen or create file
viOpen new file to be named later
vi -r filenameRecover crashed file
view filenameOpen file read-only
vi +num filenameOpen file at line number
vi +/pattern filenameOpen file at first pattern match

Operation Modes:

  • Command Mode: Where you give commands (delete, copy, save, etc.) – Press ESC to return to this mode
  • Insert Mode: Where you actually type text – Enter with: i, I, a, A, o, O
  • Visual Mode: For selecting text – Enter with: v (character), V (line), Ctrl+v (block)

Cursor Movement Commands:

CommandDescription
hMove left one character
jMove down one line
kMove up one line
lMove right one character
SpacebarMove right one character
ReturnMove down one line
BackspaceMove left one character
0 or |Move to beginning of line
$Move to end of line
wMove to next word beginning
WMove to next word (past punctuation)
bMove to previous word beginning
BMove to previous word (past punctuation)
eMove to end of current word
EMove to end of word (past punctuation)
HMove to top of screen
MMove to middle of screen
LMove to bottom of screen
GGo to last line of file
1GGo to first line of file
21GGo to line 21
nG or :nGo to line n
ggGo to first line in file
%Move to matching bracket
)Move to next sentence
(Move to previous sentence
}Move to next paragraph
{Move to current paragraph
]]Move to next section
[[Move to current section

Screen Movement Commands:

CommandDescription
Ctrl+fPage forward one screen
Ctrl+bPage backward one screen
Ctrl+dScroll forward one-half screen
Ctrl+uScroll backward one-half screen
Ctrl+eScroll down one line
Ctrl+yScroll up one line
Ctrl+lClear (refresh) scrambled screen

Inserting Characters and Lines:

CommandDescription
iInsert text before cursor
IInsert text at beginning of line
aInsert text after cursor
AInsert text at end of line
oOpen new line below cursor
OOpen new line above cursor

Deleting Text:

CommandDescription
xDelete character at cursor
XDelete character before cursor
dwDelete word from cursor position
d^Delete from cursor to beginning of line
d$Delete from cursor to end of line
DDelete from cursor to end of line
ddDelete entire line
XddDelete X number of lines
dGDelete to end of file
d1GDelete from beginning to cursor
:5,10 dDelete lines 5-10

Changing Text:

CommandDescription
cwChange word from cursor position
ccChange entire line
CChange from cursor to end of line
sSubstitute character
SSubstitute entire line
rReplace character at cursor
RReplace multiple characters (overwrite)
r ReturnBreak line
JJoin current line with next line
xpTranspose character at cursor
~Change case of letter
Ctrl+aIncrement number under cursor
Ctrl+xDecrement number under cursor

Undo/Redo Commands:

CommandDescription
uUndo last change
UUndo all changes on current line
Ctrl+rRedo last change
:uUndo previous command-line command

Copying and Moving Text:

CommandDescription
yy or YYank (copy) current line
ywYank current word
XyyYank X number of lines
pPut yanked text below current line
PPut yanked text above current line
:1,2 co 3Copy lines 1-2 and put after line 3
:4,5 m 6Move lines 4-5 and put after line 6

Searching Commands:

CommandDescription
/stringSearch forward for string
?stringSearch backward for string
nFind next occurrence
NFind previous occurrence
*Search for word under cursor
/\Search for exact word
fxFind character x in current line
FxFind character x backward in line
txFind before character x in line
TxFind before character x backward

Search Patterns:

PatternDescription
^Beginning of line
$End of line
.Any single character
*Zero or more of previous character
[abc]Any character a, b, or c
\dAny digit (0-9)
+One or more of previous character
?Zero or one of previous character
string1|string2Either string1 or string2

Replacing Text:

CommandDescription
:s/old/newReplace first occurrence in line
:s/old/new/gReplace all in current line
:%s/old/new/gReplace all in file
:%s/old/new/gcReplace with confirmation
:5,10s/old/new/gReplace in lines 5-10
:g/search/s//replace/gSearch and replace globally

File Operations:

CommandDescription
:wSave file
:w filenameSave as filename
:w!Save (force)
:wq or ZZSave and quit
:qQuit (if no changes)
:q!Quit without saving
:qaQuit all files
:e filenameEdit another file
:r filenameInsert file after cursor
:34 r filenameInsert file after line 34
:fShow file info
:f filenameRename current file
:cd dirnameChange directory

Multiple Files:

CommandDescription
:nNext file
:pPrevious file
:NPrevious file
:argsShow file list
:e#Toggle between two files

Advanced Commands:

CommandDescription
<<Shift line left
>>Shift line right
:20,40>Shift lines 20-40 right
:20,40<Shift lines 20-40 left
Ctrl+gShow file status
.Repeat last command
maMark line as ‘a’
‘aJump to mark ‘a’
]’Next mark
[‘Previous mark
q[register]Start recording macro
@[register]Execute macro
@@Repeat last macro

Set Commands (Configuration):

CommandDescription
:set nuShow line numbers
:set nonuHide line numbers
:set icIgnore case in searches
:set noicCase sensitive searches
:set aiAutoindent
:set noaiNo autoindent
:set sw=4Set shift width to 4
:set ts=8Set tab stop to 8
:set wsWrap scan
:set wm=2Wrap margin 2 chars
:set listShow tabs/ends
:set nolistHide tabs/ends
:set roRead-only mode
:set bg=darkDark background
:set bg=lightLight background
:set syntax onSyntax highlighting
:set allShow all settings

Vim Command Line Arguments:

ArgumentDescription
vim +num fileOpen at line num
vim +/pattern fileOpen at pattern
vim -b fileBinary mode
vim -d file1 file2Diff mode
vim -R fileRead-only mode
vim -x fileEncryption mode
vim -r fileRecovery mode

Running Commands:

CommandDescription
:!commandRun shell command
:!lsList files
:!pwdShow directory
:shStart shell
Ctrl+ZSuspend vim
fgResume vim

Buffer Operations:

CommandDescription
:lsList buffers
:bnNext buffer
:bpPrevious buffer
:bdDelete buffer
“ayyYank to buffer a
“apPaste from buffer a
“a5ddDelete 5 lines to buffer a

Visual Mode:

CommandDescription
vVisual character mode
VVisual line mode
Ctrl+vVisual block mode
yYank selected
dDelete selected
>Indent right
<Indent left
:'<,’>w fileWrite selection to file

Getting Help:

CommandDescription
:helpOpen help
:help topicHelp on topic
vimtutorInteractive tutorial
:versionShow version

Practice Exercises:

  1. Create a new file: vi practice.txt
  2. Enter insert mode (i) and type several lines
  3. Practice navigation (h,j,k,l, w,b,$,0,G)
  4. Delete a line (dd) and undo it (u)
  5. Copy a line (yy) and paste it (p)
  6. Search for a word (/word)
  7. Replace text (:s/old/new/g)
  8. Save and exit (:wq or ZZ)

Quick Reference (Most Essential):

  • i – Enter insert mode
  • ESC – Return to command mode
  • :wq – Save and quit
  • :q! – Quit without saving
  • dd – Delete line
  • yy – Copy line
  • p – Paste
  • u – Undo
  • /text – Search
  • :s/old/new/g – Replace

Tip: Start with vimtutor – it’s a 30-minute interactive tutorial that comes with Vim. The best way to learn Vi/Vim is through practice.

Remember: Vim has a steep learning curve but pays off with increased productivity once mastered. Start with basics and gradually incorporate advanced features!

Leave a Reply

Your email address will not be published. Required fields are marked *