Sunday, December 25, 2016

Essentials of Vi editor - part 1

I'm sure if you are a serious programmer that you would agree vi is a programmers editor. Knowing vi's commands and usage helps you a lot with your programming tasks and undoubtedly it's a light weight powerful toolkit in your arsenal. In this post I would like to refresh your know-how on vi commands and usage, although there are hundreds of posts and cheat sheets available online. Some commands are extremely common whereas there are few I think which is not so common but extremely powerful. I cover these using three posts.

Moving around the files
--------------------------------------------------------------


H (left) , J (down), K (up) , L (right)
w (move forward by one word), b (move backward by one word)
e (move forward till end of current word)
) (Forward a sentence), ( (Bacward a sentence)
} (Forward a full paragraph), { (Backward a full paragraph)

^ (Move to beginning of a line)
$ (Move to end of a line)
% (Matching bracket or brace)

Shift+g (Jump to end of file)
1 and then Shift+g (Jump to beginning of the file)

This works to jump on to a line as well.
e.g: 23 and then Shift+g (Jump to line 23)

Ctrl+e // scroll down one line
Ctrl+y // Scroll up one line
Ctrl+d // Scroll down half a screen
Ctrl+u // Scroll up half a screen
Ctrl+f // Scroll down a full screen
Ctrl+b // Scroll up a full screen



Getting the status of the file
--------------------------------------------------------------


Ctrl+g    // Shows if the file is modified, the number of lines and the percentage the current line is from the beginning)


Saving and quitting
--------------------------------------------------------------


:w (Saves the file into the disk and keeps it open)
:wq (Save and close the file) // Equivalent to Shift + ZZ
:q (Quit without saving an unedited file. Warns you if edited)
:q! (Quite without saving even if the file is edited)
:vi <filename> // Closes the current file and open the <filename>. Equivalent to :q and then issuing vi <filename>. If the current file is edited as :q does, a warning will be given
:vi! <filename> // Does the same as above but doesn't warn you. Equivalent to :q! and then issuing vi <filename>.


e.g:

vi file1.txt file2.txt  // loads both file into memory and shows the file1.txt

:n // Shift to the next file
:N // Shift back to the previous file
:rew  // Rewind to first file if you have multiple files open

:r // read a file and insert into the current file you are editing
Ctrl + g  // shows line number and file status



Text Editing
--------------------------------------------------------------

a - append at the end of the cursor
A - append at the end of the line
i - insert before the cursor
I - insert at the beginning of the line
o - Open a new line below
O - open a new line above the current line


All above commands switch the file to insert mode

r - change the current character and be in command mode
s - change the current character and switch to insert mode
cc - delete the current line and switch to insert mode for editing
cw - Edit the current word
c$ - Delete from current position to end of line and keep editing
c^ - Delete all from beginning of the line to current location and keep editing


x - deletes the current character e.g - 5x to delete 5 character
dd - deletes the current line - e.g - 5dd to delete 5 lines
dw - deletes the current word
de - deletes till the end of current word including a whitespace
d^ - deletes from beginning of the line to current caret position
d$ - deletes from current caret position to end of the line (Shift + d does the same)

R - enters to overwrite mode. Whatever character you type will replace the character under the caret
~ - Changes the case of the character under the caret
J - join the next line to the current line

No comments:

Post a Comment