Genotrance

Icon

Random thoughts, ideas and experiences

My Vim customization

In my previous post, I mentioned how I migrated back to Vim for my development environment. Vim is fantastic but the default settings may not suit your needs. Given that Vim does not provide a GUI based configuration mechanism, getting it to fit your needs will take a bit of tweaking .VIM files and scavenging Google for tips. This was part of the reason why I stuck to Eclipse for so long.

Anyway, I have finally customized Vim enough to use it full time. Below is a list of my customizations; perhaps it will be of use to you.

Vim Screenshot

No Cream with that

Cream is a really popular Vim customization. It demonstrates the flexibility of Vim with its extensive enhancements, convenient packaging and GUI based settings management. If only Vim packaged everything the way Cream does, customization would be so much easier.

I did try Cream prior to attempting my own customization. However, Cream does away with one very essential feature of Vim: Insert Mode. This is done with the spirit to make Vim easier for beginners. Cream does provide a trap door to access this mode but it is very limited and frustrating. After struggling with this for a few days, I got fed up with Cream and decided to start with Vim as a base and customize it to my needs instead of adjusting to Cream’s vision.

Installing Vim

I ended up using the base Vim installer packaged and distributed by Cream. You can find the latest release here. You can also use AppSnap to install Vim for you. It will be much faster if nothing else.

Vim will install by default to C:\Program Files\Vim. There will be a _vimrc in that directory with all the binaries in a sub-directory. All settings in this _vimrc will get loaded by Vim on startup so all the following customizations will go there.

GUI customizations

The GUI allows for customization of various aspects with a simple setting. I have opted for the menu bar and tab bar and disabled all other enhancements. The following lines need to be added to _vimrc in order to achieve this:

set guioptions=em
set showtabline=2

Setting ‘e’ in guioptions ensures that the tab bar is displayed by the GUI. Setting showtabline to 2 further ensures that the tab bar is always displayed, even if there is only one file open. Setting ‘m’ in guioptions ensures that the menu bar is displayed.

Refer to the Vim documentation for guioptions to see all the available options.

Tab navigation like Firefox

I am very comfortable with the tab navigation shortcuts in Firefox so it only made sense to do the same within Vim. This can be done with the following settings:

:nmap <C-S-tab> :tabprevious<cr>
:nmap <C-tab> :tabnext<cr>
:imap <C-S-tab> <ESC>:tabprevious<cr>i
:imap <C-tab> <ESC>:tabnext<cr>i
:nmap <C-t> :tabnew<cr>
:imap <C-t> <ESC>:tabnew<cr>i
:map <C-w> :tabclose<cr>

This maps Ctrl-Tab and Ctrl-Shift-Tab to navigate between tabs, Ctrl-T to create a new tab and Ctrl-W to close the current tab.

Custom Font

Sometime in the past, I looked for a decent programming font and found ProggyClean which, in my opinion, is very readable and pleasant. I’ve used ProggyClean with Eclipse for several years so Vim had to do the same. You can download and install the TTF fonts from this link. There are many variations of ProggyClean but I just stick to the base version.

The next thing that needs to be done is to get Vim to use this font by default. This can be done with the following line in _vimrc:

set gfn=ProggyCleanTT:h12:cANSI

ProggyClean is designed for and looks best at size 12. By the way, the default font in Vim is FixedSys so if you prefer that then you don’t need to change anything. The above syntax can also be used to set any other font of your preference as the default.

Color Scheme

When I was playing with Cream, I came across its Terminal color scheme. Since I really liked this scheme, I incorporated it into my customization. You can get it from here. Save it to C:\Program Files\Vim\vim71\colors\terminal.vim and add the following line in _vimrc:

colorscheme terminal

Vim (and Cream) ship with several other color schemes so if you prefer another one, you can set it as default as above instead.

Cream Statusline

In the above screenshot, you can see a status bar at the bottom. I copied that from Cream. Majority of the code was in one file but it used several common functions from other parts of Cream. I copied all those dependencies into a single file and got it to work. You can get the file from the .ZIP file further down in this post.

You could also re-create it based on the latest versions by copying cream-statusline.vim to the Vim plugin directory. Every time you startup Vim, it will complain with unresolved dependencies. Look for those functions in the Cream source and copy them one by one. Eventually, you will have a functioning status bar.

Maximize on startup

I like my windows to be maximized, especially my text editor, so I needed Vim to maximize on startup. This can be done easily with the following Vim script. Just download the .ZIP, extract the contents to the C:\Program Files\Vim\vim71\plugin directory and Vim will start maximizing on startup. It couldn’t get any easier.

Python Tabs

I code in Python extensively so I had to make sure that Vim interpreted tabs correctly. The settings below ensure that tabs are replaced by spaces and map to four spaces.

set tabstop=4
set shiftwidth=4
set expandtab
set softtabstop=4
set autoindent

SuperTab
No one can deny the convenience of tab completion. Vim has completion built in (Ctrl-P) but SuperTab is much prettier and cooler. This Vim script can be downloaded from here. Again, extract the .ZIP to the Vim plugin directory and the tab key will automatically invoke SuperTab.

Function taglist

It is not a feature I used very much in Eclipse, but a taglist helps to quickly get to a specific function in a source file. Vim Taglist provides this feature by leveraging Ctags. Just download the .ZIP for Vim Taglist and extract to the Vim plugin directory. Next, download Ctags for Windows and extract ctags.exe to C:\Program Files\Vim\vim71.

Next, add the following lines to _vimrc:

let Tlist_Sort_Type = “name”
:nmap <F3> :TlistToggle<cr>

The next time you are editing code, hit F3 and a taglist will show up. Clicking any item in the list will move the cursor to that item in the source. Press F3 again and the taglist will disappear. You can change the shortcut by changing the F3 mapping above.

File Explorer

Another script I have incorporated is a simple file explorer – VTreeExplorer. It is not quite as pretty as Eclipse but is very functional. I do not use this very much and prefer using Windows Explorer. However, it is worth having since it is quickly accessible within Vim. You can download it here. Extract the .ZIP to the plugin directory and add the following lines to _vimrc.

let treeExplVertical=1
let treeExplWinSize=35
let treeExplDirSort=1
let treeExplNoList=1
:nmap <F2> :VSTreeExplore<cr>

I have mapped F2 to open the explorer which can be changed per your convenience.

Miscellaneous customizations

Some other customizations I have are as follows:

set nobackup
set number
set nuw=6

The nobackup option prevents Vim from creating backup files. Otherwise, everytime a file changes, Vim will create a .BAK file with the original contents. I find this annoying since I have to manually delete these files.

The number option enables line numbers, an important piece of information for a programmer. The nuw option sets the line number width to 6 which makes it a little neater.

File Associations

The Cream installer does not add the ‘Edit with Vim’ right-click option, something I used all the time earlier. In addition, I wanted to open all files associated with Vim in a tab in an existing Vim session. All this can be achieved with the following registry changes. Just copy them to a .REG file and double-click the file.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Edit with Vim]
@=””

[HKEY_CLASSES_ROOT\*\shell\Edit with Vim\command]
@=”\”C:\\Program Files\\vim\\vim71\\gvim.exe\” -p –remote-tab-silent \”%1\” \”%*\””

[HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\open\command]
@=”\”C:\\Program Files\\vim\\vim71\\gvim.exe\” -p –remote-tab-silent \”%1\” \”%*\””

Summary

The post above goes through a bunch of very common Vim customizations. You can get all of them by downloading this .ZIP file and extracting it to your Vim directory. You can then double click the vim.reg file and set up what is described in the file associations section above. In my case, these simple changes made Vim a lot more usable and convenient. The real power of Vim is in knowing the advanced functionality that makes people rage wars.

Advertisements

Filed under: Tips

10 Responses

  1. stone says:

    Great post, Thank you, but I can’t find the zip file you are refering to..
    I’m especially interested in the cream status bar file 🙂

    //S

  2. genotrance says:

    Hey stone,
    Seems like I forgot to add a link to the file to the post. I’ve updated the summary section accordingly.

    Thanks!

  3. Casimir says:

    You wrote: “I did try Cream prior to attempting my own customization. However, Cream does away with one very essential feature of Vim: Insert Mode.”

    I would like to point out that technically this is not true. In “default mode” Cream doesn’t use same modes as (G)Vim.

    Cream has “expert mode” that has the same kind of modes -interface that basic (G)Vim has (Settings / Preferences / Expert Mode). Perhaps its a recent feature.

    More on Cream insert mode/’normal mode’ here:
    http://cream.sourceforge.net/faq.html#0205

    Cream is very nice, great effort and service, especially to beginning OS codewrighs. But it has some quirks ime, even crashes on XP sometimes:

    So personally I have taken the same path as you: basic Vim and lots of customization in .vimrc. I have mapped the basic ‘windows style’ shortcuts ctrl+z, x, c, v (undo/cut/copy/paste) for all modes, but otherwise I am using “vi commands” – and modes.

    I suppose moded interfaces aren’t really the best way of UI design.

    Very nice article. Thank you for sharing! I really enjoyed this. The customized status bar “ripoff” also much appreciated. 🙂

    Casimir Pohjanraito

  4. genotrance says:

    Hello Casimir,
    Glad this post helped you some.

    I did try out the expert mode in Cream but it was very quirky. It didn’t behave the same way as Vim by default. I tolerated it for a few days then decided that I’d rather have Vim adapt to me than have to tweak Cream.

    What I liked most about Cream was that it saved any setting changes I made in the GUI. That’s how Vim should have worked by default. I applaud Cream for that great improvement.

    Thanks.

  5. Casimir says:

    did you try: Settings > Preferences > Behaviour > Vim? Default behaviour is Cream, whatever that means.

    I guess Cream is very Vim in the sense that you will need to edit up a vimrc to make it for you :p

    Casimir Pohjanraito

  6. Simon says:

    A shortcut to maximizing your gvim window is to add the following to _vimrc:

    ” force full screen window on open
    au GUIEnter * simalt ~x

  7. Anoop Iyer says:

    Useful stuff. I use vim heavily but haven’t updated my vim customizations since 2002-ish. I didn’t even know about tabs. 🙂

  8. Vincent says:

    Thanks for the Cream statusline!

  9. Larry says:

    Thanks for this post, but I can’t download the .zip file. It displays ‘Page was not found’. Can you update the download address or just email it to me(larry530tang#gmail.com). Very appreciate!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: