Vim Tips Wiki
Register
(Adjust previous/next navigation)
(merge of my user-specific tip + cleanup/review/expansion of existing content)
Line 1: Line 1:
{{Duplicate|1440}}
 
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=1003
 
|id=1003
Line 13: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
Although the Vim installer gives you options to create right-click "edit with Vim" options, it is convenient to be able to simply double-click on a file to have it open in Vim. This "double-click to open" is commonly referred to as a "file association" and is fairly easy to set up in Microsoft Windows. You can use the "Open With..." menu in Windows to set up a basic association, but that will not allow you to specify any command-line arguments to Vim, such as <tt>--remote-silent</tt> to open with an existing Vim instance. In order to fully specify the action to take when double-clicking a file, Windows provides the <tt>ftype</tt> and <tt>assoc</tt> command-line tools.
You can enter the following at Windows command prompt to associate php files with gvim (change the command to suit your system):
 
  +
  +
To use, simply open a Windows command prompt and type something like the following:
   
 
<pre>
 
<pre>
 
assoc .php=PHPFile
 
assoc .php=PHPFile
ftype PHPFile="C:\Program Files\Vim\Vim63\gvim.exe" --remote "%1"
+
ftype PHPFile="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
 
</pre>
 
</pre>
   
Then whenever you double click a .php file in Explorer, it will be opened in existing gvim window (or it will open new gvim window if there is no already opened gvim window).
+
This particular command sequence will set up Windows to open any files with a .php extension in an existing gvim window (or it will open new gvim window if there is no already opened gvim window).
   
  +
In general, the <tt>assoc</tt> command is used to associate a given file extension with a filetype. The <tt>ftype</tt> command is used to tell Windows what to do to open files of a given file type. You can associate any number of file extensions with a given filetype, which makes it easy to set up a single action for similar types of files. For example, the following will treat any files with extensions .c, .h, .pl, or .py as "sourcecode", which it will launch in Vim as we did with PHPFile above:
==Notes==
 
*<tt>assoc</tt> is the command to associate an extension with a filetype in the registry.
 
*<tt>ftype</tt> is the command to set the action (program) that will be executed for a specific filetype.
 
* maybe better use --remote-silent instead of --remote
 
   
  +
<pre>
==Comments==
 
  +
assoc .c=sourcecode
I prefer the context menu that the Gvim installer gives you in a Windows install.
 
  +
assoc .h=sourcecode
*Edit with Vim
 
  +
assoc .pl=sourcecode
*Edit with Existing Vim - <file name>
 
  +
assoc .py=sourcecode
  +
ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
  +
</pre>
   
  +
==How it works==
will appear when you right-click on a file in Explorer.
 
  +
Both the <tt>ftype</tt> and <tt>assoc</tt> commands work by creating entries in a specific registry location which Windows will check for various actions taken on a file. The easiest and safest way to create these entries is to use the command-line tools, but it is possible to create them by hand.
   
  +
<tt>assoc .c=sourcecode</tt> will create a registry key at '''<tt>HKEY_CLASSES_ROOT\.c</tt>''' that has a <tt>(Default)</tt> value of <tt>sourcecode</tt>. When you double-click on a .c file, Windows will see this key and look for another key to figure out what to do with a <tt>sourcecode</tt> file.
In fact if you have more than one gvim window open, all of them will appear in the context menu and so you can choose which existing gvim session to open your file within.
 
   
  +
<tt>ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" "%1"</tt> will set up another registry entry, in '''<tt>HKEY_CLASSES_ROOT\sourcecode\Shell\Open\command</tt>''' with <tt>(Default)</tt> value of <tt>"C:\Program Files\Vim\vim72\gvim.exe" "%1"</tt>. This entry could do with more inspection.
----
 
This also works with Gnome and KDE. And on Mac it's the standard way to open files in Vim.
 
   
  +
==Associating Vim with other actions==
It's smart to open files "inside" one instance of VIM. All your actions are saved (like yanking, pasting or searching) - and you really don't have to keep the track on the windows (by using mouse or alt+tab). Using a plugin like minibufexpl you can keep track of the buffers open.Another gain is to use multiply desktops - for instance, I have Nautilus on one and VIM on another.
 
  +
Consider the .html file extension. For most users, it would not make sense to open a .html file in Vim when double-clicking the file. Most user would want the file to open in a web browser when double-clicking, but being able to automatically ''edit'' the file in Vim when modifying the file would be a nice thing to be able to do. This is where the registry entry created by <tt>ftype</tt> comes in.
   
  +
{{RegistryWarning}}
Check out: {{script|id=159|text=minibufexpl}}
 
   
  +
There are no easy command-line or GUI tools to do this, but you can set up other actions, such as "edit", in the same registry area that <tt>ftype</tt> uses. Simply replace the "open" portion of the registry key path with the desired action (in this case, "edit"). For example, assume that .html files have been associated with a filetype called <tt>htmlfile</tt>. Then, to keep the existing double-click action in place, but add Vim as the "edit" action, give the following registry key a <tt>(Default)</tt> value of <tt>"C:\Program Files\Vim\vim72\gvim.exe" "%1"</tt>: '''<tt>HKEY_CLASSES_ROOT\htmlfile\Shell\Edit\command</tt>'''
----
 
Maybe it is better to use --remote-silent instead of --remote. So you will not get a warning dialog, when opening a file, and no gvim is already running.
 
   
  +
[[File:Edit action file association reg key.PNG|frame|Registry entry for the "edit" action]]
----
 
Associating files with gvim --remote-silent works pretty well for me, except in the following scenario:
 
#There is a gvim instance already running.
 
#I select multiple files in Explorer and hit Enter.
 
   
  +
''Note: in Windows XP, there is a GUI for doing this, accessible through the "Folder Options" dialog.''
Some of the files that were selected will not be opened, they simply get "lost". It seems to work every time if I just select a single file. I figure it must be some sort of timing issue in the gvim server.
 
   
  +
Now, you can double-click on a .html file to open it in your web browser as normal. But you can also right-click on the file, and choose "Edit" from the context menu. This will open the file in gvim.
----
 
I tried your scenario, and all selected files opened in the already running instance of gvim.
 
   
  +
<!--Dirty hack to make sure the screenshot doesn't show up in the wrong section; fix later if possible--><div style="clear:both;">
----
 
  +
==User-specific file associations==
  +
Unfortunately, all of the methods mentioned above apply your file associations in an area of the registry used by every user account in your Windows installation. 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 <tt>HKEY_CURRENT_USER\Software\Classes</tt>. Creating a file association here uses two steps, which duplicate what the <tt>assoc</tt> and <tt>ftype</tt> commands do in their registry area. 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, [[Launch_files_in_new_tabs_under_Windows|opening the file in a new tab]]. It should be easy to adjust this as desired.
  +
  +
{{RegistryWarning}}
  +
  +
===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, '''<tt>HKEY_CURRENT_USER\Software\Classes\.c</tt>''' (using <tt>regedit</tt> or the [[#Scripting the additions|command-line "reg add" command]]). In regedit, you can see a (Default) value for this key after creating it. Set the value to <tt>sourcecode</tt>.
  +
  +
[[File:User-specific associations assoc reg key.PNG|frame|Registry entry for file extension association]]
  +
<!--Dirty hack to make sure the screenshot doesn't show up in the wrong section; fix later if possible--><div style="clear:both;">
  +
  +
===Step 2: associate the filetype with gvim===
  +
The next step is to tell Windows what to do with your new filetype, just like the <tt>ftype</tt> command.
  +
  +
First, create the key '''<tt>HKEY_CURRENT_USER\Software\Classes\sourcecode\shell\open\command</tt>'''. 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 <tt>"C:\Program Files\Vim\vim72\gvim.exe" --remote-tab-silent "%1"</tt> to open .c files (and other <tt>sourcecode</tt> typed files) in a new tab in the default gvim.
  +
  +
[[File:User-specific associations ftype reg key.PNG|frame|Registry entry for filetype action]]
  +
  +
Just as we explained above, you can specify [[#Associating Vim with other actions|other actions]] to take besides opening on a double-click, simply by replacing "open" with the appropriate action in the registry key path.
  +
<!--Dirty hack to make sure the screenshot doesn't show up in the wrong section; fix later if possible--><div style="clear:both;">
  +
  +
===Scripting the additions===
  +
Windows does provide a command-line utility for editing the registry: <tt>reg add</tt>. You can use this tool from a CMD prompt, but it is probably more useful to create a batch file containing commands to set ''all'' your desired file associations.
  +
  +
You need to escape double-quote characters with a backslash. To set the (Default) value we need, provide an empty string as the value name using <tt>/v ""</tt>. In a batch file, you also need to escape the '%' character with a second '%' character. With this in mind, the following lines in a batch file will set up the file association given in our example above:
  +
  +
<pre>
  +
reg add HKCU\SOFTWARE\Classes\.c /v "" /t REG_SZ /d "sourcecode" /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
  +
</pre>
  +
  +
==See also==
  +
* [[Launch files in new tabs under Windows#Using File Associations]] demonstrating clever use of the <tt>--remote</tt> family of command-line options as part of a file association.
  +
* [[VimTip934|Special-use file associations]] for when you don't just want to ''edit'' a file, but do something else with it in Vim.
  +
 
==Comments==
  +
{{todo}}
  +
* For user-specific associations, 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.
  +
* The usual Windows escape character is "^". Why do we need to escape quotes with a ''backslash'' in the "reg add" command? This seems weird, but it works.

Revision as of 03:39, 1 July 2009

Tip 1003 Printable Monobook Previous Next

created September 25, 2005 · complexity basic · author Anon · version 5.7


Although the Vim installer gives you options to create right-click "edit with Vim" options, it is convenient to be able to simply double-click on a file to have it open in Vim. This "double-click to open" is commonly referred to as a "file association" and is fairly easy to set up in Microsoft Windows. You can use the "Open With..." menu in Windows to set up a basic association, but that will not allow you to specify any command-line arguments to Vim, such as --remote-silent to open with an existing Vim instance. In order to fully specify the action to take when double-clicking a file, Windows provides the ftype and assoc command-line tools.

To use, simply open a Windows command prompt and type something like the following:

assoc .php=PHPFile
ftype PHPFile="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"

This particular command sequence will set up Windows to open any files with a .php extension in an existing gvim window (or it will open new gvim window if there is no already opened gvim window).

In general, the assoc command is used to associate a given file extension with a filetype. The ftype command is used to tell Windows what to do to open files of a given file type. You can associate any number of file extensions with a given filetype, which makes it easy to set up a single action for similar types of files. For example, the following will treat any files with extensions .c, .h, .pl, or .py as "sourcecode", which it will launch in Vim as we did with PHPFile above:

assoc .c=sourcecode
assoc .h=sourcecode
assoc .pl=sourcecode
assoc .py=sourcecode
ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"

How it works

Both the ftype and assoc commands work by creating entries in a specific registry location which Windows will check for various actions taken on a file. The easiest and safest way to create these entries is to use the command-line tools, but it is possible to create them by hand.

assoc .c=sourcecode will create a registry key at HKEY_CLASSES_ROOT\.c that has a (Default) value of sourcecode. When you double-click on a .c file, Windows will see this key and look for another key to figure out what to do with a sourcecode file.

ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" "%1" will set up another registry entry, in HKEY_CLASSES_ROOT\sourcecode\Shell\Open\command with (Default) value of "C:\Program Files\Vim\vim72\gvim.exe" "%1". This entry could do with more inspection.

Associating Vim with other actions

Consider the .html file extension. For most users, it would not make sense to open a .html file in Vim when double-clicking the file. Most user would want the file to open in a web browser when double-clicking, but being able to automatically edit the file in Vim when modifying the file would be a nice thing to be able to do. This is where the registry entry created by ftype comes in.

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.

There are no easy command-line or GUI tools to do this, but you can set up other actions, such as "edit", in the same registry area that ftype uses. Simply replace the "open" portion of the registry key path with the desired action (in this case, "edit"). For example, assume that .html files have been associated with a filetype called htmlfile. Then, to keep the existing double-click action in place, but add Vim as the "edit" action, give the following registry key a (Default) value of "C:\Program Files\Vim\vim72\gvim.exe" "%1": HKEY_CLASSES_ROOT\htmlfile\Shell\Edit\command

Edit action file association reg key

Registry entry for the "edit" action

Note: in Windows XP, there is a GUI for doing this, accessible through the "Folder Options" dialog.

Now, you can double-click on a .html file to open it in your web browser as normal. But you can also right-click on the file, and choose "Edit" from the context menu. This will open the file in gvim.

User-specific file associations

Unfortunately, all of the methods mentioned above apply your file associations in an area of the registry used by every user account in your Windows installation. 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, which duplicate what the assoc and ftype commands do in their registry area. 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, opening the file in a new tab. 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, just like the ftype command.

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

Just as we explained above, you can specify other actions to take besides opening on a double-click, simply by replacing "open" with the appropriate action in the registry key path.

Scripting the additions

Windows does provide a command-line utility for editing the registry: reg add. You can use this tool from a CMD prompt, but it is probably more useful to create a batch file containing commands to set all your desired file associations.

You need to escape double-quote characters with a backslash. 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. With this in mind, the following lines in a batch file will set up the file association given in our example above:

reg add HKCU\SOFTWARE\Classes\.c /v "" /t REG_SZ /d "sourcecode" /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

See also

Comments

 TO DO 

  • For user-specific associations, 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.
  • The usual Windows escape character is "^". Why do we need to escape quotes with a backslash in the "reg add" command? This seems weird, but it works.