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
- Add new comment
- 887 reads
- Email this page
Thanks for Reading and Check Other Popular Linux Topics
All time:
- Configure and Setup Squid Cache Proxy Server on Fedora Core.
- Using Linux rm command to delete, remove hidden files and folder - Basic Linux Command.
- Copy Linux Files and Copy Directory Using cp Command - Linux Basic Command.
- Moving or rename files and directory using Linux mv command - Basic Linux Command.
- Add, view, edit and configure route and static route on Linux Fedora Core - Linux Server Configuration.
- Get and display the size of file and directory in Linux system using du command.
- Step by step Install Setup and Configure Linux Fedora Core Server and Network Management with Screen Shot and Example.
- Create file and display file contents in Linux using cat command.
- Configure Squid to block HTTP request to specific web sites.
- Example of samba smbpasswd file and samba smbusers configuration file.

Recent comments
18 hours 53 min ago
1 day 10 hours ago
3 days 1 hour ago
5 days 7 hours ago
5 days 15 hours ago
5 weeks 3 days ago
9 weeks 3 days ago
14 weeks 1 day ago
14 weeks 1 day ago
14 weeks 6 days ago