Vi text editor is powerful. As I started using it, I felt its advantage over others. The only problem is memorizing all these commands and shortcuts. In this tutorial, I not going to describe everything in detail, only some helpful commands (at least helpful to me), which I wanted to document first, for my own reference, and with no extra cost I am sharing this with you.
There are two modes:command mode and insert mode, you press mainly 'i' or 'a' to enter the insert mode, and ESC to go back to command mode.
While in command mode type: '[count]x'
to delete 'count' letters starting from the position of the cursor. Without specifying any number and pressing just x will delete just one character under cursor.
If you want to replace a character in a word, say 'pen' into 'pin' then bring the cursor under this 'e' in 'pen' and press ri
, so 'r'
is the replace command, and 'i' is to replace 'e' in 'pen' to 'i' in 'pin'.
pressing u
in command more will undo any changes. :redo
command does redo.
"mdd
, this means delete dd
the line under cursor, and place it in buffer m
, you can choose other character as buffer name. However, you do not need to choose a buffer and store your text in default buffer, thus you can just say dw
and delete the word under cursor, and go where you want to paste it and type p
which will paste the buffer after the cursor position, and P
to paste it before the cursor.
Delete (or Cut) can be done with diffirent option like:
d^
: delete from current cursor position to the start of line.
d$
: delete from current cursor position to the end of the line.
3dw
: delete 3 words starting from current cursor position.
2dd
: delete 2 lines.
Instead of cutting text you can also copy or yank a text using y
command with same options as d
.
/
or ?
commands followed by search string. /
searches from the cursor position and below till the end of the file, and ?
does the same but searches for expressions before the cursor. Here are various examples:
/fish
: search for 'fish' or 'fishing' or 'affish', etc.
/\<fish\>
: search exactly for the word 'fish'
:15,27s/</\</g
: this search will replace all < signs with < signs starting from line 15 till 27
/f[ia]sh
: searches for either 'fish' or 'fash'.
Ctrl b
: Move one page back (or up).
Ctrl f
: Move one page forward (or down).
[count] Ctrl n
: Move cursor next one line. count
specifies number of lines.
[count] Ctrl p
: Move cursor previous one line.
[count] w
: Move cursor by words forward.
[count] b
: Move cursor by words backwards.
[count] (
: Move cursor to the beginning of the sentense.
[count] )
: Move cursor to the end of the sentense.
[count] {
: Move cursor to the beginning of the paragraph.
[count] }
: Move cursor to the end of the paragraph.