sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #805: Windows: gvim as an external editor

 tip karma   Rating 213/62, Viewed by 13825 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 17, 2004 22:37      complexity:   intermediate
author:   Ben Collerson      as of Vim:   6.0

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.


Step 1.
=======
Download a copy of clipbrd.vim (vimscript#1014) and put it in your
plugin directory. Modify clipbrd.vim by adding a line as shown below to
the autocommand block in clipbrd.vim:

  aug ClipBrd
    au!
    exec 'au BufWriteCmd ' . auTitle .' :call <SID>UpdateRegister(0, 1)'
    exec 'au BufReadCmd ' . auTitle .' :call <SID>ViewRegister()'

    exec 'au VimLeave ' .auTitle .' :norm gg"*yG'        " <-- this line

  aug END


Step 2.
=======
The vbscript below should be saved as something like runvim.vbs and put
it in your c:\vim\vim6x\ directory. Create a shortcut to runvim.vbs on
your desktop. Right-click on the shortcut, select "Properties" from
the context menu, click the "Shorcut" tab and add a "Shortcut Key" --
something like: "Ctrl-Alt-V".

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


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, quit without saving (:q!). Your
edited text should now appear in the text area.

 rate this tip  Life Changing Helpful Unfulfilling 

<< single 'tags' file for a source tree | Always keep quickfix window at specified height >>

Additional Notes

[email protected], October 18, 2004 0:22
Brilliant! Although I had some trouble getting it working.

1) Perhaps you should somewhere mention that one needs genutils.vim as well as multvals.vim.

2) 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)


zzapper, October 18, 2004 6:18
This is a very useful, but could you perhaps arrange with the writer (Hari Krishna Dara) of clipbrd.vim, genutils.vim, multvals.com to "unify" all this into ideally one script?

[email protected], October 18, 2004 7:11
This is a very cool tip! Thank you very much.
I agree with zzapper about unifying the underlying vim scripts.
Thanks again.
benc [at] bur [dot] st, October 18, 2004 17:38
I am glad that someone found this script useful. I find anything that reduces the time I spend in Lotus Notes to be a good thing. :-)

Thanks for the suggestions, there are quite a few changes that need to be made to create a real out-of-the-box solution.

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.

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
Anonymous, October 18, 2004 18:59
beautiful
Anonymous, October 18, 2004 19:00
use it in microsoft outlook to edit email/calendar/tasks is good
benc [at] bur [dot] st, October 19, 2004 16:53
I had a better look at Hari Krishna Dara's script: vimscript#1014 and the modification I called for in step 1 above is completely unnecessary. The unmodified script will update the "* register if you type :wq after doing your editing. The amended tip should read:

