Unit 7 The VI Editor: Course Code LX13 Linux Basics
Unit 7 The VI Editor: Course Code LX13 Linux Basics
Full-screen editor
Two modes of operation
command
text
Utilizes one-letter command
Does not format text
Flexible search and replace facility with pattern matching
Allows for user-defined editing functions using macros
Starting vi
$ vi vifile
~
~
~
~
~
~
~
~
~
~
~
~
"vifile" New file
Two Modes
Command Input
mode mode
a append text after cursor
A append text at end of line
i insert text at the cursor
I insert text at beginning of line
o add an empty line under cursor
O add an empty line above cursor
s substitute a character with text
<ESC>
Cursor Movement
<ctrl-b>
H
k
<up-arrow>
0 $
b,B w,W
h l
<left-arrow> <right-arrow>
<down-arrow>
j
L
<ctrl-f>
G
Editing Text
Keystroke
i This file contains some lines.
Line 2.
And this is line 3.
Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
"vifile" New file
Exiting the Editor
Keystroke
This file contains some lines.
<esc> Line 2.
And this is line 3.
Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
:wq
Search for a Pattern
Keystroke
<esc> This file contains some lines.
Line 2.
And this is line 3.
n Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
/line
Find and Replace
Keystroke
<esc> This file contains some lines.
Line 2.
And this is lINE 3.
Line 4 follows lINE 3.
The last lINE is lINE 5.
~
~
~
~
~
~
~
:g/ line /s// lINE /g
Cut and Paste
Keystroke
<esc> This file contains some lines.
Line 2.
And this is lINE 3.
yy Line 4 follows lINE 3.
The last line is lINE 5.
p Line 4 follows lINE 3.
~
~
~
~
~
~
"vifile" 5 lines, 108 characters
vi - Executing Linux Commands
Examples:
:! ls will execute the ls command
:! bash will give you a shell prompt in which you
can enter a series of commands.
vi Options
set showmode
set tabstop=5
set list
ab lx linux
ab thansk thanks
ab sohw show
~
~
~
~
~
".exrc" 8 lines, 81 characters
vi Editors
$ set -o vi
h j k l
vi Cheat Sheet
Start vi Change text
$ vi file name cw R
Force Copy/Paste
! yy p