I freaking love Vim. And yes, I’ve used Emacs in the past (mostly as the recommended editor for a class I took last year – did that for a few days and then switched back to Vim while in terminal or opened up the SSH directory locally and used Gedit. (Also, Gedit is almost certainly the best and most powerful built-in GUI editor you will ever come across. With plugin support, it’s incredibly extensible. It was my favorite editor for a very, very long time too.)
Obviously, Vim’s power comes in its insane customization options. Basically every conceivable thing you’d ever want to do on a text file can be done with a collection of cryptic characters which become not-so-cryptic as you begin to understand the language of Vim. Seriously, hacking and playing with this editor is also as fun as actually using it to code.
But of course, as with all good things in the world, there are some annoyances. Not so much in Vim as in Ubuntu. Hey I love Ubuntu and use it almost exclusively, but it’s kinda boring that it’s always so late to get updates into the main packages. Of course, that’s the reason behind PPAs in the first place, but still.. Anyway, as you may have heard, Vim 7.3 was finally released a few weeks back and of course I wanted to play with it. Basically the thing I wanted most was CursorColumn, which introduced a vertical column at whatever character width(s) you wanted. This is really useful at times when you have code formatting rules such as an 80-character limit on lines. RelativeNumber, which displays how many lines away other lines are from your current line instead of a fixed numbering system starting at the beginning of the file, is also a cool feature sometimes on certain motions (e.g.: copy next N lines – which was a pain before). And file encryption was the last big feature in 7.3, but that’s something I’ll likely never use (at least from within Vim).
So I decided I couldn’t wait until Ubuntu brought 7.3 into it’s main packages (apparently it’s not happening until 11.04 😦 ), so I figured I’d compile it myself. This I merrily did and modified
/usr/local/bin/vim to point to the newly compiled executable. And this worked great for a while.. until I realized I was rather naive when I did that and didn’t include all the nice flags that would have enabled such things as Python support in use with certain plugins. So I tried to include stuff that I wanted and recompiled except I kept realizing there was more stuff I wanted. This went on for a while until one day I discovered a fully functional PPA for 7.3 😀 (note, I really wanted the latest release on the day it was announced, so there weren’t any PPA or straight up debs that I could use on the first day that I came across). Win! For the record, here’s the PPA in question. Okay so after finding Software Sources in Ubuntu 10.10 (wow, in Software Center until Edit and inaccessible through menus? grr..), I updated and languished in Vim glory. Very gratifying indeed.
In any case, I just got that done and now I am happy. Also, the past few days of looking through Vim help and learning random stuff from the Internet (did you know that “K” over a standard C function will open up the man-page for it?!), I arrived at my
.vimrc file below:
""" Basic Configurations set autochdir set autoindent set autowrite set bs=2 set colorcolumn=80 set cursorline set expandtab set foldmethod=syntax set foldminlines=3 set hlsearch set laststatus=2 set mouse=a set nocompatible set number set ruler set shiftwidth=4 set showcmd set showfulltag set showmode set smartindent set tabstop=4 set wildmenu set wildmode=longest,full set wrap set whichwrap+=h,l,<,> """ Options filetype on syn on au FocusLost * :wa """ Python options autocmd BufRead *.py cinwords=if,elif,else,for,while,with,try,except,finally,def,class """ Hard tabs in Makefiles autocmd FileType make setlocal noexpandtab """ Theme options set background=dark set t_Co=256 colors mustang """ Misc mappings and configs cmap w!! %!sudo tee > /dev/null % imap <c-c> <Esc> imap <F1> <Esc> imap jj <Esc> nmap <leader> <space> nnoremap <silent> <C-Left> :tabprevious<CR> nnoremap <silent> <C-Right> :tabnext<CR> nnoremap <silent> <C-t> :tabnew<CR> nnoremap ' ` vmap < <gv vmap > >gv
Some notes here:
- Note that the first several lines under Basic Configuration are sorted alphabetically. There’s really no reason whatsoever for this except that I was far too amused that I could do this automatically. Basically you just do
:set rnufor relative numbers. Then point your cursor to the first line, do
N Shift-V, where N-1 is how many lines you want to select (after getting into Linewise Visual Mode), then
:!sortwhich takes in the selected lines as input, uses the
:!to execute shell commands, and of course the shell program
sortto actually do the sort. I also realized this is possible by issuing
N!!sortwhere N-1 is again how many lines I want to sort. The
!!feeds in selected lines into the shell program and then the result is read back. Oh yeah and you can also do this with
N Shift-V v_!sort. Basically, the point is that Vim is powerful.
- Be sure to do
set mouse=aif you want to enjoy the ability to switch tabs using a freaking mouse inside the terminal. Cool, though I rarely use it. Note that if you do this, it will override the built-in Copy/Paste functionality and I can’t seem to figure out how to directly copy text from within the terminal to the OS (or application, if on Ubuntu) copy buffer.
- The options for
set wildmode=longest,fullare the coolest things ever. So basically when you are entering commands, it will suggest completions on and then will iterate through the completions with each subsequent . Also, reverses through the list. This mimics the behavior of standard Bash’s completions and is thus extremely intuitive for me.
- Another cool command is
au FocusLost * :wa. As described in the Vim wiki, this basically will Write All files when the focus is lost. Very useful in my opinion.
- The option
set t_Co=256enables colorful magic on XTerm terminals (and others). So instead of the usual 8 colors for syntax highlighting, you now get 256. Color-schemes look better, roses smell fresher, etc.
- The line
cmap w!! %!sudo tee > /dev/null %is an interesting hack that allows you to write files for which you don’t have permission when opened. It’s a pain to open a file, edit it, and then realize you don’t have permissions to write it. Sloppy approaches include writing the file to a temporary location and then copying back with permission, but that’s not nice. (One pre-emptive indicator would be to have a [readonly] flag in the statusline.) A solution is the above hack, which allows you to gain temporary super-user access and then write the file to it. Handy. This is a pretty popular hack found all over the Internet, so I’m not sure who to credit it too. I think the first place I saw it was on Command-Fu.
- Yeah, so map
Esc. Just do it. You’ll see how much time you save and it will shock you incredibly causing unbounded, unexplainable glee. YMMV.
- Finally, the last two lines which map < and > to <gv and >gv. This restores your selection after indenting or un-indenting. In my opinion, this should be the default behavior.
- Finally, my Color Scheme is Mustang, slightly modified. I may post that up later. It’s an extremely awesome theme and my second most favorite one is Vitamin, also from the same guy. Both very cool.