Step 1.
=======
Download the following scripts:
  * clipbrd.vim (vimscript#1014)
  * genutils.vim (vimscript#197)
  * multvals.vim (vimscript#171)
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 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".

-----8<-----
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}"
-----8<-----

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.

Thanks for your suggestions.

Cheers,
Ben.
hari_vim at yahoo dot com, October 19, 2004 20:23
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:

---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
---vimEditClipbrd.au3---

Follow the same steps as the above comment (ie., 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

Thanks to Ben for making it easier to use clipbrd.vim plugin.
Hari
[email protected], October 20, 2004 0:24
Works great! But I would like to know why when I am using Mozilla to compose a message the text comes back from gVim quoted (with ">" marks).

Marcelo
Anonymous, October 20, 2004 23:10
Excellent! but would it be possible to open the ClipBrd in an already running gvim session?

I'm not sure, but in combination with the --remote options might do?
net dot hiwaay at gbacon, October 21, 2004 10:26
On Win2k, there seems to be trouble with loading the clipboard -- Ctrl-Alt-V produces a Vim window with the previous contents of the clipboard, not what's in the cheesy editor I'm trying to escape -- but the pastes are successful.  Yes, runvim.vbs has the ^A-^C sequence.

I tried with Outlook, Mozilla, and notepad.  No joy.
benc [at] bur [dot] st, October 21, 2004 16:22
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

Cheers,
Ben
net dot hiwaay at gbacon, October 22, 2004 5:24
Thanks, but still no joy.

As I mentioned, I tried with Outlook and notepad, applications that understand Ctrl-A.  The paste that runs after vim successfully copies what's in vim, but it's the initial copy that doesn't seem to fire: vim starts with the contents of the old clipboard.
hari_vim at yahoo dot com, October 22, 2004 22:41
Did you try ifthe 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.
Klaus Horsten, October 28, 2004 6:51
Hallo,

great idea!

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.

Hope that helps.

Besides:

Trojan warning

autoit3

On one of the downloads (in one of the zip files) there is a trojan!

http://www.autoitscript.com/autoit3/downloads.php


Regards,

Klaus
[email protected], November 12, 2004 14:03
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!  Very, very happy to have found this tip!

John Couch
Tivoli Administrator
Novant Health
paul_evans7<at>hotmail<dot>com, January 12, 2005 7:24
Wicked Ben!

This gets my Best-of-2004 vote!

also thanks to Hari for the great AutoIt3 meta-tip...

(this note was written in gvim from the opera browser)

Cheers,
Paul
jindan at gmail dot com, January 18, 2005 10:49
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?
Ben Collerson - benc at bur dot st, January 28, 2005 18:15
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


Bill Wert billwert at gmail dot com, February 21, 2005 14:20
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\Gvim.exe +ClipBrd +only",0,True)
Bill Wert billwert at gmail d0t com, March 4, 2005 17:28
Ack!  I just realized there's a typo in my above comment.

You should set the shortcut target to:
C:\WINDOWS\system32\wscript.exe C:\vim\vim63\runvim.vbs
[email protected], June 6, 2005 14:51
Hello, how do I use this with the desktop locked? Unfortunately this is when I have to run most of my tools when the client is not on the desktop.

Thank you,

Ace Williams
Anonymous, December 16, 2005 19:09
Excellent!!!
[email protected], March 8, 2006 15:16
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.
[email protected], March 21, 2006 1:23
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.
frioux [~(a@@@t)~] gmail (dot) com, April 25, 2006 22:23
This is really awesome.  I am using it right now if not only for the novelty of
it.  I am thinking it will be really good when posting on forums and stuff since
I can easily spell check and stuff like that then.

I was wondering if anyone knew if this is possible on linux?  If so, I am very
interested.
muteki, May 15, 2006 20:44
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:50
Ah, I noticed clipbrd.vim is where overriding all those options.
jmartin at no-spam-please dot infusco dot net, June 6, 2006 2:45
If you're using PortableGVim [1], 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.

[1] http://portablegvim.sourceforge.net/
[email protected], July 22, 2006 3:58
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 <SNR>11_ViewRegister..<SNR>11_SetupBuf:

line    1:
E117: Unknown function: SetupScratchBuffer
Error detected while processing function SNR>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.

regards,
Rajendra
rainer [period] grohmann [at] gmx [period] net, September 6, 2006 3:58
I just installed Ben's program external (http://bur.st/~benc/?p=external) for vim 7.0 on windows xp. I found, that the vim-plugin external.vim included in the zip-file needed a little refinement:

the section
------8<--------8<------
  " reset EnhancedCommentify.vim
  if exists(b:ECdidBufferInit)
    unlet b:ECdidBufferInit
    call EnhancedCommentifyInitBuffer()
  endif
------8<--------8<------
caused error messages at vim-startup. Adding double quotes to the call to exists removed that problem.

So:
------8<--------8<------
  " reset EnhancedCommentify.vim
  if exists("b:ECdidBufferInit")
    unlet b:ECdidBufferInit
    call EnhancedCommentifyInitBuffer()
  endif
------8<--------8<------
works flawlessly (up to now)

Best,
   Rainer

p.s.: this tip gives great relief to me when working with windows -- i no longer have to bear crappy editors in all those programs.
Thanks a lot!
benc [at] bur [dot] st, December 3, 2006 0:39
I made the change suggested by rainer and referred to me by zzapper. The updated version is at the same place:

http://bur.st/~benc/?p=external
[email protected], February 3, 2007 7:08
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.

Tetsu
[email protected], January 8, 2007 11:59
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.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo