How to use Vi editor
Step by step how to use Vi editor
This article is made for basic guide to use vi editor to create and edit file on Linux system. Please bear in mind that this vi editor guide only focusing on the basic step to use vi editor.
need to know: The vi mainly have 2 mode, the editing mode and command mode… the other is "ex" command.
The "ex" command start with colon (:). They are called "ex" commands because they are the ex text editor commands.
Basics step of the vi editor start with:
Open file using vi editor
To open the file with vi editor, just invoke vi command follow with the filename, please note that if the filename specify not in the directory path the vi editor give new empty file to start with…
# vi - open vi editor
# vi filename.txt - open filename.txt with vi editor
# vi /tmp/filename.txt - open filename.txt inside /tmp/ directory with vi editor
Edit modes: Create and edit text
These keys enter editing modes to enable you to type in the text on your document.
i - Insert before current cursor position
I - Insert at beginning of current line
a - Insert (append) after current cursor position
A - Append to end of line
r - Replace 1 character
R - Replace mode
Press <ESC> key to exit from an editing mode.
Command mode: Yank, Copy, Delete, Cut and Paste
x - Delete single character (cut)
dd - Delete current line and put in buffer (cut)
ndd - Delete n lines (n = number of lines e.g. 3dd) and put them in buffer (cut)
yy - Yank current line into buffer (copy)
nyy - Yank n lines (n = number of lines e.g. 3yy) into buffer (copy)
J - Attaches the next line to the end of the current line (deletes carriage return).
p - Put the contents of the buffer after the current line (paste)
P - Put the contents of the buffer before the current line (paste)
Command mode: Undo
u - Undo last command
U - Undo all action in the current line
cursor positioning
^d - Page down
^u - Page up
:n - Position cursor at line n
:$ - Position cursor at end of file
^g - Display current line number
h - Left, j - Down, k - Up, l - Right.
string substitution
:n1,n2:s/string1/string2/[g] - Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global), all instances of string1 on each line are substituted. If g is not included, only the first instance per matching line is substituted.
^ - matches start of line
. - matches any single character
$ - matches end of line
These and other "special characters" (like the forward slash) can be "escaped" with \ i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT"
Examples:
:1,$:s/dog/cat/g - Substitute 'cat' for 'dog', every instance for the entire file - lines 1 to $ (end of file)
:23,25:/frog/bird/ - Substitute 'bird' for 'frog' on lines 23 through 25. Only the first instance on each line is substituted.
Command mode: Save and Exit files
ZZ - write and quit
Note: you need to press <ESC> to exit from an editing mode.
"ex" command: Save and Exit files
command start with colon (:).
:w - Write the current file.
:w new.file - Write the file to the name 'new.file'.
:w! - Overwrite file.
:wq - Write and quit.
:x - Write and quit.
:q - Quit.
:q! - Quit with no changes.
Other useful "ex" command
:e filename - Open the file 'filename' for editing.
Parameters
:set number - Turns on line numbering
:set nonumber - Turns off line numbering
:set all - List all possible parameters
Search text
/string - search down for string
?string - search up for string
n - repeat search
N - repeat search reverse from present position
Keywords: how to use vi, using vi, using vi editor, vi example
- 8071 reads
- Email this page
Recent comments
8 years 52 weeks ago
9 years 3 weeks ago
9 years 16 weeks ago
9 years 22 weeks ago
9 years 32 weeks ago
9 years 35 weeks ago
9 years 43 weeks ago
9 years 50 weeks ago
10 years 2 weeks ago
10 years 7 weeks ago