If you’ve been following this blog or using Ubuntu for any amount of time I’m sure you’ve had to get your hands a bit dirty on the command line and use a text editor of some sort. Everyone seems to have their favorite. Some people prefer nano, maybe pico and the old-school peeps will always tell you about vi/vim. Why vim? You might say vim is the MacGyver of text editors. With a few commands hacked together you can do *anything* with your text file. Perhaps even blow up your machine!
For those of you that have avoided vim in the past due to its fairly steep initial learning curve I thought I would outline some of the key commands and shortcuts for using it. I’m sure I will end up leaving a few out so if you have any additional tips on using vim please pipe in with a comment.
h,j,k,l : move cursor left, up, down, right within the file. arrow keys will do the same.
w,b : move forward or backward one word at a time. faster than one space at a time!
^,$ : move to the beginning or end of the current line.
gg,G : move to the beginning or end of the current file.
:number : jumps to a specific line number.
/keyword : keyword search within the document. n for next.
Now these are all commands to navigate your way around the file but what about actually editing the file? First use the i command to change to insert mode or a for append mode. Either will let you start adding or removing text from the file. Press escape to drop back out of this mode once you’ve entered.
If you want to do any copy/paste magic within vim you’ll need to change into visual mode. You use this, surprisingly, with the v key. Once you are in visual mode you’ll notice that as you move your cursor it will highlight the text and characters with it. Once you’ve highlighted the text you want to copy press the y key to “yank” the text and then use p to paste it after you’ve set your cursor in the proper position. Also, if you know you want to copy full lines of text you’ll want to switch to visual mode using the uppercase V and use the same method afterwards. (can anyone offer insight into why these aren’t he more standard x,c,v for cut, copy & paste?)
A few more things. Have you ever screwed up part of a text file while you were editing? Never? Really? Well I do it all the time and that’s why God invented undo. In vim simply press u while you’re in normal mode (press escape to always exit to normal mode) and you have unlimited undo power on your document.
Now after you’ve navigated your way through your text, done some copy pasting and put together your Pulitzer prize winning novel (all using vim, of course!) you’ll want to save it. To save something within vim you’ll need to switch to command mode using the :.
Once you’re in command mode you can use :w to write the file, :q to quit the file or a combination of the two, :wq to write and then quit the file.
That should be enough for today. Tomorrow I’ll have some more about find / replace within vim and where you can find out more. You can also find more here about vim’s sweet moves.