My Vim/vi Notes
This blog covers basics of Vim/vi that are useful in day to day activities of a weblogic/fmw admin.
Navigation Commands:
h – Move cursor left
j – Move cursor down
k – Move cursor up
l – Move cursor right
0 – Move to first character of real line
gj/gk – Move down or up by display lines instead of real lines
g0 – To first character of display line (when line spreads multiple lines, useful in navigating log files)
gg – Go to beginning of the file
G – Go to last line of the file
8G/8gg – Go to 8th line
w – Move to start of the word
e – Move to end of the word
b – Move backward to start of the word
Movement by finding character:
f{char} – Move to the next occurrence of {char}F{char} – Move backward to the previous occurrence of {char}t{char} – Move to the character before the next occurrence of {char}T{char} – Move backward to the character after the previous occurrence of {char}* – Search for the word under the cursor at that moment# – Same as above but in backward direction.; – Repeat the last character-search command, – Reverse the last character-search command
Screen Movements:
ctrl + f – Move forward by one screenctrl + b – Move backward by one screen
Search and Replace:
/pattern – Go to pattern in forward direction?pattern – Go to pattern in backward directionn – Repeat last search, looking in direction of initial searchN – Repeat last search, looking in reverse direction:s/old/new – Replaces first occurrence of old string with new string in the current line:s/old/new/g – Replaces all occurrences of old with new in the current line:%s/old/new/g – Replaces all occurrences of old with new in whole file:%s/old/new/gc – Replaces all occurrences of old with new in whole file with confirmation
Display Commands:
:set list – Show invisible characters:set nolist – Don’t show invisible characters:set number – Show line numbers:set nonumber – Don’t show line numbers
Command History:
: + up arrow – To scroll through command history/ + up arrow – To scroll through search history:string + up arrow – To scroll through command history that starts with string/string + up arrow – To scroll through search history that starts with string
Save/Exit:
:w – Save your file but not quit vi:wq – Save and quit edit session:q – To quit if no changes are made:q! – To quit ignoring unsaved edits
Yank & Paste:
yy – Copy current line to general buffer10Y – Yank 20 linesyG – Yank till end of fileyw – Yank current wordp – Put general buffer after cursorP – Put general buffer before cursor
Insert Mode Commands:
Ctrl-h – Delete back one characterCtrl-w – Delete back one wordCtrl-u – Delete back to start of line
Ctrl-c – Go back to normal mode
Undo:
u – undoU – Undo all changes on the lineCtrl + r – redo
Vim Modes:
r – replaces single character in command modeR – Replace modev – Character-wise Visual modeV – Line-wise Visual modectr + v – Block-wise Visual mode
Changing Case:
~ – changes the case of the letterg~w – swtiches the case of whole wordgUw – upper case whole wordgUU – changes case of whole lineguw – changes case of whole word to lowercaseguu – changes case of whole line to lower case
Input commands:
a – Append after cursorA – Appends at the end of the current linei – Insert before cursorI – insert mode by moving cursor to the first non blanck character of the lineo – Go to next line in insert modeO – Go to line above current line in insert mode>G – Increases the indentation from the current line until the end of the file
Change commands:
cw – Change wordcc – Change linec$ – Change to end of line. – Repeat last change
ci” – To change inside a quote. This can be used for all other paterns like },]
Deletion:
x – Deletes current characterdd – Deletes current line5dd – Deletes 5 lines from current linedk – Deletes current line and line above. Similarly djd0 – Deletes to beginning of the lined$/D – Deletes to the end of the linedw – Deletes worddb – Deletes previous worddas – Deletes entire sentencedap – Deletes entire paragraphdaw – Deletes the word irrespective of its positiondi” – Deletes inside a quoteda” – Deletes inside a quote including quote
Command Mode:
:!command – Execute shell command from vi editor:! sh % – Execute currently editing file:shell – Open shell without closing edit session and come back:read !{cmd} – Executes {cmd} in the shell and insert its standard output below the cursor
Misc:
J – Joins two lines:lineNum1,lineNum2s/^/# – To comment lines from lineNum1 to lineNum280i* + esc – To repeat * 80 times5o# + esc – To write 5 lines that starts with #c$ or C – deletes till end of the line and goes to insert mode
Comments
Post a Comment