AIX 5L Basics (Unit 07) – vi Editor

The name vi comes from the word visual.
The vi editor is a text editor not a word processor.
The vi editor takes over the full screen

Two Modes:
——————
Command Mode (vi starts in command mode)
Text Mode

Note: vi doesn’t give line breaks (you must press enter to insert a line break)

vi filename – start vi

i – insert before cursor, I – insert at beginning of line
a – append after cursor, A – append at end of line
o – insert a new line
0r filename – read filename into the current file

esc – command mode
:q! – quit without saving
:x, :wq or shift-zz – save and exit
:wq – /home/wltest/filename – save to filename
:w – save but don’t quit

Cursor Movements:
—————————-
h – left
j – down
k – up
l – right

1G – Top of document
4G – 4th line of document
G – Bottom of document

H – Top of screen
L – Bottom of screen

– back previous screen
– forward next screen

0 – beginning of line
$ – end of line

b – backwards one word
w – forwards one ward

B – backwards one word including punctuation
W – forward one word including punctuation

1G – Top of document
back previous screen
H – Top of screen
k – up one line

^
|
0 – beginning of line | $ – end of line
B – backword including punc | W – forward includ punc
b – backwards one word | w – foreward one word
h – left | l – right
<------------------------|------------------------->
|
|
^

j – down one line
L – Bottom of screen
forward next screen
G – Bottom of document

Deleting Text:
———————
x – delete single character
dw – delete word
d$ – delete to end of line
d0 – delete to beginning of line
dd – delete whole line
:20,40d – delete range of lines
u – undo last change
U – Undo all changes to the current line

Search for a Pattern:
——————————
/text – search forward
?text – search backward
n – find next

Change Text:
——————-
:g/ text /s// replacement text /g
g – global search
s – substitute
/g – entire document repeat
Note: spacing does matter in the ‘text’ or ‘replacement text’.
Note: only replace the first occurance of ‘text’ on each line
c2w – change two words
Rnewtext – replace by overtyping

Moving Text:
——————-
dd – delete line (cut)
p – paste
yy – yanking (copy line to buffer)
:20,40d (delete line range) p – (paste lines)

Executing AIX Commands within vi:
—————————————————
esc – command mode
:!ls – runs the ls command, press enter to continue in vi
:r snacks – pastes file contents into the current file being edited in vi
:!sh – gives the AIX prompt (ctrl-d or exit – to return to vi)
:!ksh

vi Options: (Store in $HOME/.exrc to make permanent)
—————-
:set all
:set autoindent / noautoindent
:set number / nonumber – displays line numbers
:set list / nolist
:set showmode / noshowmode (Text/Command)
:set tabstop=x
:set wrapmargin=5

Command Line Editing: (Store in $HOME/.kshrc)
———————————- (h-left, j-down, k-up, l-right)
set -o vi (setting to use vi to edit command lines, arrow keys don’t work)
esc -k (go to previous command)

vi Editors:
————–
vi – Full screen, full-function editor
view – read only form of vi (cannot save unless overwritten with force !)
vedit – Beginners version of vi, showmode on by default
ex, ed – Subset of vi working in line mode, access screen editing capabilities of vi
edit – simple form of ex

Leave a Reply

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

*