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.

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 edit or change it, Windows 2000 and above 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.

WARNING: Editing your Windows registry may cause unintended side effects that render your system inoperable. Although this tip has worked in the past for some people, there is no guarantee that it will work for you. Use with caution, and at your own risk.

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.

User-specific associations assoc reg key

Registry entry for file extension association

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.

User-specific associations ftype reg key

Registry entry for filetype action

For other file types (htmlfile for example) you may not want to open the file in gvim when you double-click, but instead when you edit the file by right-clicking and choosing, "Edit". You can do this by replacing the "open" in the key path given above with "edit". For example, for the sourcecode file type just created, you would accomplish this using the HKEY_CURRENT_USER\Software\Classes\sourcecode\shell\edit\command key.

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 "". In a batch file, you also need to escape the '%' character with a second '%' character.

Here is a dump of my current batch-file script to set up my file associations, this section will need more attention. Probably move the script into a subpage. Should also mention creating and merging a reg file, which is probably easier. Associate vimprojects file with vim does this, but not in a user-specific manner.

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 HKCU\SOFTWARE\Classes\. /v "" /t REG_SZ /d "noext" /f
reg add HKCU\SOFTWARE\Classes\.asm /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.bash /v "" /t REG_SZ /d "unix" /f
reg add HKCU\SOFTWARE\Classes\.bin /v "" /t REG_SZ /d "binary" /f
reg add HKCU\SOFTWARE\Classes\.c /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.config /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.cpp /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.csh /v "" /t REG_SZ /d "unix" /f
reg add HKCU\SOFTWARE\Classes\.cshrc /v "" /t REG_SZ /d "unix" /f
reg add HKCU\SOFTWARE\Classes\.css /v "" /t REG_SZ /d "stylesheet" /f
reg add HKCU\SOFTWARE\Classes\.dic /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.err /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.exc /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.h /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.hex /v "" /t REG_SZ /d "binary" /f
reg add HKCU\SOFTWARE\Classes\.hpp /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.inf /v "" /t REG_SZ /d "inffile" /f
reg add HKCU\SOFTWARE\Classes\.ini /v "" /t REG_SZ /d "inifile" /f
reg add HKCU\SOFTWARE\Classes\.java /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.lib /v "" /t REG_SZ /d "binary" /f
reg add HKCU\SOFTWARE\Classes\.log /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.m /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.py /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.s /v "" /t REG_SZ /d "sourcecode" /f
reg add HKCU\SOFTWARE\Classes\.scp /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.sct /v "" /t REG_SZ /d "scriptletfile" /f
reg add HKCU\SOFTWARE\Classes\.text /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.txt /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.vim /v "" /t REG_SZ /d "vimscript" /f
reg add HKCU\SOFTWARE\Classes\.vimrc /v "" /t REG_SZ /d "vimscript" /f
reg add HKCU\SOFTWARE\Classes\.wsc /v "" /t REG_SZ /d "scriptletfile" /f
reg add HKCU\SOFTWARE\Classes\.wtx /v "" /t REG_SZ /d "txtfile" /f
reg add HKCU\SOFTWARE\Classes\.xml /v "" /t REG_SZ /d "xmlfile" /f
echo.
echo Registering file types to launch in Vim...
echo.
reg add HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\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 HKCU\Environment /v EDITOR)
reg add HKCU\Environment /v EDITOR /t REG_SZ /d "gvim -f"
echo.
pause
echo on

Comments

Advertisement