My Favorite Vi(m) commands
Thursday, September 20th, 2012I have found myself using Vim as my Linux editor of choice. It is not dependent on X11 being available and I can edit pretty much anything fairly quickly. I have been using a Redhat flavored linux(Redhat,Fedora, Centos) for quite sometime and because I couldn’t guarantee that Emacs would be installed I just got used to using Vi.
When I first attempted to use Vi after using Emacs from my college days, I was initially very frustrated with modes and key sequences. But after a very short time I found I could edit files very rapidly and make changes quite easily.
Quick key list:
Esc | The escape key is the way to get out of editor mode and clear |
Ctrl-c | will exit the editor mode |
:q! | Exits without saving |
u | Undo |
cw | changes the following word |
dd | deletes the line |
D | deletes the line to the end |
J | removes the end of the line |
:e or :edit | reload the file from disk |
1) The thing that I do quite often is to replace all Ctrl-M (Windows Returns) within a file. (Yes, I could do this with a call to dos2unix, but this is a Vi list). I found the following key sequence works quite well.
:%s/Ctrl-v<enter>//g
breaking this sequence down
2) Another sequence I use quite often is to reformat source code.
:1 <enter> <shift-v> <shift>-g =
breaking the sequence down
:1<enter> – states go to the first line of the file
<shift>-v – Turns on visual line mode which selects the entire current line
<shift>-g -Makes the cursor go to the last line in the file and with the last command
selects the entire file
= – Tells the editor to reformat the selection area
This page will be updated as I figure out more useful commands. This is only a small amount of the power of VI!