Categories
Linux

Vi Basics

Linux Foundation

Commands Used to Start, Exit, Read, and Write Files in vi

Command Usage
vi myfile Start the vi editor and edit the m yfile file
vi -r myfile Start vi and edit m yfile in recovery mode from a system crash
:r file2<ret> Read in file2 and insert at current position
:w<ret> Write to the file
:w myfile<ret> Write out the file to m yfile
:w!    file2<ret> Overwrite file2
:x<ret> or :wq<ret> Exit vi and write out modified file
:q<ret> Quit vi
:q!<ret> Quit vi even though modifications have not been saved

 

Keystrokes Used When Changing Cursor Position in vi

Keys Usage
arrow keys To move up, down, left and right
j or <ret> To move one line down
k To move one line up
h or Backspace To move one character left
l or Space To move one character right
0 To move to beginning of line
$ To move to end of line
w To move to beginning of next word

 

 h  To move to top of file
b To move back to beginning of preceding word
:0 <ret> or (G To move to beginning of file
:n <ret> or nG To move to line n
:$ <ret> or G To move to last line in file
CTRL-F or Page Down To move forward one page
CTRL-B or Page Up To move backward one page
Al To refresh and center screen

 

Searching for Text in vi

Command Usage
/pattern<ret> Search forward for pattern
string<ret> Search backward for pattern

 

Key Usage
n Move to next occurrence of search pattern
N Move to previous occurrence of search pattern

 

Working with Text in vi

Key Usage
a Append text after cursor; stop upon Escape key
A Append text at end of current line; stop upon Escape key
i Insert text before cursor; stop upon Escape key
I Insert text at beginning of current line; stop upon Escape key
o Start a new line below current line, insert text there; stop upon Escape key
0 Start a new line above current line, insert text there; stop upon Escape key
r Replace character at current position
R Replace text starting with current position; stop upon Escape key

 

x Delete character at current position
Nx Delete N characters, starting at current position
dw Delete the word at the current position
D Delete the rest of the current line
dd Delete the current line
Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (cut) the current line and put it in buffer
Nyy or yNy Yank (cut) N lines and put it in buffer
P Paste at the current position the yanked line or lines from the buffer

 

Leave a Reply

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