VIM Editor
VI was the first screen editor written for UNIX. Although it was intended to be small and simple, it can be a challenge for users who use GUI editors such as Notepad++ or gedit, to name a few:)
Last updated
Was this helpful?
VI was the first screen editor written for UNIX. Although it was intended to be small and simple, it can be a challenge for users who use GUI editors such as Notepad++ or gedit, to name a few:)
Last updated
Was this helpful?
To use Vi we must first understand the 3 modes in which this powerful program operates, in order to begin learning later about its text-editing procedures. Please note that modern Linux distro's ship with a variant of Vi called Vim (vi improved). For this narrative we use vi and vim interchangeably.
Vim is different to other text editors but 2 main features makes it so different:
Modal Editing - Effectively editing text makes Vim more powerful than any other text editor. Want to jump to the top of the file gg
or go to the bottom G
. In normal text editors you type gg or G you will in fact type those letters into your file, that's why Vim has modes, and pressing gg or G in a different mode produces different results
Operators- If you are moving your cursor using arrow keys, "page-up", "page-down", "end" and "home" keys - Vim has so much more to offer: d$
if you want to delete text to the end of the line ($= end of line and d=delete). Here 'd' is the operator command. These type of commands (operators) can delete, change or insert text, copy or format it etc
Vim has 12 modes in total but we will normally use between 4 and 5 in our daily use. Very important to understand these.
When you start vim = normal mode. Mostly used for navigation and text manipulation. A good habit to adopt and keep in mind: whenever you're not typing - get back into normal mode
Inserts new text, but can also run some commands
In this mode you can run Ex commands like :set number
, enter search patterns like /word
and enter filter commands. After running the command Vim returns to normal mode.
For navigation and manipulation of text selections. It's similar to Normal mode, but the movement commands extend a highlighted area. When you use a non-movement command, it’s executed for the highlighted area. By default, there is “– VISUAL –” shown at the bottom of the window.
Current file = current 'buffer'
From terminal
vim /etc/passwd
Inside Vim
:e /etc/passwd
Copy a file or contents of a file to the current file using the read command or r :r file.txt Insert the file file.txt below the cursor in current buffer. :0r file.txt Insert the file file.txt before the first line. :r!sed -n 2,8p file.txt Insert lines 2 to 8 from file file.txt below the cursor :r !ls Insert a directory listing below the cursor.
:wq Save currently opened file and exit Vim (even if file is not changed). :x Exit Vim but write only when changes have been made. ZZ Equivalent to :x. Notice there’s no : This is a key press :q! Exit Vim without saving currently opened file. :qa Exit all open files in current Vim session
:w Save currently opened file (which was previously saved). :w file.txt Save currently opened file as file.txt. :w! file.txt Save file as file.txt with overwrite option. :sav file.txt Save current buffer as a new file file.txt. :up[date] file.txt Like :w but only save when the buffer has been modified
Key Command
Description
h or left arrow (5h or 5 left arrow)
Go one character to the left (or 5 characters left)
j or down arrow (5j or 5 down arrow)
Go down one line
k or up arrow (6k or 6up arrow)
Go up one line
l or right arrow (6l or 6right arrow)
Go one character to the right
H
Go to top of the current window
M
Go to middle of the current window
L
Go to bottom of current window
w (or 3w etc)
Move one word (incl delimiters)to the right (can also use 3w etc)
W
Move one word (excludes deliminator like .{ } - $ etc
b (or 7b etc)
Move back one word to the left (incl deliminator)(Can also use 7b etc)
B
Move back one word to the left (excludes deliminator)
e
end of current word (incl deliminator)
E
end of current word (excludes deliminator)
0(zero)
Go to the beginning of the current line
^
Go to the first non blank character on the current line
$
Go to the end of the current line
Ctrl-b
Go back one page
Ctrl-f
Go forward one page
i
Insert
r
Replace a single letter under the cursor
R
Replace multiple characters
x
Deleting text under character
Shift-x
Deletes character before cursor
cw
Change the current word with new text under cursor
dw
Deleting the single word beginning with character under cursor
dd
Delete entire current line
Shift-d
Deletes everything from the cursor to the end of the line
5dd
Deletes 5 (or whatever number) lines beginning with current line
D
Delete remainder of the line starting with current cursor position
:set nu
Displays all line number
:12,22w test
Writes lines 12 - 22 to new file 'test' in pwd
Replaces every occurrence (the g) of apple with pear
%s/apple/pear/g
:%s/^M//g
How to remove Ctrl-M characters from a file in UNIX.(Hold down Ctrl key press V and M in succession
yy
copy(yank,cut) the current line into the buffer
Nyy
copy(yank,cut) the next N lines including current line
p
put(paste) the line(s) in the buffer after the current line
gg Go to the top of the file G Go to the bottom of the file { Go to the beginning of current paragraph } Go to the end of current paragraph % Go to the matching pair of ( ), [ ], { } 50% Go to line at the 50% of the file :NUM Go to line NUM. :28 jumps to line 28
/hello - search hello using the forward slash, use n for next or N to search backwards Can use ggn to start from the top or GN to start from the bottom up :) * or # Search for word under cursor use * going forward and # searching backwards - COOL!! / or ? + up or down arrows to search previous history
When you cut and paste into vi form an outside program, the formatting can get a little messed up! Vim comes with a feature called "pastetoggle" which you can enable by editing the line /etc/vim/vimrc and adding the following at the bottom: