Vim Tips Wiki
m (Windows: gvim as an external editor moved to Use gvim as an external editor for Windows apps: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=805
 
|id=805
  +
|previous=804
|title=Windows: gvim as an external editor
 
  +
|next=806
|created=October 17, 2004 22:37
+
|created=October 17, 2004
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Ben Collerson
 
|author=Ben Collerson
 
|version=6.0
 
|version=6.0
 
|rating=164/49
 
|rating=164/49
 
}}
|text=
 
This tip provides a (Windows only) means of using gvim as an external editor for almost any other program. This script has been tested with Lotus Notes and Internet Explorer on Windows NT. This tip was written using gvim from Internet Explorer.
+
This tip provides a (Windows only) means of using gvim as an external editor for almost any program. This script has been tested with Lotus Notes and Internet Explorer.
   
 
===Step 1===
 
===Step 1===
Download the following scripts and place them in your vim plugin directory:
+
Download the following scripts and place them in your Vim plugin directory:
* clipbrd.vim ({{Script|id=1014}})
+
* clipbrd.vim {{script|id=1014}}
* genutils.vim ({{Script|id=197}}
+
* genutils.vim {{script|id=197}}
* multvals.vim ({{Script|id=171}}
+
* multvals.vim {{script|id=171}}
   
 
===Step 2===
 
===Step 2===
The vbscript below should be saved as something like runvim.vbs and put it in a directory such as C:\Program Files\Vim\Vim63. Modify line 5 to show the correct path to your gvim executable. Create a shortcut to runvim.vbs on your desktop. Right-click on the shortcut, select "Properties" from the context menu, click the "Shortcut" tab and add a "Shortcut Key" -- something like: "Ctrl-Alt-V".
+
The vbscript below should be saved as something like runvim.vbs and put it in a directory such as C:\Program Files\Vim\Vim63. Modify the ''run'' line to show the correct path to your gvim executable. Create a shortcut to runvim.vbs on your desktop. Right-click on the shortcut, select "Properties" from the context menu, click the "Shortcut" tab and add a "Shortcut Key" – something like "Ctrl-Alt-V".
   
  +
<pre>
set oShell = CreateObject("WScript.Shell")
+
set oShell = CreateObject("WScript.Shell")
oShell.SendKeys"^a"
 
oShell.SendKeys"^c"
+
oShell.SendKeys"^a"
oShell.SendKeys"^{Home}"
+
oShell.SendKeys"^c"
 
oShell.SendKeys"^{Home}"
ReturnCode = oShell.run ("""C:\\Program Files\\Vim\\Vim63\\Gvim.exe"" +ClipBrd +only",0,True)
+
ReturnCode = oShell.run ("""C:\\Program Files\\Vim\\Vim63\\Gvim.exe"" +ClipBrd +only",0,True)
oShell.SendKeys"^a"
 
oShell.SendKeys"^v"
+
oShell.SendKeys"^a"
oShell.SendKeys"^{Home}"
+
oShell.SendKeys"^v"
 
oShell.SendKeys"^{Home}"
  +
</pre>
   
 
===Usage===
 
===Usage===
Now when you want to use vim to edit some text in your employers' crappy email client or web browser just move your cursor to the text area and hit Ctrl-Alt-V. The current text will be copied into gvim where it can be edited. Once you are finished editing, type :wq and answer "Y" to update register "*". Your edited text should now appear in the text area.
+
Now when you want to use Vim to edit some text in your employers' crappy email client or web browser just move your cursor to the text area and hit Ctrl-Alt-V. The current text will be copied into gvim where it can be edited. Once you are finished editing, type <tt>:wq</tt> and answer "Y" to update register "*". Your edited text should now appear in the text area.
 
}}
 
   
== Comments ==
+
==Comments==
Brilliant! Although I had some trouble getting it working.
+
I had some trouble getting it working.
   
 
You install Vim in c:\vim\ so I guess then the VBS will work, but I had to do some trickery there because I install vim in Program Files. This is the line that worked for me. Note the triple quotes at the start and the double ones at the end of the location of gvim.exe. I don't know VBS, so I'm guessing this has something to do with escaping quotes (or something).
 
You install Vim in c:\vim\ so I guess then the VBS will work, but I had to do some trickery there because I install vim in Program Files. This is the line that worked for me. Note the triple quotes at the start and the double ones at the end of the location of gvim.exe. I don't know VBS, so I'm guessing this has something to do with escaping quotes (or something).
Line 44: Line 45:
 
I used Hari Krishna Dara's clipbrd.vim script because it provided a quick solution to the problem of getting the text from one application to another. I feel this solution is not ideal in this case.
 
I used Hari Krishna Dara's clipbrd.vim script because it provided a quick solution to the problem of getting the text from one application to another. I feel this solution is not ideal in this case.
   
a) It would be better for the VBScript to put the clipboard into a temporary file and pass this to vim. This would allow the original clipboard to be saved and restored. Using a temporary file also fits better into how vim works. When you finish editing :wq is normal, :q! is a kludge.
+
a) It would be better for the VBScript to put the clipboard into a temporary file and pass this to Vim. This would allow the original clipboard to be saved and restored. Using a temporary file also fits better into how Vim works. When you finish editing <tt>:wq</tt> is normal, <tt>:q!</tt> is a kludge.
   
b) When vim is exited the original application should be made active to ensure that the text is pasted back in to the correct place. AppActivate is the WSH method which would be used, unfortunately there is no built-in method to get the title of the currently active application before gvim is launched. I am still trying to sort this one out.
+
b) When Vim is exited the original application should be made active to ensure that the text is pasted back in to the correct place. AppActivate is the WSH method which would be used, unfortunately there is no built-in method to get the title of the currently active application before gvim is launched. I am still trying to sort this one out.
   
Anyway if there is anyone out there who actually knows what they are doing with WSH/VBScript maybe they could post some suggestions.
 
 
Cheers,
 
Ben
 
 
----
 
----
This is a great tip. I have been using clipbrd.vim to do exactly these steps manually, and it never occurred to me that it can be automated using a script. Inspired, I have created somewhat more reliable approach for doing this. I talked to Ben about focussing the original application before pasting the clipboard, but there seems to be no solution for this in vbscript. So here is a working solution that uses the excellent open source automation tool called AutoIt3. Install AutoIt3, copy the following lines and create a .au3 script:
+
This is a great tip. I have been using clipbrd.vim to do exactly these steps manually, and it never occurred to me that it can be automated using a script. Inspired, I have created somewhat more reliable approach for doing this. I talked to Ben about focusing the original application before pasting the clipboard, but there seems to be no solution for this in vbscript. So here is a working solution that uses the excellent open source automation tool called AutoIt3. Install AutoIt3, copy the following lines and create a .au3 script:
   
---vimEditClipbrd.au3---
 
 
<pre>
 
<pre>
 
; vimEditClipbrd.au3
 
; Make sure the focus is on the top most window.
 
; Make sure the focus is on the top most window.
 
If WinGetTitle("") == "" Then
 
If WinGetTitle("") == "" Then
Line 72: Line 69:
 
</pre>
 
</pre>
   
Follow the same steps as the above comment (ie., create a shortcut etc.).
+
Follow the same steps as the above comment (i.e., create a shortcut etc).
   
For folks who may not like to install AutoIt3, I created a self executable script out of the above script and made it available at the below address. Download and place it anywhere, and while creating the shortcut, use this file instead of the above script.
 
 
http://mywebpage.netscape.com/haridara/vim/vimEditClipbrd.exe
 
 
Hari
 
 
----
 
----
If the runvim.vbs script does not work for some editor or other, you
+
If the runvim.vbs script does not work for some editor or other, you could try experimenting with other ways of getting the clipboard. Here is a version of runvim.vbs which might work for editors which have trouble with Ctrl-A:
could try experimenting with other ways of getting the clipboard. Here is
 
a version of runvim.vbs which might work for editors which have trouble
 
with Ctrl-A:
 
   
  +
<pre>
---
 
 
set oShell = CreateObject("WScript.Shell")
 
set oShell = CreateObject("WScript.Shell")
 
oShell.SendKeys"^{End}^+{Home}%ec^{Home}"
 
oShell.SendKeys"^{End}^+{Home}%ec^{Home}"
 
ReturnCode = oShell.run("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True)
 
ReturnCode = oShell.run("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True)
 
oShell.SendKeys"^{End}^+{Home}%ep^{Home}"
 
oShell.SendKeys"^{End}^+{Home}%ep^{Home}"
  +
</pre>
---
 
   
 
More SendKeys info:
 
More SendKeys info:
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp
   
Cheers,
 
Ben
 
 
----
 
----
Did you try if the AutoIt solution works for you? I also suggest you to try debugginga bit. You can start off by just having the following:
 
 
set oShell = CreateObject("WScript.Shell")
 
oShell.SendKeys"^a"
 
 
See if the text gets selected. If this itself doesn't work, then I would check if the focus is on the application or not. I hope you get the idea.
 
 
hari_vim at yahoo dot com
 
, October 22, 2004 22:41
 
----
 
Hallo,
 
 
 
This works for me:
 
This works for me:
   
Line 131: Line 106:
 
Apparently the application is to quick with out the pauses.
 
Apparently the application is to quick with out the pauses.
 
----
 
----
 
I had the problem I think was related before in which I would return to the editor (outlook in this case) and the contents would not update. I found that instead of exiting per the original instructions ':q!' I had to exit ':wq!' and it worked just great!
Hello,
 
   
I had the problem I think was related before in which I would return to the editor (outlook in this case) and the contents would not update. I found that instead of exiting per the original instructions ':q!' I had to exit ':wq!' and it worked just great!
 
 
----
 
----
I don't have gvim installed. Instead I use vim under Windows XP + cygwin, after I followed the scripts here, when I press Ctrl + Alt + V, vim does fire up, but no contents are copied to and from it. I figure this is because in rxvt + vim, the way of copy and paste clipboard is different, i.e. we do Shift + V to paste clipboard to vim, not Ctrl + V. Is there a workaround for rxvt + vim?
+
I don't have gvim installed. Instead I use Vim under Windows XP + cygwin, after I followed the scripts here, when I press Ctrl+Alt+V, Vim does fire up, but no contents are copied to and from it. I figure this is because in rxvt + vim, the way of copy and paste clipboard is different, i.e. we do Shift + V to paste clipboard to vim, not Ctrl + V. Is there a workaround for rxvt + vim?
  +
 
----
 
----
Based on the ideas above I have put together a more robust program for
+
Based on the ideas above I have put together a more robust program for launching an external editor.
launching an external editor.
 
   
Features include error checking, so you don't lose you text when you
+
Features include error checking, so you don't lose you text when you are on the wrong Firefox tab. It also keeps track of editor sessions, reactivating previous sessions for a particular application.
are on the wrong Firefox tab. It also keeps track of editor sessions,
 
reactivating previous sessions for a particular application.
 
   
To install, just unzip, edit the config file "external.ini", and run
+
To install, just unzip, edit the config file "external.ini", and run external.exe.
external.exe.
 
   
 
Check the following links for more info:
 
Check the following links for more info:
Line 153: Line 124:
 
Download: http://bur.st/~benc/external/external.zip
 
Download: http://bur.st/~benc/external/external.zip
   
Ben Collerson - benc at bur dot st
 
 
----
 
----
 
It should be noted that if your default script host is set to cscript this doesn't work since you end up with a command window hanging around stealing focus. Change the shortcut to
 
It should be noted that if your default script host is set to cscript this doesn't work since you end up with a command window hanging around stealing focus. Change the shortcut to
 
ReturnCode = oShell.run ("%windir%\system32\wscript.exe C:\Vim\Vim63\runvim.vbs +ClipBrd +only",0,True)
 
ReturnCode = oShell.run ("%windir%\system32\wscript.exe C:\Vim\Vim63\runvim.vbs +ClipBrd +only",0,True)
  +
 
----
 
----
I am loving this, however, I have one problem. When I use vim this way ( from the vbs script), I don't get the settings from my _vimrc file. There is no syntax highligting etc. Any suggestions? Could it be the switches at the end of this statement? ReturnCode = oShell.run ("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True) I don't know what the +only,0,True options do. Thanks for your help.
+
I am loving this, however, I have one problem. When I use vim this way ( from the vbs script), I don't get the settings from my _vimrc file. There is no syntax highligting etc. Any suggestions? Could it be the switches at the end of this statement? ReturnCode = oShell.run ("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True) I don't know what the +only,0,True options do.
   
toni.baker--AT--sbcglobal.net
 
, March 8, 2006 15:16
 
 
----
 
----
Excellent script. Tested this with Gvim 6.4 on Windows XP (damned work machines) and Klaus Horsten's script seems to do the trick. The original script works for the first time, but does not update the clipboard when launched from some apps (Firefox etc). Klaus' script works perfectly.
+
Excellent script. Tested this with gvim 6.4 on Windows XP (damned work machines) and Klaus Horsten's script seems to do the trick. The original script works for the first time, but does not update the clipboard when launched from some apps (Firefox etc). Klaus' script works perfectly.
----
 
I am having the same problem as toni. I noticed the _vimrc is being read but something else is overriden all the option there. Any idea?
 
   
muteki
 
, May 15, 2006 20:44
 
 
----
 
----
Ah, I noticed clipbrd.vim is where overriding all those options.
+
I noticed the _vimrc is being read but something else is overriden all the option there. I noticed clipbrd.vim is where overriding all those options.
   
muteki
 
, May 15, 2006 20:50
 
 
----
 
----
 
If you're using [http://portablegvim.sourceforge.net/ PortableGVim], try pointing the vbscript at PortableGvim.exe and setting the WaitForGVim=true setting in the PortableGvim.ini file. Otherwise you may find that the vim never gets focus after the copy/paste and subsequently cannot update the register when you :wq.
 
If you're using [http://portablegvim.sourceforge.net/ PortableGVim], try pointing the vbscript at PortableGvim.exe and setting the WaitForGVim=true setting in the PortableGvim.ini file. Otherwise you may find that the vim never gets focus after the copy/paste and subsequently cannot update the register when you :wq.
   
 
----
 
----
Hi,
 
 
I am running gvim 7. While running the .VBS script using ctrl+alt+V I am getting the following error message :
 
 
Error detected while processing function &lt;SNR&gt;11_ViewRegister..&lt;SNR&gt;11_SetupBuf:
 
 
line 1:
 
E117: Unknown function: SetupScratchBuffer
 
Error detected while processing function SNR&gt;11_ViewRegister:
 
line 51:
 
E117: Unnown function: OptClearBuffer.
 
 
I tried recopying multivals.vim, genutils.vim and clipbrd.vim.
 
 
but the problem is still occuring.
 
 
Any help will be appreciated.
 
----
 
Hi,
 
 
I really want to use the programme at http://bur.st/~benc/?p=external, but did not work for me. Can you help? I used it on Firefox 2.0, and it can start Vim (gvim) with a text copied from Firefox. But, the programme does not paste a text back to to Firefox when I type ":q!". Please, please help.
 
----
 
Hi, I tried the plugins with vim 7 , but it does not work. I tried to find external.vim at http://bur.st/~benc/?p=external, but the link seems to be broken. Any idea where I can find external.vim so that I can get it working with vim 7? Thanks.
 
----
 
I don't know about autoit but I'm using autohotkey to do the task with a script I'd written call [http://haa.homeip.net:8080/files/windows/vimdow.ahk vimdow.ahk] It retains cursor position between transfer as well as to keep you from unfocussing the edit window during editing.
 
 
 
<!-- parsed by vimtips.py in 0.791813 seconds-->
 
 
[[Category:Windows]]
 
[[Category:Windows]]

Revision as of 00:54, 25 November 2007

Tip 805 Printable Monobook Previous Next

created October 17, 2004 · complexity intermediate · author Ben Collerson · version 6.0


This tip provides a (Windows only) means of using gvim as an external editor for almost any program. This script has been tested with Lotus Notes and Internet Explorer.

Step 1

Download the following scripts and place them in your Vim plugin directory:

Step 2

The vbscript below should be saved as something like runvim.vbs and put it in a directory such as C:\Program Files\Vim\Vim63. Modify the run line to show the correct path to your gvim executable. Create a shortcut to runvim.vbs on your desktop. Right-click on the shortcut, select "Properties" from the context menu, click the "Shortcut" tab and add a "Shortcut Key" – something like "Ctrl-Alt-V".

set oShell = CreateObject("WScript.Shell")
oShell.SendKeys"^a"
oShell.SendKeys"^c"
oShell.SendKeys"^{Home}"
ReturnCode = oShell.run ("""C:\\Program Files\\Vim\\Vim63\\Gvim.exe"" +ClipBrd +only",0,True)
oShell.SendKeys"^a"
oShell.SendKeys"^v"
oShell.SendKeys"^{Home}"

Usage

Now when you want to use Vim to edit some text in your employers' crappy email client or web browser just move your cursor to the text area and hit Ctrl-Alt-V. The current text will be copied into gvim where it can be edited. Once you are finished editing, type :wq and answer "Y" to update register "*". Your edited text should now appear in the text area.

Comments

I had some trouble getting it working.

You install Vim in c:\vim\ so I guess then the VBS will work, but I had to do some trickery there because I install vim in Program Files. This is the line that worked for me. Note the triple quotes at the start and the double ones at the end of the location of gvim.exe. I don't know VBS, so I'm guessing this has something to do with escaping quotes (or something).

ReturnCode = oShell.run ("""c:\\Program Files\\Vim\\vim63\\gvim.exe"" +ClipBrd +only",0,True)


I used Hari Krishna Dara's clipbrd.vim script because it provided a quick solution to the problem of getting the text from one application to another. I feel this solution is not ideal in this case.

a) It would be better for the VBScript to put the clipboard into a temporary file and pass this to Vim. This would allow the original clipboard to be saved and restored. Using a temporary file also fits better into how Vim works. When you finish editing :wq is normal, :q! is a kludge.

b) When Vim is exited the original application should be made active to ensure that the text is pasted back in to the correct place. AppActivate is the WSH method which would be used, unfortunately there is no built-in method to get the title of the currently active application before gvim is launched. I am still trying to sort this one out.


This is a great tip. I have been using clipbrd.vim to do exactly these steps manually, and it never occurred to me that it can be automated using a script. Inspired, I have created somewhat more reliable approach for doing this. I talked to Ben about focusing the original application before pasting the clipboard, but there seems to be no solution for this in vbscript. So here is a working solution that uses the excellent open source automation tool called AutoIt3. Install AutoIt3, copy the following lines and create a .au3 script:

; vimEditClipbrd.au3
; Make sure the focus is on the top most window.
If WinGetTitle("") == "" Then
    MouseClick("left")
EndIf
Sleep(10)
Send("^a^c^{HOME}")
$actTitle = WinGetTitle("")
$vimPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Vim\Gvim", "path")
If RunWait($vimPath&" --servername ClipBrd +ClipBrd +only ""+call foreground()""") == 0 Then
    WinActivate($actTitle)
    Sleep(10)
    Send("^a^v^{HOME}")
EndIf

Follow the same steps as the above comment (i.e., create a shortcut etc).


If the runvim.vbs script does not work for some editor or other, you could try experimenting with other ways of getting the clipboard. Here is a version of runvim.vbs which might work for editors which have trouble with Ctrl-A:

set oShell = CreateObject("WScript.Shell")
oShell.SendKeys"^{End}^+{Home}%ec^{Home}"
ReturnCode = oShell.run("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True)
oShell.SendKeys"^{End}^+{Home}%ep^{Home}"

More SendKeys info: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthsendkeys.asp


This works for me:

set oShell = CreateObject("WScript.Shell")
WScript.Sleep 100
oShell.SendKeys"^a"
WScript.Sleep 100
oShell.SendKeys"^c"
oShell.SendKeys"^{Home}"
ReturnCode = oShell.run ("""d:\\vim\\vim63\\gvim.exe"" +ClipBrd +only",0,True)
WScript.Sleep 100
oShell.SendKeys"^a"
WScript.Sleep 100
oShell.SendKeys"^v"
oShell.SendKeys"^{Home}"

I added WScript.Sleep 100

Apparently the application is to quick with out the pauses.


I had the problem I think was related before in which I would return to the editor (outlook in this case) and the contents would not update. I found that instead of exiting per the original instructions ':q!' I had to exit ':wq!' and it worked just great!


I don't have gvim installed. Instead I use Vim under Windows XP + cygwin, after I followed the scripts here, when I press Ctrl+Alt+V, Vim does fire up, but no contents are copied to and from it. I figure this is because in rxvt + vim, the way of copy and paste clipboard is different, i.e. we do Shift + V to paste clipboard to vim, not Ctrl + V. Is there a workaround for rxvt + vim?


Based on the ideas above I have put together a more robust program for launching an external editor.

Features include error checking, so you don't lose you text when you are on the wrong Firefox tab. It also keeps track of editor sessions, reactivating previous sessions for a particular application.

To install, just unzip, edit the config file "external.ini", and run external.exe.

Check the following links for more info:

http://bur.st/~benc/?p=external

Download: http://bur.st/~benc/external/external.zip


It should be noted that if your default script host is set to cscript this doesn't work since you end up with a command window hanging around stealing focus. Change the shortcut to ReturnCode = oShell.run ("%windir%\system32\wscript.exe C:\Vim\Vim63\runvim.vbs +ClipBrd +only",0,True)


I am loving this, however, I have one problem. When I use vim this way ( from the vbs script), I don't get the settings from my _vimrc file. There is no syntax highligting etc. Any suggestions? Could it be the switches at the end of this statement? ReturnCode = oShell.run ("c:\\vim\\vim63\\gvim.exe +ClipBrd +only",0,True) I don't know what the +only,0,True options do.


Excellent script. Tested this with gvim 6.4 on Windows XP (damned work machines) and Klaus Horsten's script seems to do the trick. The original script works for the first time, but does not update the clipboard when launched from some apps (Firefox etc). Klaus' script works perfectly.


I noticed the _vimrc is being read but something else is overriden all the option there. I noticed clipbrd.vim is where overriding all those options.


If you're using PortableGVim, try pointing the vbscript at PortableGvim.exe and setting the WaitForGVim=true setting in the PortableGvim.ini file. Otherwise you may find that the vim never gets focus after the copy/paste and subsequently cannot update the register when you :wq.