Config files for the Windows version of VIM

Written by David Craddock on January 10th, 2010

Today I encountered problems configuring the windows version of the popular text editor VIM, so I thought I’d write up a quick post talk about configuration files under the Windows version, if anyone becomes stuck like I did. I use Linux, OSX and Windows on a day-to-day basis, and VIM as a text editor for a lot of quick edits on all three platforms. Here’s a quick comparison:

Linux

Linux is easy because that’s what most people who use VIM run, and so it is very well tested.

~/.vimrc – Configuration file for command line vim.
~/.gvimrc – Configuration file for gui vim.

OSX

OSX is simple also, as it’s based on unix:

~/.vimrc – Configuration file for command line vim.
~/.gvimrc – Configuration file for gui vim.

Windows

Windows is not easy at all.. it doesn’t have a unix file structure, and doesn’t have support for the unix hidden file names, that start with a ‘.’, ie: ‘.vimrc’, ‘.bashrc’, and so on. Most open-source programs like VIM that require these hidden configuration files, and have been ported over to windows, seem to adopt this naming convention: ‘_vimrc’, ‘_bashrc’.. and so forth. So:

_vimrc – Configuration file for command line vim.
_gvimrc – Configuration file for gui vim.

Renaming configuration files from “.” to “_” wouldn’t make much difference on its own. You’d have to rename your files, but.. big deal. It’s not much of a problem.

Another, more tricky, problem you may encounter however, is that there’s no clear home directory on windows systems. Each major incarnation of windows seems to have a slightly different way of dealing with user’s files.. from 2000 to XP, a change, from XP to Vista, there is a change. I haven’t tried VIM on W7 yet, but it seems similar to Vista in structure, so this information may actually be consistent to W7.

The Vista 64 version of VIM I have, looks in another place for configuration files. For a global configuration file, it looks in “C:\Program Files”. Yes.. “C:\Program Files”. According to Vista 64’s version of VIM.. that’s the exact directory where I installed VIM. This is clearly not right. What’s happening is that the file system on windows is different to the unix-type file systems, and the VIM port is having problems adapting. The real VIM install directory is C:\Program Files\vim72. Because VIM is looking for a global configuration file in “C:\Program Files\_vimrc”, it’ll never find it.

Now you could override this with a batch file that sets the right environmental variables on startup, or you could change the environmental variables exported in windows, but I prefer to have a user-specified configuration file in my personal files directory, as it’s easier to backup and manage. If you wanted to specify the environmental variables yourself, which I’m guessing many will, the two environmental variables to override are:

$VIM = the VIM install directory, not always set properly, as I mentioned.
$HOME = the logged in user’s documents and settings directory, in windows speak this is also where the ‘user profile’ is stored, which is a collection of settings and configurations for the user. The exact directory will depend on which version of Windows you’re running, and if you override the HOME folder, you may have problems with other programs that rely on it being static.

On my Windows Vista 64 install:

$VIM = “C:\Program Files”
$HOME = “C:\Users\Dave”

You can see what files VIM includes by running the handy command

vim -V

at a command prompt; it will go through the different settings and output something similar to this:

