blob: c546e971a65af051de295a23bb7c5b32b662beac [file] [log] [blame]
Christian Brabandtb4ddc6c2024-01-02 16:51:11 +01001*if_ole.txt* For Vim version 9.1. Last change: 2023 Nov 19
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
Bram Moolenaar25c9c682019-05-05 18:13:34 +020015{only available when compiled with the |+ole| feature. See
16src/if_ole.INSTALL}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017An alternative is using the client-server communication |clientserver|.
18
19==============================================================================
201. Activation *ole-activation*
21
22Vim acts as an OLE automation server, accessible from any automation client,
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000023for example, Visual Basic, Python, or Perl. The Vim application "name" (its
Bram Moolenaar071d4272004-06-13 20:20:40 +000024"ProgID", in OLE terminology) is "Vim.Application".
25
26Hence, in order to start a Vim instance (or connect to an already running
27instance), code similar to the following should be used:
28
29[Visual Basic] >
30 Dim Vim As Object
31 Set Vim = CreateObject("Vim.Application")
32
33[Python] >
34 from win32com.client.dynamic import Dispatch
35 vim = Dispatch('Vim.Application')
36
37[Perl] >
38 use Win32::OLE;
39 $vim = new Win32::OLE 'Vim.Application';
40
Bram Moolenaarff1d0d42007-05-10 17:24:16 +000041[C#] >
zeertzjqd086b8f2024-02-25 15:42:52 +080042 // Add a reference to Vim in your project.
43 // Choose the COM tab.
44 // Select "Vim Ole Interface 1.1 Type Library"
Bram Moolenaarff1d0d42007-05-10 17:24:16 +000045 Vim.Vim vimobj = new Vim.Vim();
46
Bram Moolenaar071d4272004-06-13 20:20:40 +000047Vim does not support acting as a "hidden" OLE server, like some other OLE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000048Automation servers. When a client starts up an instance of Vim, that instance
49is immediately visible. Simply closing the OLE connection to the Vim instance
Bram Moolenaar071d4272004-06-13 20:20:40 +000050is not enough to shut down the Vim instance - it is necessary to explicitly
51execute a quit command (for example, :qa!, :wqa).
52
53==============================================================================
542. Methods *ole-methods*
55
56Vim exposes four methods for use by clients.
57
58 *ole-sendkeys*
59SendKeys(keys) Execute a series of keys.
60
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000061This method takes a single parameter, which is a string of keystrokes. These
njohnstonaabca252023-11-19 23:18:57 +000062keystrokes are executed exactly as if they had been typed in at the keyboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +000063Special keys can be given using their <..> names, as for the right hand side
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000064of a mapping. Note: Execution of the Ex "normal" command is not supported -
Bram Moolenaar071d4272004-06-13 20:20:40 +000065see below |ole-normal|.
66
67Examples (Visual Basic syntax) >
68 Vim.SendKeys "ihello<Esc>"
69 Vim.SendKeys "ma1GV4jy`a"
70
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000071These examples assume that Vim starts in Normal mode. To force Normal mode,
Bram Moolenaar071d4272004-06-13 20:20:40 +000072start the key sequence with CTRL-\ CTRL-N as in >
73
74 Vim.SendKeys "<C-\><C-N>ihello<Esc>"
75
76CTRL-\ CTRL-N returns Vim to Normal mode, when in Insert or Command-line mode.
77Note that this doesn't work halfway a Vim command
78
79 *ole-eval*
80Eval(expr) Evaluate an expression.
81
82This method takes a single parameter, which is an expression in Vim's normal
83format (see |expression|). It returns a string, which is the result of
Bram Moolenaar362e1a32006-03-06 23:29:24 +000084evaluating the expression. A |List| is turned into a string by joining the
85items and inserting line breaks.
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
87Examples (Visual Basic syntax) >
88 Line20 = Vim.Eval("getline(20)")
89 Twelve = Vim.Eval("6 + 6") ' Note this is a STRING
90 Font = Vim.Eval("&guifont")
91<
92 *ole-setforeground*
93SetForeground() Make the Vim window come to the foreground
94
95This method takes no arguments. No value is returned.
96
97Example (Visual Basic syntax) >
98 Vim.SetForeground
99<
100
101 *ole-gethwnd*
102GetHwnd() Return the handle of the Vim window.
103
104This method takes no arguments. It returns the hwnd of the main Vimwindow.
105You can use this if you are writing something which needs to manipulate the
106Vim window, or to track it in the z-order, etc.
107
108Example (Visual Basic syntax) >
109 Vim_Hwnd = Vim.GetHwnd
110<
111
112==============================================================================
1133. The "normal" command *ole-normal*
114
115Due to the way Vim processes OLE Automation commands, combined with the method
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100116of implementation of the Ex command :normal, it is not possible to execute the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000117:normal command via OLE automation. Any attempt to do so will fail, probably
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118harmlessly, although possibly in unpredictable ways.
119
120There is currently no practical way to trap this situation, and users must
121simply be aware of the limitation.
122==============================================================================
1234. Registration *ole-registration* *E243*
124
125Before Vim will act as an OLE server, it must be registered in the system
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000126registry. In order to do this, Vim should be run with a single parameter of
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127"-register".
128 *-register* >
129 gvim -register
130
131If gvim with OLE support is run and notices that no Vim OLE server has been
132registered, it will present a dialog and offers you the choice to register by
133clicking "Yes".
134
135In some situations registering is not possible. This happens when the
136registry is not writable. If you run into this problem you need to run gvim
137as "Administrator".
138
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000139Once vim is registered, the application path is stored in the registry.
140Before moving, deleting, or upgrading Vim, the registry entries should be
141removed using the "-unregister" switch.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 *-unregister* >
143 gvim -unregister
144
145The OLE mechanism will use the first registered Vim it finds. If a Vim is
146already running, this one will be used. If you want to have (several) Vim
147sessions open that should not react to OLE commands, use the non-OLE version,
148and put it in a different directory. The OLE version should then be put in a
149directory that is not in your normal path, so that typing "gvim" will start
150the non-OLE version.
151
152 *-silent*
153To avoid the message box that pops up to report the result, prepend "-silent":
154>
155 gvim -silent -register
156 gvim -silent -unregister
157
158==============================================================================
Martin Tournoij251c1e22022-10-08 17:15:28 +01001595. MS Visual Studio integration *MSVisualStudio*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160
Martin Tournoij251c1e22022-10-08 17:15:28 +0100161The old "VisVim" integration was removed from Vim in patch 9.0.0698.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000163
164Using Vim with Visual Studio .Net~
165
Martin Tournoij251c1e22022-10-08 17:15:28 +0100166.Net studio has support for external editors. Follow these directions:
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000167
168In .Net Studio choose from the menu Tools->External Tools...
169Add
170 Title - Vim
171 Command - c:\vim\vim63\gvim.exe
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000172 Arguments - --servername VS_NET --remote-silent "+call cursor($(CurLine), $(CurCol))" $(ItemPath)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000173 Init Dir - Empty
174
175Now, when you open a file in .Net, you can choose from the .Net menu:
176Tools->Vim
177
178That will open the file in Vim.
179You can then add this external command as an icon and place it anywhere you
180like. You might also be able to set this as your default editor.
181
182If you refine this further, please post back to the Vim maillist so we have a
183record of it.
184
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000185--servername VS_NET
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000186This will create a new instance of vim called VS_NET. So if you open multiple
187files from VS, they will use the same instance of Vim. This allows you to
188have multiple copies of Vim running, but you can control which one has VS
189files in it.
190
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000191--remote-silent "+call cursor(10, 27)"
192 - Places the cursor on line 10 column 27
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000193In Vim >
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100194 :h --remote-silent for more details
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000195
196[.Net remarks provided by Dave Fishburn and Brian Sturk]
197
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198==============================================================================
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200199 vim:tw=78:ts=8:noet:ft=help:norl: