Monday, June 24, 2013

Vi Editor Tips & Tricks

The most popular text editor for Linux and Unix operating system is vi (visual editor). It is the most robust and comprehensive text editor to work in CLI mode. Of course there are other text editor emacs, nano etc. The choice is yours.

Vi is the default text editor in Linux and Unix systems. So you don't need to install this package.


Vi editor has various types of modes -
(1) Insert Mode - To edit files.
(2) Command Mode - To execute command to be applied on edited files.
(3) Visual Mode - Visual mode is a flexible and easy way to select a piece of text for an operator.  It is the only way to select a block of text.
(4) Replace Mode - Replace characters, starting with current cursor position, until hit.

Open Vi
To open a file using vi, just execute the following command
$ vi
You can use the following command to recover the file that was being edited when system crashed.
$ vi -r

By default when you open a file using the above command vi opens it in the command mode with the cursor at the first character of the file. To change into the command mode apply any of the following key.
i Enter insert mode at cursor.
IEnter insert mode at the first non white character on the line.
aMove cursor forward by one character and then enter insert mode.
AEnter insert mode at end of line.
oOpen a new line below the current line and enter insert mode.
OOpen a new line above the current line and enter insert mode.
COpen a new line above the current line and enter insert mode.
c$Open a new line above the current line and enter insert mode.
v – Start Visual mode per character.
V – Start Visual mode linewise.
CTRL-V – Start Visual mode blockwise.
R – Start relpace mode.

Exit Vi
You need to be in command mode to exit from vi. If you are in insert mode press escape (Esc) key to go into command mode. Then type either of the following commands and press enter.

Basic Commands
Execute these commands while in command mode.
:w – To save
:w - Save contents to a new file file named
:x Save file and exit from vi
:wq Same as above
:q Exit from vi. If you have edited the file this command won't work
:q! Exit from vi without saving even though changes have been made
:help display help. to quit from help type !q.

Line Navigation
k or up arrow – Navigate one line upwards.
j or down arrow – Navigate one line downwards.
l or right arrow – navigate one character right side.
h or left arrow – navigate one character left side.
w - Forward word by word in a line.
b - Backward word by word in a line.
0 – go to the starting of the current line.
$ – go to the end of the current line.

Paragraph Navigation
{ – Beginning of the current paragraph. Press { again to move to the previous paragraph beginnings.
} – End of the current paragraph. Press } again to move to the previous paragraph ends.

Screen Navigation
H – Go to the first line of current screen.
M – Go to the middle line of current screen.
L – Go to the last line of current screen.
ctrl+f – Jump forward one full screen.
ctrl+b – Jump backwards one full screen

File Navigation
NG – Go to the Nth line of the file.
:N - This also takes you to the Nth line of the file.
G – Move cursor to last line in file.
:$ – Move cursor to first line in file
gg – Move cursor to first line in file.
1G – Move cursor to first line in file.
:0 – Move cursor to first line in file

Search
/searched_word – Search for the pattern which will take you to the next occurrence of it.
?searched_word – Search for the pattern which will take you to the previous occurrence of it.
* - Go to the next occurrence of the current searched text under the cursor.
n - Go to the next occurrence of the current searched text under the cursor.
# - Go to the previous occurrence of the current searched text under the cursor.
N - Go to the previous occurrence of the current searched text under the cursor.
:set ignorecase - Apply this in command mode for case insensitive search.
:set noignorecase - To turn off case insensitive search.
:set incsearch - Jumps to search word as you type (annoying but excellent).
:set hlsearch - To highlight all the search pattern matches in a file set the following option.
:set nohlsearch - To disable the highlighting temporarily.

Editing
x – Delete a character.
u – Undo an action.
ctrl+r – Redo an action.
dd – Cut a line.
dw – Cut a word.
yy – Copy a line.
yw – Copy a word.
D - Delete the remainder of the line, starting with current cursor position.
p – Paste the previously copied line or word.
e – Move to the next word (faster than just moving with the arrow keys).
r – Replace a letter (press r, then the new letter).
guu – Lowercase line.
gUU – Uppercase line.
~ – Invert case (upper->lower; lower->upper) of current character.
xp – Swap next two characters around.
. – Type . in normal mode to repeat last change, this is super useful when doing a receptive task.
% – Jump to a matching opening or closing parenthesis, square bracket or a curly brace. Very useful when programming.

You can type a number before any of these commands and the command will be executed that number of times.
5x – Will delete five characters in a row.
3p – Will paste three times.
Nyy or yNy - Copy (yank, cut) the next N lines, including the current line, into the buffer

Advanced Commands
ctrl+g – Report cursor position within the file in the status line at the bottom of the screen.
:! name_of_a_command – To launch a command external to Vim, directly into your shell. For example, :! ls will display the files within the directory you are currently working in, without quitting the editor.
:sp name_of_a_text_file – Will split the screen in half horizontally, showing the new text file in the other half. To shift the focus from the right to the left window, use the shortcut Ctrl+w.
:vsp name_of_a_text_file – Same as before, but splits the screen vertically.
Ctrl+Shift+C and Ctrl+Shift+V – To copy and paste text in a terminal.
:syntax on – Colour syntax in Perl, HTML, PHP etc.
:Ex – File explorer.
:cd .. – Move to parent directory.

CTRL+N – Insert mode and it will try to complete the current word with the first match in the current file.
CTRL+P – Does the same thing but searches backwards.
:.= - Returns line number of current line at bottom of screen.
:= - Returns the total number of lines at bottom of screen.

For More Info
VIM Manual
vim tips and tricks

Reference
Best Vim Tips
How to Use the vi Editor*
Basic vi Commands
10 kick-ass Vim tips
8 Essential Vim Editor Navigation Fundamentals

No comments:

Post a Comment