Searching for "C:\Users\Dave/vimfiles\filetype.vim"
Searching for "C:\Program Files/vimfiles\filetype.vim"
Searching for "C:\Program Files\vim72\filetype.vim"
line 49: sourcing "C:\Program Files\vim72\filetype.vim"
finished sourcing C:\Program Files\vim72\filetype.vim
continuing in C:\Users\Dave\_vimrc
Searching for "C:\Program Files/vimfiles/after\filetype.vim"
Searching for "C:\Users\Dave/vimfiles/after\filetype.vim"
Searching for "ftplugin.vim" in "C:\Users\Dave/vimfiles,C:\Program Files/vimfiles,C:\Program Files\vim72,C:\Program Files/vimfiles/after,C:\Users\Dave/vimfiles/after"
Searching for "C:\Users\Dave/vimfiles\ftplugin.vim"
Searching for "C:\Program Files/vimfiles\ftplugin.vim"
Searching for "C:\Program Files\vim72\ftplugin.vim"
line 49: sourcing "C:\Program Files\vim72\ftplugin.vim"
finished sourcing C:\Program Files\vim72\ftplugin.vim
continuing in C:\Users\Dave\_vimrc
Searching for "C:\Program Files/vimfiles/after\ftplugin.vim"
Searching for "C:\Users\Dave/vimfiles/after\ftplugin.vim"
finished sourcing $HOME\_vimrc
Searching for "plugin/**/*.vim" in "C:\Users\Dave/vimfiles,C:\Program Files/vimfiles,C:\Program Files\vim72,C:\Program Files/vimfiles/after,C:\Users\Dave/vimfiles/after"
Searching for "C:\Users\Dave/vimfiles\plugin/**/*.vim"
Searching for "C:\Program Files/vimfiles\plugin/**/*.vim"
Searching for "C:\Program Files\vim72\plugin/**/*.vim"
sourcing "C:\Program Files\vim72\plugin\getscriptPlugin.vim"
finished sourcing C:\Program Files\vim72\plugin\getscriptPlugin.vim
sourcing "C:\Program Files\vim72\plugin\gzip.vim"
finished sourcing C:\Program Files\vim72\plugin\gzip.vim
sourcing "C:\Program Files\vim72\plugin\matchparen.vim"
finished sourcing C:\Program Files\vim72\plugin\matchparen.vim
sourcing "C:\Program Files\vim72\plugin\netrwPlugin.vim"
finished sourcing C:\Program Files\vim72\plugin\netrwPlugin.vim
sourcing "C:\Program Files\vim72\plugin\rrhelper.vim"
finished sourcing C:\Program Files\vim72\plugin\rrhelper.vim
sourcing "C:\Program Files\vim72\plugin\spellfile.vim"
finished sourcing C:\Program Files\vim72\plugin\spellfile.vim
sourcing "C:\Program Files\vim72\plugin\tarPlugin.vim"
finished sourcing C:\Program Files\vim72\plugin\tarPlugin.vim
sourcing "C:\Program Files\vim72\plugin\tohtml.vim"
finished sourcing C:\Program Files\vim72\plugin\tohtml.vim
sourcing "C:\Program Files\vim72\plugin\vimballPlugin.vim"
finished sourcing C:\Program Files\vim72\plugin\vimballPlugin.vim
sourcing "C:\Program Files\vim72\plugin\zipPlugin.vim"
finished sourcing C:\Program Files\vim72\plugin\zipPlugin.vim
Searching for "C:\Program Files/vimfiles/after\plugin/**/*.vim"
Searching for "C:\Users\Dave/vimfiles/after\plugin/**/*.vim"
Reading viminfo file "C:\Users\Dave\_viminfo" info
Press ENTER or type command to continue

Notice how it does pull in all the syntax highlighting macros and other extension files correctly, which are specified in the .vim files above.. but it doesn’t pull in the global configuration files that I’ve copied also to C:\Program Files\vim72\_gvimrc and C:\Program Files\vim72\_vimrc. However, it does pickup the files I copied to C:\Users\Dave.. both the C:\Users\Dave\_vimrc and C:\Users\Dave\_gvimrc are picked up, although VIM will normally read ‘_gvimrc’ when the gui version of VIM is run (called gvim).

To see exactly what those environmental variables are being set to, when you’re inside the editor, issue these two commands, and their values will be show in the editor:

:echo $HOME
:echo $VIM

It seems to make sense for me – and perhaps you, if you’re working with VIM on windows – to place my _vimrc and _gvimrc files configuration files in $HOME in Vista. They are then picked up without having to worry about explicitly defining any environmental variables, creating a batch file, or any other hassle.

You can do this easily by the following two commands:

:ed $HOME\_vimrc
:sp $HOME\_gvimrc

That will open the two new configuration files, side by side, and you can paste in your existing configuration that you’ve used in Linux, and windows will pick them up the next time you start VIM.

 

oDesk and Work

Written by David Craddock on January 3rd, 2010

I’ve been so busy working lately, I’ve hardly had time to update this website. On top of other things, I’ve just started freelancing as a contractor on oDesk – which actually seems quite a good way of getting paid for working on projects at home. Here is my current oDesk profile:



It is a huge community, and I highly recommend the site to those who wish to work from home. You will become part of the global IT workforce, so you may have to lower your rates, but if you’re good at what you do, then you can easily earn a modest living from anywhere. Someone I know is working via oDesk while traveling around Asia, for example.

 

Speed Climbing

Written by David Craddock on October 17th, 2009

I’m going to take a break from the purely technical posts to get more people to see this video. Here the late Dan Osman speed-climbs a cliff without any safety equipment.

Risky? Just a bit. A few years later he lost his life during a similar attempt. But no-one can doubt the guts required, and the level of skill he achieved, in this type of climbing.

 

Regex in VIM.. simple

Written by David Craddock on October 7th, 2009

There are more than a gazillion ways to use regexs. I am sure they are each very useful for their own subset of problems. The sheer variety can be highly confusing and scary for a lot of people though, and you only need to use a few approaches to accomplish most text-editing tasks.

Here is a simple method for using regex in the powerful text editor VIM that will work well for common use.

Method

We are going to take the “search and delete a word” problem for an example. We want to delete all instances of the singular noun “needle” in a text file. Let’s assume there are no instances of the pluralisation “needles” in our document.

  1. Debug on.. turn some VIM options on
    :set hlsearch
    :set wrapscan
    – this will make all regex expressions possible to debug by visually showing what they match in your document (first line) and make all searches wrap around instead of just search forward from your current position, which is the default. (second line)
  2. Develop and Test.. your regex attempts by using a simple search. Here we see three attempts at solving the problem: :/needl
    :/needle
    :/\<needle\>
    – our third try is correct, and highlights all words that spell “needle”. The \< and \> markers allow you to specify the beginning and the end of a word. Play with different regexs using the simple search and watching what is highlighted, until you discover one that works for you.

  3. Run… your regex:%s/\<needle\>//g – once you’ve figured out a regex, run the regex on your document. This example will execute a search for the word “needle” and delete every one. If you wanted to substitute needle for another word, you would put the word in between the // marks. As we can see, there is nothing between the marks in this example, so it will replace instances of “needle” with nothing. This means it will serve to delete every instance of the word “needle”.
  4. Check things are OK… with your document :/\<needle\>
    :/needle
    :/needl
    – has the regex done what you want? Use the search function to see if regex has done what you wanted it to do. The above examples show different searches through the document to see if different variations remain. Any matches of these searches will highlight any problems. You can use the lower-case N(next search result) and lower-case P(previous search result) commands to navigate through any found search results. You must remember to manually look through the document and see what the regex has changed, make sure there aren’t any unwanted surprises!
  5. Recover… from any mistakes u – just press the U key (with no capslock or shift). This will undo the very last last change made to the document.
  6. Redo… any work that you need to <ctrl>-r – use the redo fuction; press the CONTROL and R keys together (with no capslock or shift). This will redo the last change made to the document.
  7. Finish up and Write… to file :w – write your work on the document to file. Even after you have written out to file, you can probably still use the undo function to get back to where you were, but it’s best practice to not rely on this, and only write once you’re done.
  8. Debug off.. turn some options off
    :set nohlsearch
    :set nowrapscan
    – turn off the regular expression highlighting (line 1). turn off the wraparound searching (line 2). You can leave either or both options on if you want, they’re often useful. Up to you.

Use a combination of these wonderful commands to test and improve your regex development skills in VIM.

Examples

Here I use the shorthand “#…” to denote comments on what I’m doing… if you want to copy and paste the example as written, then you will have to remove those comments.

1. Remove ancient uppercase <BLINK> tags from a document.

:set wrapscan # debug on
:set hlsearch # debug on
:/<BLINK> # try 1.. bingo! first time.. selected all tags I want
:%s/<BLINK>//g # lets execute my regex remove
:/BLINK # check 1.. testing things are OK in my file by searching through..
:/blinked # check 2.. yep thats ok..
:/<BLINK\> # check 3.. yep looks ok... the problem tags are gone
# ...manual scroll through the document.. looks good!
:w # write out to file
:set nohlsearch # debug off
:set nowrapscan # debug off

2. Oh no! We missed some lower and mixedcase <bLiNK> tags that some sneaky person slipped in. Let’s take them out.

:set wrapscan # debug on
:set hlsearch # debug on
:/<blink> # try 1.. hm.. worked for many, but didnt match BlInK or blINK mixedcase
:/<blink>/i # try 2.. much better.. seems to have worked!
:%s/<blink>//i # lets execute my regex remove
:/BLINK # check 1.. testing things are OK in my file by searching through..
:/blinked # check 2.. yep thats ok..
:/<blink> # check 3.. yep thats fine.
:/<blink>/i # check 4.. looks good... problem solved
# ...manual scroll through the document.. looks much better!
:w # write out to file
:set nohlsearch # debug off
:set nowrapscan # debug off

3. Replacing uppercase or mixedcase <BR> tags with the more modern <br>.

:set wrapscan # debug on
:set hlsearch # debug on
:/<BR> # try 1.. hmm.. just uppercase.. not gonna work..
:/<br> # try 2.. hmm.. just lowercase..
:/<BR>/i # try 3.. ahh.. that'll be it then
:%s/<BR>/<br>/gi # lets execute my regex substitution
:/BR # check 1.. testing things are OK in my file by searching through..
:/br # check 2.. yep thats ok..
/bR # check 3 ..yup..
:/<BR>/i # check 4.. yep looks ok... the problem tags seem to be gone
# ...manual scroll through the document.. looks good!
:w # write out to file
:set nohlsearch # debug off
:set nowrapscan # debug off

For More..

Regexs are the gift that just keeps on giving. Here are some good resources on regexs in general, and regexs in VIM.

 

Heart rate monitor now available for the XO Laptop

Written by David Craddock on April 7th, 2009

Volunteers on the OLPC project have developed a low-cost heart rate monitor that can be plugged into the XO laptop, making steps towards an affordable monitoring system for hospitals in developing countries.

For more information on the development, see the: OLPC Goldenstate project. People in the US can currently buy the sensor here; unfortunately there is no international shipping yet in place.