What is vim and why do I care?
vi was the tool that old scool unix admins became intimate with in ways best not too carefully examined.
vi was the visual version of ed (ed is actually hard to use.) vi is easy by comparision, and vim is the improved version of vi, but you will think it sucks for the first 10 times you use it.
You'll use it because geeks love it and you want to understand. I will help you. I am not an expert, I'm just somebody who uses it.
The primary use is vim filename but you can also use vim filename1 filename2 filename3
If you use a version of linux based on Redhat then you can use vi instead of vim, because vi on Redhat systems is really vim. Everywhere I show an example using vim you can type vi instead.
Enough history and basic stuff. Lets dive in.
To move around in vim you can probably use the arrow keys, you can use Ctrl+F for forward, Ctrl+B for backward. Ctrl+U for up and Ctrl+D works for down, but the amount of scrolling is different. The other way to move around is using the the hjkl keys. They move left, down, up and right.
If you want to insert text before the cursor, use the i key. If you want to add text after the cursor use the a key. When you're done adding text, press the Esc key.
Thats enough to get started, but sooner or later you'll want to save your work. You can save with :w which means "write the file." If you want to quit use :q or use them together as :wq (as in write and quit.) If you can't save for some reason, you might need to use :w! (as in write the file, I really mean it!)
You messed up, use the u key to undo whatever you did wrong. If you did it all at once use a capital letter as in U to undo all the changes to that line.
If you messed up really bad, use the :e! to read in the original file and lose all the changes you've made.
If you want to save what you've done, but under a new name use :w newfilename to create/overwrite the new file.
Speaking of new files, use vim newfilename to start a new file.
Okay, once you've memorized all that, you need to learn what makes vim cool. Use 200G to go the 200th line. Use gg or 1G to go to the first line. Use G to go the last line. This may not sound that interesting, but if you do any programming you will get errors (or you're psycho.) When you get errors, it will usually tell you a number where it had a problem.
If you want to copy a line, use yy and then p to paste that line. You'll probably want to move around in between the copy and paste.
If you want to remove a line, you use dd instead and still use p for paste if you want plug it back in some where.
If you want to copy a word, you can use yw or delete it with dw. If you want to paste it.. well p of course.
What if you want to change a word, cw of course. To change everything to the end of the line you will need to know that $ means the end of the line. c$ will change to the end of the line.
Easy enough right? If you've got that memorized then you can use some cool stuff like marking lines with ma for mark this place with an a or mb for mark this place with a b. You can delete to point a with d`a or you can delete to line a with d'a instead. You can also move to those points if you leave off the d.
Other magic:
- Replacing with regular expresssions
:%s/something/somethingelse/gc
- Copying from one file into another
yy :next [Enter] p
- Manipulating files between markers
:a,b s/someinstance/somethingnew