Sunday, August 12, 2007

Back to basics: Vi

I've been doing a lot of programming in Ubuntu, mostly in PHP (my sister's in law web site), Perl and Python. I tried a lot of editors and (so called) IDEs, but was never happy with any of them. So I went back to basics: using Vim.

Now, I can ear the screams of Emacs users and the cries of all of you who only learned the :q! shortcut to exit Vi as soon as possible, but here's my view on things:

Emacs users: I tried using Emacs. I really did. And I actually used it for a long time while I was in college. But after using Vi for a while I had a really hard time both remembering the shortcuts and configuring Emacs to behave like I wanted to. So I just stuck with Vi.

:q! users: Vi is not that hard. Really! It does need a bit getting used to, but the freedom of not using the mouse and all those small shortcuts that you think you'll never use but turn out to be extremely useful make up for the learning curve.

To speed up your learning curve, here are the two secrets to loving Vi:
  • A proper .vimrc file
  • Knowing the right commands
Because there are so many "right commands" I'll leave that to a later post. Let's focus on the .vimrc file for now. And just to get your expectations in the right place, I'm not a Vim power user. If you look the web for .vimrc files you'll find files with hundreds of lines of code. Mine has 25, including comments... But it makes Vim behave like I want, so it's enough. Just go through the code and comments and you'll be able to understand what the configurations do. If not, drop me a line.

NOTE: The last 2 lines are wrong! Instead of [ you should have <. And instead of ] you should have >. But Blogger does a really bad job with these 2 characters....
" Turn on syntax highlight
syntax on
" Set tabs to 4 in size and make sure autoindent is on
set tabstop=4
set autoindent
" Make backspace work as in most editors
set backspace=indent,eol,start
" Black background and white foreground
highlight Normal guibg=black
highlight Normal guifg=white
" Make the default window size 50x80
set lines=50
set columns=80
" Store temp and swap files in this directory
set directory=/tmp/
" Show the status line
set ls=2
" Set the status line to:
" Filename [Modifed & Readonly Status] line number/total lines column (#buffer)
" More info at:
" http://www.winterdom.com/weblog/CommentView,guid,3372fa1e-966e-4389-b2ba-bb2ce527f52a.aspx
let &stl="%f %([%R%M]%)%=%l/%L %c (#%n)"
" Map Ctrl-Ins and Shift-Ins to clipboard copy paste (FIX THIS!!!)
map [C-Insert] "+y
map [S-Insert] "+gP

1 comment:

Jabba Laci said...

I use Vim too, best editor ever. For the beginners we should mention that there is a fine and simple tutorial. If you use Linux, just type "vimtutor" in the terminal and follow the instructions.