Vim the MacGyver of text editors : Ubuntu (6.06.1 / 6.10)

By | 2006/12/23

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.

9 thoughts on “Vim the MacGyver of text editors : Ubuntu (6.06.1 / 6.10)

  1. Marius Gedminas

    You can run ‘vimtutor’ for an interactive tutorial of basic vim’s commands.

    And then there’s the extreme power that vim has as a text editor. I probably like keyword completion best. Press Ctrl+N or Ctrl+P in insert mode and vim will try to complete the word you’ve started entering, by looking into all the files you have in vim, the tags file if you’ve built one, or include files if you happen to edit C source code. There are other kinds of completion as well, e.g. file name completion that you get by pressing Ctrl+X followed by Ctrl+F.

    :help is your friend.

  2. Ubuntu Tutorials

    Marius – tomorrow’s post mentions vimtutor a bit (I was getting there) 🙂

    thanks for the tips about keyword completion, I didn’t know those actually. Vim is super powerful and even in two posts I’m sure I’m only getting to the surface of things.

  3. Sebastian

    Thanks for the short tutorial; I have been using Linux for years now (thx warty), but never liked vi(m) or emacs… nano is not powerful enough though. With this short posting I got around trying some stuff though… maybe I’ll use vim more often now. 😀

  4. Pingback: How To pwn Your Text Files With Vim : Ubuntu Tutorials : Breezy - Dapper - Edgy - Feisty

  5. Pingback: Ubuntu | Christer Edwards: How To pwn Your Text Files With Vim

  6. anon

    Another quit + save command is 😡
    Also you can jump to a line number by pressing, say, 41G in order to get to ln 41. Not as visual as :41 but does the same thing!

    Other good nav tools include H,M,L which will bring your cursor to the Top, Middle, and Bottom of the screen, respectively.

    And then there’s z to bring the current line as the top line of the terminal … etc. etc 🙂

    Gotta love vim.

  7. Pingback: Vim Tip of the Week : SHIFT-K : Ubuntu Tutorials : Breezy - Dapper - Edgy - Feisty

  8. JOHNNY LAKIU

    how to save vi and configure dns etc.

Comments are closed.