Vim Tips Wiki
Register
(Undo revision 38209 by 71.90.208.213 (talk) Not Vim.)
Tags: rollback sourceedit
(14 intermediate revisions by 10 users not shown)
Line 3: Line 3:
 
|previous=1001
 
|previous=1001
 
|next=1004
 
|next=1004
|created=September 25, 2005
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=
 
|author=
Line 11: Line 11:
 
|category2=Windows
 
|category2=Windows
 
}}
 
}}
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.
+
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 <code>--remote-silent</code> to open with an existing Vim instance. In order to fully specify the action to take when double-clicking a file, Windows provides the <code>ftype</code> and <code>assoc</code> command-line tools.
   
 
To use, simply open a Windows command prompt and type something like the following:
 
To use, simply open a Windows command prompt and type something like the following:
Line 19: Line 19:
 
ftype PHPFile="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
 
ftype PHPFile="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
 
</pre>
 
</pre>
  +
  +
'''Note:''' If the above commands are entered in a batch file, the percent sign must be escaped (replace <code>"%1"</code> with <code>"%%1"</code>).
   
 
This particular command sequence will set up Windows to open any files with a .php extension in an [[Launch_files_in_new_tabs_under_Windows|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 [[Launch_files_in_new_tabs_under_Windows|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:
+
In general, the <code>assoc</code> command is used to associate a given file extension with a filetype. The <code>ftype</code> 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:
   
 
<pre>
 
<pre>
Line 31: Line 33:
 
ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
 
ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" --remote-silent "%1"
 
</pre>
 
</pre>
  +
  +
'''Note:''' If the above commands are entered in a batch file, the percent sign must be escaped (replace <code>"%1"</code> with <code>"%%1"</code>).
   
 
==How it works==
 
==How it works==
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.
+
Both the <code>ftype</code> and <code>assoc</code> 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.
+
<code>assoc .c=sourcecode</code> will create a registry key at '''<code>HKEY_CLASSES_ROOT\.c</code>''' that has a <code>(Default)</code> value of <code>sourcecode</code>. 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 <code>sourcecode</code> file.
   
<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.
+
<code>ftype sourcecode="C:\Program Files\Vim\vim72\gvim.exe" "%1"</code> will set up another registry entry, in '''<code>HKEY_CLASSES_ROOT\sourcecode\Shell\Open\command</code>''' with <code>(Default)</code> value of <code>"C:\Program Files\Vim\vim72\gvim.exe" "%1"</code>. This entry could do with more inspection.
   
 
==Associating Vim with other actions==
 
==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 users 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.
+
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 users 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 <code>ftype</code> comes in.
   
 
{{RegistryWarning}}
 
{{RegistryWarning}}
   
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>'''
+
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 <code>ftype</code> 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 <code>htmlfile</code>. Then, to keep the existing double-click action in place, but add Vim as the "edit" action, give the following registry key a <code>(Default)</code> value of <code>"C:\Program Files\Vim\vim72\gvim.exe" "%1"</code>: '''<code>HKEY_CLASSES_ROOT\htmlfile\Shell\Edit\command</code>'''
   
 
[[File:Edit action file association reg key.PNG|frame|Registry entry for the "edit" action]]
 
[[File:Edit action file association reg key.PNG|frame|Registry entry for the "edit" action]]
Line 52: Line 56:
 
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.
 
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.
   
  +
Do not limit yourself to just edit actions! For example, you could make an entry to [[Right_click_in_Windows_Explorer_to_open_gvim_in_explorer_mode|open gvim on a directory]], [[Vim-sessions under Windows|load configuration files, or source scripts]].
<!--Dirty hack to make sure the screenshot doesn't show up in the wrong section; fix later if possible--><div style="clear:both;">
 
  +
 
<!--Dirty hack to make sure the screenshot doesn't show up in the wrong section; fix later if possible but do not remove until then-->
  +
<div style="clear:both;">
   
 
==User-specific file associations==
 
==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.
 
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.
+
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 <code>HKEY_CURRENT_USER\Software\Classes</code>. Creating a file association here uses two steps, which duplicate what the <code>assoc</code> and <code>ftype</code> 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}}
 
{{RegistryWarning}}
   
 
===Step 1: associate the file extension with a file type===
 
===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>.
+
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, '''<code>HKEY_CURRENT_USER\Software\Classes\.c</code>''' (using <code>regedit</code> 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 <code>sourcecode</code>.
   
 
[[File:User-specific associations assoc reg key.PNG|frame|Registry entry for file extension association]]
 
[[File:User-specific associations assoc reg key.PNG|frame|Registry entry for file extension association]]
  +
{{clear}}
<!--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===
 
===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.
+
The next step is to tell Windows what to do with your new filetype, just like the <code>ftype</code> 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.
+
First, create the key '''<code>HKEY_CURRENT_USER\Software\Classes\sourcecode\shell\open\command</code>'''. 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 <code>"C:\Program Files\Vim\vim72\gvim.exe" --remote-tab-silent "%1"</code> to open .c files (and other <code>sourcecode</code> typed files) in a new tab in the default gvim.
   
 
[[File:User-specific associations ftype reg key.PNG|frame|Registry entry for filetype action]]
 
[[File:User-specific associations ftype reg key.PNG|frame|Registry entry for filetype action]]
Line 78: Line 85:
   
 
===Scripting the additions===
 
===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.
+
Windows does provide a command-line utility for editing the registry: <code>reg add</code>. 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:
+
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 <code>/v ""</code>. 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>
 
<pre>
Line 88: Line 95:
   
 
==See also==
 
==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.
+
*[[Launch files in new tabs under Windows#Using File Associations]] demonstrating clever use of the <code>--remote</code> 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.
+
*[[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==
 
==Comments==
 
{{todo}}
 
{{todo}}
* For user-specific associations, should also mention creating and merging a reg file, which is probably easier.
+
*For user-specific associations, should also mention creating and merging a reg file, which is probably easier.
* 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.
+
*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.
 
 
----
 
----
  +
You need to escape a double quote (and a backslash) in a ref file because that is how the syntax was designed. It allows RegEdit to differentiate between these characters entered as data vs as entered as delimiters. Same as the percent sign in batch files.
  +
ArtK Oct 14, 2014
  +
:Sure, I understand the concept of escaping. But why is the escape character '\' here? In .bat files you use ^ to escape anything, and backslash normally doesn't do anything special. So what's special about "reg add"? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
-------
  +
Whoa! Lots of inaccurate and misleading statements here. Some corrections and clarifications:
  +
:I don't think anything is actually inaccurate here, see my responses below. Maybe we need to retool which portions get emphasis. Please let us know what you think. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
File association definitions are are stored under registry keys in two (virtual) hives: HKLM and HKU. Specifically, HKLM\Software\Classes\ and HKCU\Software\Classes\ plus HKCU\...\FileExts\:
  +
  +
HKLM\Software\Classes\ defines associations for the local machine, i.e., ''all users'' (unless overridden by HKCU definitions)
  +
:Yes, this was mentioned in the [[#User-specific file associations]] section. This is the place the ftype and assoc commands modify. Where does this need clarification? Do you want to see this in the intro and [[#How it works]] sections? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
HKCU\Software\Classes\ defines associations for the ''current logged on user''.
  +
:Yes, this is the entire point of the [[#User-specific file associations]] section. Do you think that section needs more prominence? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
HCR is an ''alias'' for HKLM\Software\Classes\. It is ''not'' a copy. It is ''not'' ambiguous. It is ''not'' a merging of anything. It ''is'' simply a shorthand way of directly referring to a subset of HKLM (HKLM\Software\Classes\).
  +
:Experimentation shows that HCR includes entries for both HKLM and HKCU definitions. I agree it is not a copy...but it certainly seems to be a merging of some kind. Do you know where to find documentation on how this alias works? What happens if you create something here, does it get stored in HKLM or HKCU? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
Similarly, HKCU is an ''alias'' for a ''subset'' of HKU (HKU\S-1-5-21-...\) - the currently logged on user account. Again, just a shorthand name. (Each user account has a unique S-1-5-21-... key under HKU so HKCU is unique for each user.)
  +
:Good to know. I don't think that's very useful information for this tip, however...I don't think that much detail is needed in knowing how to set up file associations in HKCU. Do you disagree? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
In addition, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\ contains all the user account created definitions managed via "Explorer\Tools\Folder Options\File Types". These include, for example, application created entries when you install an app for a single user or when you allow your browser to set itself as the default browser.
  +
:OK, good to know. Where does this tie-in to double-clicking to open a file? Is it worth mentioning in this tip? Is there an easy editor or configuration tool for this outside the registry? We should include it if it is useful, especially if it provides an easy alternative to registry hacking. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
When a file reference is double clicked, Windows first looks in HKCU for any file association definitions for that specific file extent. ''If and only if'' no definition exists in HKCU for the requested ''verb'' (Open, Edit, Print, etc), does Windows look in HCR for its info.
  +
:I don't think HCR works the way you think it does. Right now on my system, in HCR, I have a "sourcecode" entry (with various open/edit verbs defined). "sourcecode" does ''not'' exist in HKLM\Software\Classes; here the entries skip right from "SoundRec" to "SPCFile". On the other hand, in HKCU\Software\Classes, "sourcecode" is present. So, obviously HCR includes entries from HKCU\Software\Classes.
  +
:On the other hand, in HCR, "sourcecode" is surrounded "SoundRec" and "SPCFile" just like in HKLM\Software\Classes. But HKCU\Software\Classes has "Software" and "stylesheet" surrounding the "sourcecode" entry. Thus, HCR also obviously includes entries from HKLM. HCR is ''definitely'' merging the two locations in some fashion. I just don't know the details as to how. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
Consequently, ASSOC and FTYPE are totally useless for managing user specific file associations. These utilities operate ''only'' on ''HCR'' keys (and require admin rights to change entries). Also, FTYPE only operates on the command key for the "open" verb (...\shell\open\command\); it cannot set or reset any other verb command key.
  +
:Yes, I agree. That's why we have a separate section on [[#User-specific associations]], and also [[#Associating Vim with other actions]]. But since most computers probably have only a few users, and this doesn't involve manually hacking the registry, and since alternate verbs are less common than "double-click to open": ftype and assoc are probably good to mention first. Do we need to emphasize that more in the section about ftype and assoc? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
This is just the tip of the iceberg regarding file associations. The next level is all of the "OpenWith" mechanisms for many-to-one and one-to-many associations. For more info, see SuperUser post[http://superuser.com/questions/204354/how-do-i-get-ftype-assoc-to-match-windows-explorer/814400#814400], do a Google search[http://www.google.com/search?as_q=windows&as_epq=file+association] or browse the registry with RegEdit (if you don't change anything, it is safe to look. Create a check point first to be doubly safe.)
  +
  +
ArtK Oct 14, 2014
  +
  +
:This tip is the result of hours of fiddling with the registry and Google searching, and ''it works''. So please don't assume proper searching was not done first. If this were easy, well-documented, or if Windows had a built-in UI, we may not have needed a tip in the first place. I'm sure there is more to the story about file associations, this is only an attempt to document the process SOMEWHERE because it isn't easy to find this information. Especially as it relates to something as user-specific as a preferred text editor.
  +
  +
:The top few Google hits for 'Windows "file associations"' all talk about using the GUI tools in Windows to set an "open with" program. This is all well and good, but programs like Vim often need to pass various command-line arguments along with the file to be useful. Can this be done with the "open with" GUIs?
  +
  +
:Thanks for the SuperUser link, that gives some good detail, especially about the FileExts area. But you should certainly look into the HKEY_CLASSES_ROOT again, I think you're incorrect there. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:30, October 14, 2014 (UTC)
  +
  +
-------
 
The following is a "for reader's information" comment; not a suggestion that the tip be changed. In classic Microsoft style, using HKEY_CLASSES_ROOT is ambiguous. I do not know the details, but on at least some systems (after Windows 9x), I think the following is correct:
 
The following is a "for reader's information" comment; not a suggestion that the tip be changed. In classic Microsoft style, using HKEY_CLASSES_ROOT is ambiguous. I do not know the details, but on at least some systems (after Windows 9x), I think the following is correct:
:<tt>HKEY_LOCAL_MACHINE\Software\Classes</tt> : for all users
+
:<code>HKEY_LOCAL_MACHINE\Software\Classes</code> : for all users
:<tt>HKEY_CURRENT_USER\Software\Classes</tt> : for interactive user
+
:<code>HKEY_CURRENT_USER\Software\Classes</code> : for interactive user
:<tt>HKEY_CLASSES_ROOT</tt> : merged view of above (and is used by Win9x apps)
+
:<code>HKEY_CLASSES_ROOT</code> : merged view of above (and is used by Win9x apps)
   
 
I don't know what happens if you write to HKEY_CLASSES_ROOT (which does not exist). Perhaps (like installing some apps), if you are in the Administrators group, you will write to HKLM, otherwise you will write to HKCU. [[User:JohnBeckett|JohnBeckett]] 08:24, 2 July 2009 (UTC)
 
I don't know what happens if you write to HKEY_CLASSES_ROOT (which does not exist). Perhaps (like installing some apps), if you are in the Administrators group, you will write to HKLM, otherwise you will write to HKCU. [[User:JohnBeckett|JohnBeckett]] 08:24, 2 July 2009 (UTC)
Line 107: Line 155:
   
 
--[[User:Fritzophrenic|Fritzophrenic]] 15:19, 2 July 2009 (UTC)
 
--[[User:Fritzophrenic|Fritzophrenic]] 15:19, 2 July 2009 (UTC)
  +
----
  +
The <code>HKEY_CLASSES_ROOT</code> key provides a merged view of <code>HKEY_LOCAL_MACHINE\Software\Classes</code> and <code>HKEY_CURRENT_USER\Software\Classes</code>.
  +
  +
When reading, the <code>HKEY_CURRENT_USER</code> settings have priority over the <code>HKEY_LOCAL_MACHINE</code> settings.
  +
  +
If you write values to a key under <code>HKEY_CLASSES_ROOT</code>, the values will:
  +
# Go under <code>HKEY_CURRENT_USER\Software\Classes</code> if a <code>HKEY_CURRENT_USER\Software\Classes\key</code> previously existed under <code>HKEY_CURRENT_USER\Software\Classes</code>.
  +
# Go under <code>HKEY_LOCAL_MACHINE\Software\Classes</code> otherwise.
  +
--December 23, 2012
  +
----
  +
Perhaps [[Associate files with no extension to Vim under Windows|Tip 1301]] could be merged in here, by adding
  +
:<code>assoc .=sourcecode</code>
  +
to the examples, with a comment saying it applies to a file with no extension?
  +
  +
[[User:JLittle|JLittle]] 21:27, November 10, 2009 (UTC)
  +
:Yes, and probably also [[Open_Vim_Here_by_clicking_the_context_menu_on_a_folder/directory_on_Windows]]. Maybe we can create a "special cases" section that includes both of these, and potentially other "reserved" values that can be used? --[[User:Fritzophrenic|Fritzophrenic]] 21:44, November 10, 2009 (UTC)

Revision as of 06:01, 9 March 2015

Tip 1003 Printable Monobook Previous Next

created 2005 · complexity basic · version 6.0


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"

Note: If the above commands are entered in a batch file, the percent sign must be escaped (replace "%1" with "%%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"

Note: If the above commands are entered in a batch file, the percent sign must be escaped (replace "%1" with "%%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 users 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.

Do not limit yourself to just edit actions! For example, you could make an entry to open gvim on a directory, load configuration files, or source scripts.

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.
  • 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.

You need to escape a double quote (and a backslash) in a ref file because that is how the syntax was designed. It allows RegEdit to differentiate between these characters entered as data vs as entered as delimiters. Same as the percent sign in batch files. ArtK Oct 14, 2014

Sure, I understand the concept of escaping. But why is the escape character '\' here? In .bat files you use ^ to escape anything, and backslash normally doesn't do anything special. So what's special about "reg add"? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

Whoa! Lots of inaccurate and misleading statements here. Some corrections and clarifications:

I don't think anything is actually inaccurate here, see my responses below. Maybe we need to retool which portions get emphasis. Please let us know what you think. --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

File association definitions are are stored under registry keys in two (virtual) hives: HKLM and HKU. Specifically, HKLM\Software\Classes\ and HKCU\Software\Classes\ plus HKCU\...\FileExts\:

HKLM\Software\Classes\ defines associations for the local machine, i.e., all users (unless overridden by HKCU definitions)

Yes, this was mentioned in the #User-specific file associations section. This is the place the ftype and assoc commands modify. Where does this need clarification? Do you want to see this in the intro and #How it works sections? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

HKCU\Software\Classes\ defines associations for the current logged on user.

Yes, this is the entire point of the #User-specific file associations section. Do you think that section needs more prominence? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

HCR is an alias for HKLM\Software\Classes\. It is not a copy. It is not ambiguous. It is not a merging of anything. It is simply a shorthand way of directly referring to a subset of HKLM (HKLM\Software\Classes\).

Experimentation shows that HCR includes entries for both HKLM and HKCU definitions. I agree it is not a copy...but it certainly seems to be a merging of some kind. Do you know where to find documentation on how this alias works? What happens if you create something here, does it get stored in HKLM or HKCU? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

Similarly, HKCU is an alias for a subset of HKU (HKU\S-1-5-21-...\) - the currently logged on user account. Again, just a shorthand name. (Each user account has a unique S-1-5-21-... key under HKU so HKCU is unique for each user.)

Good to know. I don't think that's very useful information for this tip, however...I don't think that much detail is needed in knowing how to set up file associations in HKCU. Do you disagree? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

In addition, HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\ contains all the user account created definitions managed via "Explorer\Tools\Folder Options\File Types". These include, for example, application created entries when you install an app for a single user or when you allow your browser to set itself as the default browser.

OK, good to know. Where does this tie-in to double-clicking to open a file? Is it worth mentioning in this tip? Is there an easy editor or configuration tool for this outside the registry? We should include it if it is useful, especially if it provides an easy alternative to registry hacking. --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

When a file reference is double clicked, Windows first looks in HKCU for any file association definitions for that specific file extent. If and only if no definition exists in HKCU for the requested verb (Open, Edit, Print, etc), does Windows look in HCR for its info.

I don't think HCR works the way you think it does. Right now on my system, in HCR, I have a "sourcecode" entry (with various open/edit verbs defined). "sourcecode" does not exist in HKLM\Software\Classes; here the entries skip right from "SoundRec" to "SPCFile". On the other hand, in HKCU\Software\Classes, "sourcecode" is present. So, obviously HCR includes entries from HKCU\Software\Classes.
On the other hand, in HCR, "sourcecode" is surrounded "SoundRec" and "SPCFile" just like in HKLM\Software\Classes. But HKCU\Software\Classes has "Software" and "stylesheet" surrounding the "sourcecode" entry. Thus, HCR also obviously includes entries from HKLM. HCR is definitely merging the two locations in some fashion. I just don't know the details as to how. --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

Consequently, ASSOC and FTYPE are totally useless for managing user specific file associations. These utilities operate only on HCR keys (and require admin rights to change entries). Also, FTYPE only operates on the command key for the "open" verb (...\shell\open\command\); it cannot set or reset any other verb command key.

Yes, I agree. That's why we have a separate section on #User-specific associations, and also #Associating Vim with other actions. But since most computers probably have only a few users, and this doesn't involve manually hacking the registry, and since alternate verbs are less common than "double-click to open": ftype and assoc are probably good to mention first. Do we need to emphasize that more in the section about ftype and assoc? --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

This is just the tip of the iceberg regarding file associations. The next level is all of the "OpenWith" mechanisms for many-to-one and one-to-many associations. For more info, see SuperUser post[1], do a Google search[2] or browse the registry with RegEdit (if you don't change anything, it is safe to look. Create a check point first to be doubly safe.)

ArtK Oct 14, 2014

This tip is the result of hours of fiddling with the registry and Google searching, and it works. So please don't assume proper searching was not done first. If this were easy, well-documented, or if Windows had a built-in UI, we may not have needed a tip in the first place. I'm sure there is more to the story about file associations, this is only an attempt to document the process SOMEWHERE because it isn't easy to find this information. Especially as it relates to something as user-specific as a preferred text editor.
The top few Google hits for 'Windows "file associations"' all talk about using the GUI tools in Windows to set an "open with" program. This is all well and good, but programs like Vim often need to pass various command-line arguments along with the file to be useful. Can this be done with the "open with" GUIs?
Thanks for the SuperUser link, that gives some good detail, especially about the FileExts area. But you should certainly look into the HKEY_CLASSES_ROOT again, I think you're incorrect there. --Fritzophrenic (talk) 18:30, October 14, 2014 (UTC)

The following is a "for reader's information" comment; not a suggestion that the tip be changed. In classic Microsoft style, using HKEY_CLASSES_ROOT is ambiguous. I do not know the details, but on at least some systems (after Windows 9x), I think the following is correct:

HKEY_LOCAL_MACHINE\Software\Classes : for all users
HKEY_CURRENT_USER\Software\Classes : for interactive user
HKEY_CLASSES_ROOT : merged view of above (and is used by Win9x apps)

I don't know what happens if you write to HKEY_CLASSES_ROOT (which does not exist). Perhaps (like installing some apps), if you are in the Administrators group, you will write to HKLM, otherwise you will write to HKCU. JohnBeckett 08:24, 2 July 2009 (UTC)


This is correct as far as I know. I wanted to include some information about how HKCR is a merged view of HKLM/Software/Classes and HKCU/Software/Classes, but doing so would beg information such as "what happens when I edit an entry that actually exists in HKCU?" etc. From experimentation, it seems that creating new keys in this area will always create it in HKLM (system-wide), and ftype and assoc certainly do that, but I don't really have any documentation of that fact, and I don't know what it will do for limited-privilege accounts. I also don't know what happens when you edit an existing key, but I imagine it will "do the right thing" and keep the original where it was. I don't know this for a fact though. For these reasons, I left out that tidbit. But if we can answer some of these questions, it would be a good thing to include.

--Fritzophrenic 15:19, 2 July 2009 (UTC)


The HKEY_CLASSES_ROOT key provides a merged view of HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes.

When reading, the HKEY_CURRENT_USER settings have priority over the HKEY_LOCAL_MACHINE settings.

If you write values to a key under HKEY_CLASSES_ROOT, the values will:

  1. Go under HKEY_CURRENT_USER\Software\Classes if a HKEY_CURRENT_USER\Software\Classes\key previously existed under HKEY_CURRENT_USER\Software\Classes.
  2. Go under HKEY_LOCAL_MACHINE\Software\Classes otherwise.

--December 23, 2012


Perhaps Tip 1301 could be merged in here, by adding

assoc .=sourcecode

to the examples, with a comment saying it applies to a file with no extension?

JLittle 21:27, November 10, 2009 (UTC)

Yes, and probably also Open_Vim_Here_by_clicking_the_context_menu_on_a_folder/directory_on_Windows. Maybe we can create a "special cases" section that includes both of these, and potentially other "reserved" values that can be used? --Fritzophrenic 21:44, November 10, 2009 (UTC)