I would like to share some commonly used vi editor command which will be useful for the beginners who love to work in vi editor.
This will be more useful for new linux users.
This will be more useful for new linux users.
Purpose | vi Command |
To insert new text | esc + i ( You have to press ‘escape’ key then ‘i’) |
To save file | esc + : + w (Press ‘escape’ key then ‘colon’ and finally ‘w’) |
To save file with file name (save as) | esc + : + w “filename” |
To quit the vi editor | esc + : + q |
To quit without saving | esc + : + q! |
To save and quit vi editor | esc + : + wq |
To search for specified word in forward direction | esc + /word (Press ‘escape’ key, type /word-to-find, for e.g. to find word ‘work‘, type as /work) |
To continue with search | n |
To search for specified word in backward direction | esc + ?word (Press ‘escape’ key, type word-to-find) |
To copy the line where cursor is located | esc + yy |
To paste the text just deleted or copied at the cursor | esc + p |
To delete entire line where cursor is located | esc + dd |
To delete word from cursor position | esc + dw |
To Find all occurrence of given word and Replace then globally without confirmation | esc + :$s/word-to-find/word-to-replace/g For. e.g. :$s/work/workers/g Here word “work” is replace with “workers” |
To Find all occurrence of given word and Replace then globally with confirmation | esc + :$s/word-to-find/word-to-replace/cg |
To run shell command like ls, cp or date etc within vi | esc + :!shell-command For e.g. :!pwd |
Basic "vi" features
One edits a file in vi by issuing the command: vi file-to-edit.txt
The vi editor has three modes, command mode, insert mode and command line mode.
- Command mode: letters or sequence of letters interactively command vi. Commands are case sensitive. The ESC key can end a command.
- Insert mode: Text is inserted. The ESC key ends insert mode and returns you to command mode. One can enter insert mode with the "i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands.
- Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen.
Cursor movement:
-
Keystrokes Action h/j/k/l Move cursor left/down/up/right spacebar Move cursor right one space -/+ Move cursor down/up in first column ctrl-d Scroll down one half of a page ctrl-u Scroll up one half of a page ctrl-f Scroll forward one page ctrl-b Scroll back one page M (shift-h) Move cursor to middle of page H Move cursor to top of page L Move cursor to bottom of page W
w
5wMove cursor a word at a time
Move cursor ahead 5 wordsB
b
5bMove cursor back a word at a time
Move cursor back a word at a time
Move cursor back 5 wordse
5eMove cursor to end of word
Move cursor ahead to the end of the 5th word0 (zero) Move cursor to beginning of line $ Move cursor to end of line ) Move cursor to beginning of next sentence ( Move cursor to beginning of current sentence G Move cursor to end of file % Move cursor to the matching bracket.
Place cursor on {}[]() and type "%".
Use the matchit or xmledit plug-in to extend this capability to XML/XHTML tags.'. Move cursor to previously modified line. 'a Move cursor to line mark "a" generated by marking with keystroke "ma" 'A Move cursor to line mark "a" (global between buffers) generated by marking with keystroke "mA" ]' Move cursor to next lower case mark. [' Move cursor to previous lower case mark.
Keystrokes | Action |
---|---|
i | Insert at cursor |
a | Append after cursor |
A | Append at end of line |
ESC | Terminate insert mode |
u | Undo last change |
U | Undo all changes to entire line |
o | Open a new line |
dd 3dd | Delete line Delete 3 lines. |
D | Delete contents of line after cursor |
C | Delete contents of line after cursor and insert new text. Press esc key to end insertion. |
dw 4dw | Delete word Delete 4 words |
cw | Change word |
x | Delete character at cursor |
r | Replace character |
R | Overwrite characters from cursor onward |
s | Substitute one character under cursor continue to insert |
S | Substitute entire line and begin to insert at beginning of line |
~ | Change case of individual character |
ctrl-a ctrl-x | Increment number under the cursor. Decrement number under the cursor. |
/search_string{CR} | Search for search_string |
?search_string{CR} | Search backwards (up in file) for search_string |
/\<search_string\>{CR} | Search for search_word Ex: /\<s\> Search for variable "s" but ignore declaration "string" or words containing "s". This will find "string s;", "s = fn(x);", "x = fn(s);", etc |
n | Find next occurrence of search_word |
N | Find previous occurrence of search_word |
. | repeat last command action. |
No comments:
Post a Comment