Welcome to TheGillis.net

Consider this site a collection of random notes about a variety of topics. I hope this information helps you in some way.

7 April 2005 - 0:33gVim and the .vimrc File

I really like using Vim in FreeBSD, so when I became more than proficient in it, I decided that I needed a version for Windows. For this I had two realistic options. First, I could use the text based version in Cygwin. For those that are unfamiliar, Cygwin is a Unix implementation that sits on Windows. Although this is very possible, it doesn’t offer the benefit of being graphical and won’t respond to mouse input. The second option is the best, it uses a Windows implementation of Vim called gVim. It has the benefits of using the mouse, copies to the Windows clipboard, and has the option of writing file EOL types in either Windows or Unix format. This works very well, and I have used it for a while, but it had one funny side effect. Any file that was modified with gVim, will automatically create a backup file named ~filename. This started to become very irritating because with almost all files, a backup file is not needed. Using the settings window, I’ve discovered that there is a set nobk option. This works well for the current file, but it resets the option when gVim is restarted. This describes the easiest way to go about fixing the problem while keeping the nice gVim features such as Ctrl+c copying to clipboard. It also points out the .vimrc file for gVim.

Solution One - The .vimrc File

In the vim port on FreeBSD, the .vimrc file is located in ~/.vimrc . This should not a surprise. In gVim however, this is not the case. The .vimrc file instead is located in C:\Documents and Settings\Username\_vimrc and is not even there by default. The .vimrc file is a script file that contains vim commands to execute on startup. These are global settings such as tab spacing. We can solve our first problem by creating the C:\Documents and Settings\Username\_vimrc file containing the following:

set nobk

Save and close ( or : x as the case may be ), and restart gVim. You will notice there will no longer be any backup files written.

Problem Two - No More Windows Shortcuts

After using my new setup for a while, I noticed that things had changed for some reason. My biggest shock was when I used my Ctrl+v to paste as I had become used to doing, I was prompted with the ^ symbol. In normal default vim settings, the Ctrl+v character is used to type special characters that can’t be put in with the keyboard. This begins problem number two. How to get my nice character mappings back.

Solution Two - The Default .vimrc File

It turns out that there is a default .vimrc file that is used when the user does not have one in their home directory. The default is located in C:\Program Files\Vim\_vimrc . The contents are:

set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set diffexpr=MyDiff()
function MyDiff()
  let opt = ''
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  silent execute '!"C:\Program Files\Vim\vim62\diff" -a ' . opt .
      '"' . v:fname_in . '" "' . v:fname_new . '" > "' . v:fname_out . '"'
endfunction

The advanced syntax is beyond the scope of this article, but personally I do not have a use for the MyDiff() function. The two source lines mean that it includes those two files. Reading through these files, we see that this is where the good stuff sits. The example line for paste is found in C:\Program Files\Vim\vim62\mswin.vim line 106 with the lines:

" CTRL-V and SHIFT-Insert are Paste
map <C-V>		"+gP
map <S-Insert>		"+gP

Since that whole file contains nice maps for things, we can simply copy the good stuff from the default .vimrc file. This gives us a final .vimrc file of:

set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set nobk

Again, don’t forget the set nobk since that was our original problem.

Conclusion

The other thing to think about is that this change should be made in the user’s .vimrc file. If you add the line to the default .vimrc file, then it runs the risk of being overwritten by future upgrades. It also forces each person to not have backups of files if that is what they actually want. If the behavior is something you want to default, consider placing in C:\Documents and Settings\Default User .

As usual it is always nice to discover new vim commands, so here is the quick reference card I’ve found to be the most useful.

Although vim behaves slightly different from its FreeBSD counterpart, the blend of classic vim power combined with the graceful integration of Windows make it a great tool.

No Comments | Tags: Computer

Add a Comment