Thursday, November 23, 2006

Tables in blogger and some good VI (ViM) Commands

Someone someday said something about VI - If I remember right, it was : Either learn Vi or learn unix. You can't do both in a life time. VI itself is a unix , millions of command or shorcuts hidden inside vi. When I say VI, I meant VIM. I joined orkut community of VI and is copying many of the useful (to me) commands discussed there to my blog. Copyleft wrongs belong Orkut VI community.

BTW I must say it was VI which helped me to join all the pretty tables to a single line table, to avoid the huge space just above the table in a matter of secs. The command I used was :1,$join!

Normal/Command Mode
-------------------

cw Delete a word and put in insert mode
dw Delete a word
~ Change the case letters
guu lowercase line
gUU uppercase line
ga display hex,ascii value of character under cursor
u Undo
Ctrl + R Redo
Alt + W Move word by word
G or ]] Go to last line
gg or [[ Go to first line
$ Go to the end of line
0 Go to the beginning of line
ggdG or [[dG or [[d]] Delete the entire lines of a file
^g Display current line number on the bottom (every detail:-))
cc Cut the line, leaving a blank line there
dd Delete the line (similar to cc, but wont leave a blank line)
yy Yank/Copy a line
p Paste the line just cc'ed or dd'ed or yy'ed
. Yes, that's a dot. (Period). Repeats the last command executed in normal mode
/word Search for the word, 'word' in the document
% To match appropriate brace close for an open brace
J Join 2 lines. To join more than one line (say 10) press : 10 shift+j
D Delete from cursor to end of line
X backspace
x Delete character under cursor
ma Mark the current line as "line a"
mb Mark the current line as "line b"
'a Return to the line marked "a"
d'a or y'a delete or copy the line marked a
/\<\d\{4}\> Search for exactly 4 digit numbers
/\<\a\{4}\> Search for 4 letter words
/first\_s*second/i Search for first followed second on a new line
/bugs\(\_.\)*bunny bugs followed by bunny anywhere in file
/^\n\{3} Find 3 empty lines

Ex Mode

-----------

By Ex mode I mean the executable mode, where the command starts with a colon [ : ]

:help Vi's inbuilt help
:%s/OlD/new/g Replace 'OlD' with 'new' word all over the document / file
:%s/OlD/new/gi Same results as above, but case insensitive replacements
:s/old/new/g Replace 'old' word with 'new' word in the line at which cursor is currently pointed at
:2,5s/old/new/g Replace old with new from line number two to five
:%s/\r/\r/g Turn DOS returns ^M into real returns. I usally do a search and replace of Ctrl+v Ctrl+m.
:%s/^\(.*\)\n\1$/\1/ Delete duplicate lines, which are together
:shell Escape to shell to do anything then exit returning to vi
:q Quit :-D
:x, :wq Save and Quit
ZZ Quit equivalent to :wq!
:ma a ma is the command and a is the argument. Marks the point where cursor is present as a. (But it explicitly wont show it). Now if you want to copy somthin from some other point to the point 'a' , keep the cursor at the other point and type y'a (in escape mode) (y can be replaced by d and so on).
:r!cmd r followed by shell command, reads the command o/p and paste in a line just below cursor.
:rew! rew! is for rewind. Clear all the buffers and the files to initial state of editing.
:set nu Display line numbers
:%! nl -ba Enough display, really number the lines
:sp it splits your screen and ^W - to move between windows
:X prompts for an encryption key. After writing your key, if you save your document it will be encrypted and no one else (but you and vim) can read your documents. If you reopen the file, VIM will ask for the key. If you want to disable encryption, just type :set key=
:%!xxd view in Hex format
%!xxd -r Revert to Normal format from Hex format
:g/^\s*$/d Delete all blank lines
:v/./.,/./-1join compress empty lines (two or three lines to 1)
:s/\(.*\)\ \(.*\)/\2 \1/g Shift the last word of the line to the first, first to second and so on. %s - will do it for entire file


That's all for now. I will add more as I learn more. I need to learn the basics of advanced VI, playing with the registers.

No comments: