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 screen
ctrl + b – Move backward by one screen
Search and Replace:
          
/pattern –  Go to pattern in forward direction
?pattern – Go to pattern in backward direction
n             – Repeat last search, looking in direction of initial search
N            – 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 buffer
10Y – Yank 20 lines
yG  – Yank till end of file
yw  – Yank current word
p   – Put general buffer after cursor
P   – Put general buffer before cursor
Insert Mode Commands:
Ctrl-h  – Delete back one character
Ctrl-w – Delete back one word
Ctrl-u  – Delete back to start of line 
Ctrl-c – Go back to normal mode
Undo:
u – undo
U – Undo all changes on the line
Ctrl + r – redo
Vim Modes:
r – replaces single character in command mode
R – Replace mode
v – Character-wise Visual mode
V – Line-wise Visual mode
ctr + v – Block-wise Visual mode
Changing Case:
~     – changes the case of the letter
g~w – swtiches the case of whole word
gUw – upper case whole word
gUU – changes case of whole line
guw – changes case of whole word to lowercase
guu – changes case of whole line to lower case
Input commands:
a – Append after cursor
A – Appends at the end of the current line
i – Insert before cursor
I – insert mode by moving cursor to the first non blanck character of the line
o – Go to next line in insert mode
O – 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 word
cc – Change line 
c$ – 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 character
dd – Deletes current line
5dd – Deletes 5 lines from current line
dk – Deletes current line and line above. Similarly dj
d0 – Deletes to beginning of the line
d$/D – Deletes to the end of the line
dw – Deletes word
db – Deletes previous word
das – Deletes entire sentence
dap – Deletes entire paragraph
daw – Deletes the word irrespective of its position
di” – Deletes inside a quote
da” – 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 lineNum2
80i* + esc – To repeat * 80 times
5o# + esc – To write 5 lines that starts with #
c$ or C – deletes till end of the line and goes to insert mode

Comments

Popular posts from this blog

HOW WE REDUCED SOA OSB PROVISIONING FROM 4 DAYS TO 4 HOURS

NOT ABLE TO START RABBITMQ CLUSTER: CANNOT DECLARE A QUEUE ‘~S’ ON NODE ‘~S’: ~255P

SOA SUITE 12.2.1.4 INSTALLATION: GOT EXCEPTION WHEN AUTO CONFIGURING THE SCHEMA COMPONENT(S) WITH DATA OBTAINED FROM SHADOW TABLE