Vim Tips Wiki
Advertisement

Eventually I hope to make this a tip, but it's certainly more of a Windows how-to than anything to do with Vim. It's to solve a problem I stumbled upon at work because I'm one of very few that actually use Vim, however, so maybe it's relevant.

Please feel free to edit/comment as you see fit even though this is a user page for now.

Comments

I need to determine which versions of Windows this actually applies to. I think Vista may default to user-specific settings. --Fritzophrenic 15:30, 17 June 2009 (UTC)

User-specific Windows file associations

Windows file associations (which allow you to double-click on a file and have it open in your chosen application) can be set in several ways, including command-line tools such as ftype and assoc, the "Open with..." menu, and hidden in the "Folder Options" on Windows XP. Unfortunately, all of these tools apply your associations system-wide. This means that if you are on a multi-user system, and you are the only user using Vim, other people will get very annoyed if you use these tools to make files launch in Vim.

Luckily, although Windows does not provide a GUI or even easy command-line tools to do edit or change it, recent versions of Windows provide a place in the registry that can be used to store user-specific file associations. This place is HKEY_CURRENT_USER\Software\Classes. Creating a file association here uses two steps. For an example, we will associate the ".c" extension to the "sourcecode" filetype, and then tell Windows to launch anything in the "sourcecode" filetype in gvim version 7.2. It should be easy to adjust this as desired.

Step 1: associate the file extension with a file type

To do this, you need only create a registry key with the name of the file extension desired and a default value of the filetype you want to associate with it. For our example, you would create the key, HKEY_CURRENT_USER\Software\Classes\.c (using regedit or the command-line "reg add" command). In regedit, you can see a (Default) value for this key after creating it. Set the value to sourcecode.

Step 2: associate the filetype with gvim

The next step is to tell Windows what to do with your new filetype. You use the same registry area for this, but it's a little more complex.

First, create the key HKEY_CURRENT_USER\Software\Classes\sourcecode\shell\open\command. This key will also have a (Default) value, which you need to set to the program used to open files of this type. For example, you could set it to "C:\Program Files\Vim\vim72\gvim.exe" --remote-tab-silent "%1" to open .c files (and other sourcecode typed files) in a new tab in the default gvim.

For other file types (htmlfile for example) you may not want to open gvim to OPEN the file (on a double-click) but instead when you EDIT the file (using right-click->Edit). You can do this by replacing the "open" in the key path given above with "edit".

Scripting the additions

Windows does provide a command-line utility for editing the registry. Namely, "reg add". You do need to escape double-quote characters with a backslash (which is weird, the usual Windows escape character is "^"...I plan to investigate eventually). To set the (Default) value we need, provide an empty string as the value name using /v "". Here is a dump of my current batch-file script to set up my file associations, this section will need more attention. Should also mention creating and merging a reg file, which is probably easier. I think we may already have a tip that does this, in fact.

I should note that I use Improved Hex editing, or the binary filetype may not make much sense.

:: Sets up file associations for Vim
:: WILL NOT ASK PERMISSION! So, be sure to peruse the list below before running.
@echo off
echo.
echo Associating file extensions with file types...
echo.
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\. /v "" /t REG_SZ /d "noext" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.asm /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.bash /v "" /t REG_SZ /d "unix" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.bin /v "" /t REG_SZ /d "binary" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.c /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.config /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.cpp /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.csh /v "" /t REG_SZ /d "unix" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.cshrc /v "" /t REG_SZ /d "unix" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.css /v "" /t REG_SZ /d "stylesheet" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.dic /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.err /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.exc /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.h /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.hex /v "" /t REG_SZ /d "binary" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.hpp /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.inf /v "" /t REG_SZ /d "inffile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.ini /v "" /t REG_SZ /d "inifile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.java /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.lib /v "" /t REG_SZ /d "binary" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.log /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.m /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.py /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.s /v "" /t REG_SZ /d "sourcecode" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.scp /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.sct /v "" /t REG_SZ /d "scriptletfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.text /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.txt /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.vim /v "" /t REG_SZ /d "vimscript" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.vimrc /v "" /t REG_SZ /d "vimscript" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.wsc /v "" /t REG_SZ /d "scriptletfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.wtx /v "" /t REG_SZ /d "txtfile" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\.xml /v "" /t REG_SZ /d "xmlfile" /f
echo.
echo Registering file types to launch in Vim...
echo.
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\binary\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" -b -c \"set binary\" --servername HEXVIM --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\inffile\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --servername SYSCONFIG --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\inifile\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --servername SYSCONFIG --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\noext\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\scriptletfile\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\sourcecode\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\stylesheet\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --servername HTMLEDIT --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\txtfile\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\unix\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\vimscript\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --servername VIMCONFIG --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\xmlfile\shell\open\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\html\shell\edit\command /v "" /t REG_SZ /d "\"C:\Program Files\Vim\vim72\gvim.exe\" --remote-tab-silent \"%%1\"" /f
echo.
echo Setting EDITOR environment variable...
echo.
::Query for already existing env variable, but hide the output
reg query HKEY_CURRENT_USER\Environment /v EDITOR 2>NUL 1>NUL
::If the env var exists, display its value
if %ERRORLEVEL%==0 (
    echo EDITOR environment variable already defined:
    reg query HKEY_CURRENT_USER\Environment /v EDITOR)
reg add HKEY_CURRENT_USER\Environment /v EDITOR /t REG_SZ /d "gvim -f"
echo.
pause
echo on
Advertisement