blob: e734df9847064d1b71e7bfbfa03e4aba7b29ec9e [file] [log] [blame]
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001*if_ole.txt* For Vim version 8.1. Last change: 2008 Aug 16
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Paul Moore
5
6
7The OLE Interface to Vim *ole-interface*
8
91. Activation |ole-activation|
102. Methods |ole-methods|
113. The "normal" command |ole-normal|
124. Registration |ole-registration|
135. MS Visual Studio integration |MSVisualStudio|
14
15{Vi does not have any of these commands}
16
17OLE is only available when compiled with the |+ole| feature. See
18src/if_ole.INSTALL.
19An alternative is using the client-server communication |clientserver|.
20
21==============================================================================
221. Activation *ole-activation*
23
24Vim acts as an OLE automation server, accessible from any automation client,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000025for example, Visual Basic, Python, or Perl. The Vim application "name" (its
Bram Moolenaar071d4272004-06-13 20:20:40 +000026"ProgID", in OLE terminology) is "Vim.Application".
27
28Hence, in order to start a Vim instance (or connect to an already running
29instance), code similar to the following should be used:
30
31[Visual Basic] >
32 Dim Vim As Object
33 Set Vim = CreateObject("Vim.Application")
34
35[Python] >
36 from win32com.client.dynamic import Dispatch
37 vim = Dispatch('Vim.Application')
38
39[Perl] >
40 use Win32::OLE;
41 $vim = new Win32::OLE 'Vim.Application';
42
Bram Moolenaarff1d0d42007-05-10 17:24:16 +000043[C#] >
Bram Moolenaar6aa8cea2017-06-05 14:44:35 +020044 // Add a reference to Vim in your project.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +000045 // Choose the COM tab.
Bram Moolenaar6aa8cea2017-06-05 14:44:35 +020046 // Select "Vim Ole Interface 1.1 Type Library"
Bram Moolenaarff1d0d42007-05-10 17:24:16 +000047 Vim.Vim vimobj = new Vim.Vim();
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049Vim does not support acting as a "hidden" OLE server, like some other OLE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000050Automation servers. When a client starts up an instance of Vim, that instance
51is immediately visible. Simply closing the OLE connection to the Vim instance
Bram Moolenaar071d4272004-06-13 20:20:40 +000052is not enough to shut down the Vim instance - it is necessary to explicitly
53execute a quit command (for example, :qa!, :wqa).
54
55==============================================================================
562. Methods *ole-methods*
57
58Vim exposes four methods for use by clients.
59
60 *ole-sendkeys*
61SendKeys(keys) Execute a series of keys.
62
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000063This method takes a single parameter, which is a string of keystrokes. These
Bram Moolenaar071d4272004-06-13 20:20:40 +000064keystrokes are executed exactly as if they had been types in at the keyboard.
65Special keys can be given using their <..> names, as for the right hand side
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000066of a mapping. Note: Execution of the Ex "normal" command is not supported -
Bram Moolenaar071d4272004-06-13 20:20:40 +000067see below |ole-normal|.
68
69Examples (Visual Basic syntax) >
70 Vim.SendKeys "ihello<Esc>"
71 Vim.SendKeys "ma1GV4jy`a"
72
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000073These examples assume that Vim starts in Normal mode. To force Normal mode,
Bram Moolenaar071d4272004-06-13 20:20:40 +000074start the key sequence with CTRL-\ CTRL-N as in >
75
76 Vim.SendKeys "<C-\><C-N>ihello<Esc>"
77
78CTRL-\ CTRL-N returns Vim to Normal mode, when in Insert or Command-line mode.
79Note that this doesn't work halfway a Vim command
80
81 *ole-eval*
82Eval(expr) Evaluate an expression.
83
84This method takes a single parameter, which is an expression in Vim's normal
85format (see |expression|). It returns a string, which is the result of
Bram Moolenaar362e1a32006-03-06 23:29:24 +000086evaluating the expression. A |List| is turned into a string by joining the
87items and inserting line breaks.
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89Examples (Visual Basic syntax) >
90 Line20 = Vim.Eval("getline(20)")
91 Twelve = Vim.Eval("6 + 6") ' Note this is a STRING
92 Font = Vim.Eval("&guifont")
93<
94 *ole-setforeground*
95SetForeground() Make the Vim window come to the foreground
96
97This method takes no arguments. No value is returned.
98
99Example (Visual Basic syntax) >
100 Vim.SetForeground
101<
102
103 *ole-gethwnd*
104GetHwnd() Return the handle of the Vim window.
105
106This method takes no arguments. It returns the hwnd of the main Vimwindow.
107You can use this if you are writing something which needs to manipulate the
108Vim window, or to track it in the z-order, etc.
109
110Example (Visual Basic syntax) >
111 Vim_Hwnd = Vim.GetHwnd
112<
113
114==============================================================================
1153. The "normal" command *ole-normal*
116
117Due to the way Vim processes OLE Automation commands, combined with the method
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100118of implementation of the Ex command :normal, it is not possible to execute the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000119:normal command via OLE automation. Any attempt to do so will fail, probably
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120harmlessly, although possibly in unpredictable ways.
121
122There is currently no practical way to trap this situation, and users must
123simply be aware of the limitation.
124==============================================================================
1254. Registration *ole-registration* *E243*
126
127Before Vim will act as an OLE server, it must be registered in the system
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000128registry. In order to do this, Vim should be run with a single parameter of
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129"-register".
130 *-register* >
131 gvim -register
132
133If gvim with OLE support is run and notices that no Vim OLE server has been
134registered, it will present a dialog and offers you the choice to register by
135clicking "Yes".
136
137In some situations registering is not possible. This happens when the
138registry is not writable. If you run into this problem you need to run gvim
139as "Administrator".
140
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000141Once vim is registered, the application path is stored in the registry.
142Before moving, deleting, or upgrading Vim, the registry entries should be
143removed using the "-unregister" switch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 *-unregister* >
145 gvim -unregister
146
147The OLE mechanism will use the first registered Vim it finds. If a Vim is
148already running, this one will be used. If you want to have (several) Vim
149sessions open that should not react to OLE commands, use the non-OLE version,
150and put it in a different directory. The OLE version should then be put in a
151directory that is not in your normal path, so that typing "gvim" will start
152the non-OLE version.
153
154 *-silent*
155To avoid the message box that pops up to report the result, prepend "-silent":
156>
157 gvim -silent -register
158 gvim -silent -unregister
159
160==============================================================================
1615. MS Visual Studio integration *MSVisualStudio* *VisVim*
162
163The OLE version can be used to run Vim as the editor in Microsoft Visual
164Studio. This is called "VisVim". It is included in the archive that contains
165the OLE version. The documentation can be found in the runtime directory, the
166README_VisVim.txt file.
167
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000168
169Using Vim with Visual Studio .Net~
170
171With .Net you no longer really need VisVim, since .Net studio has support for
172external editors. Follow these directions:
173
174In .Net Studio choose from the menu Tools->External Tools...
175Add
176 Title - Vim
177 Command - c:\vim\vim63\gvim.exe
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000178 Arguments - --servername VS_NET --remote-silent "+call cursor($(CurLine), $(CurCol))" $(ItemPath)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000179 Init Dir - Empty
180
181Now, when you open a file in .Net, you can choose from the .Net menu:
182Tools->Vim
183
184That will open the file in Vim.
185You can then add this external command as an icon and place it anywhere you
186like. You might also be able to set this as your default editor.
187
188If you refine this further, please post back to the Vim maillist so we have a
189record of it.
190
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000191--servername VS_NET
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000192This will create a new instance of vim called VS_NET. So if you open multiple
193files from VS, they will use the same instance of Vim. This allows you to
194have multiple copies of Vim running, but you can control which one has VS
195files in it.
196
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000197--remote-silent "+call cursor(10, 27)"
198 - Places the cursor on line 10 column 27
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000199In Vim >
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100200 :h --remote-silent for more details
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000201
202[.Net remarks provided by Dave Fishburn and Brian Sturk]
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204==============================================================================
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200205 vim:tw=78:ts=8:noet:ft=help:norl: