Christian Brabandt | b4ddc6c | 2024-01-02 16:51:11 +0100 | [diff] [blame] | 1 | *remote.txt* For Vim version 9.1. Last change: 2022 Feb 17 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Vim client-server communication *client-server* |
| 8 | |
| 9 | 1. Common functionality |clientserver| |
| 10 | 2. X11 specific items |x11-clientserver| |
| 11 | 3. MS-Windows specific items |w32-clientserver| |
| 12 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | ============================================================================== |
| 14 | 1. Common functionality *clientserver* |
| 15 | |
| 16 | When compiled with the |+clientserver| option, Vim can act as a command |
| 17 | server. It accepts messages from a client and executes them. At the same |
| 18 | time, Vim can function as a client and send commands to a Vim server. |
| 19 | |
| 20 | The following command line arguments are available: |
| 21 | |
| 22 | argument meaning ~ |
| 23 | |
| 24 | --remote [+{cmd}] {file} ... *--remote* |
| 25 | Open the file list in a remote Vim. When |
| 26 | there is no Vim server, execute locally. |
| 27 | There is one optional init command: +{cmd}. |
| 28 | This must be an Ex command that can be |
| 29 | followed by "|". |
| 30 | The rest of the command line is taken as the |
| 31 | file list. Thus any non-file arguments must |
| 32 | come before this. |
| 33 | You cannot edit stdin this way |--|. |
| 34 | The remote Vim is raised. If you don't want |
| 35 | this use > |
| 36 | vim --remote-send "<C-\><C-N>:n filename<CR>" |
Bram Moolenaar | f3c2afb | 2015-03-02 23:16:07 +0100 | [diff] [blame] | 37 | < |
| 38 | --remote-silent [+{cmd}] {file} ... *--remote-silent* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | As above, but don't complain if there is no |
| 40 | server and the file is edited locally. |
| 41 | --remote-wait [+{cmd}] {file} ... *--remote-wait* |
| 42 | As --remote, but wait for files to complete |
| 43 | (unload) in remote Vim. |
| 44 | --remote-wait-silent [+{cmd}] {file} ... *--remote-wait-silent* |
| 45 | As --remote-wait, but don't complain if there |
| 46 | is no server. |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 47 | *--remote-tab* |
Bram Moolenaar | 038221b | 2006-03-11 21:32:59 +0000 | [diff] [blame] | 48 | --remote-tab Like --remote but open each file in a new |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 49 | tabpage. |
| 50 | *--remote-tab-silent* |
Bram Moolenaar | 038221b | 2006-03-11 21:32:59 +0000 | [diff] [blame] | 51 | --remote-tab-silent Like --remote-silent but open each file in a |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 52 | new tabpage. |
Bram Moolenaar | 038221b | 2006-03-11 21:32:59 +0000 | [diff] [blame] | 53 | *--remote-tab-wait* |
| 54 | --remote-tab-wait Like --remote-wait but open each file in a new |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 55 | tabpage. |
Bram Moolenaar | 038221b | 2006-03-11 21:32:59 +0000 | [diff] [blame] | 56 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 57 | *--remote-tab-wait-silent* |
Bram Moolenaar | 038221b | 2006-03-11 21:32:59 +0000 | [diff] [blame] | 58 | --remote-tab-wait-silent Like --remote-wait-silent but open each file |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 59 | in a new tabpage. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | *--servername* |
| 61 | --servername {name} Become the server {name}. When used together |
| 62 | with one of the --remote commands: connect to |
| 63 | server {name} instead of the default (see |
Bram Moolenaar | 675e8d6 | 2018-06-24 20:42:01 +0200 | [diff] [blame] | 64 | below). The name used will be uppercase. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | *--remote-send* |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 66 | --remote-send {keys} Send {keys} to server and exit. The {keys} |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 67 | are not mapped. Special key names are |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 68 | recognized, e.g., "<CR>" results in a CR |
| 69 | character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | *--remote-expr* |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 71 | --remote-expr {expr} Evaluate {expr} in server and print the result |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 72 | on stdout. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | *--serverlist* |
| 74 | --serverlist Output a list of server names. |
| 75 | |
| 76 | |
| 77 | Examples ~ |
| 78 | |
| 79 | Edit "file.txt" in an already running GVIM server: > |
| 80 | gvim --remote file.txt |
| 81 | |
| 82 | Edit "file.txt" in an already running server called FOOBAR: > |
| 83 | gvim --servername FOOBAR --remote file.txt |
| 84 | |
| 85 | Edit "file.txt" in server "FILES" if it exists, become server "FILES" |
| 86 | otherwise: > |
| 87 | gvim --servername FILES --remote-silent file.txt |
| 88 | |
| 89 | This doesn't work, all arguments after --remote will be used as file names: > |
| 90 | gvim --remote --servername FOOBAR file.txt |
| 91 | |
| 92 | Edit file "+foo" in a remote server (note the use of "./" to avoid the special |
| 93 | meaning of the leading plus): > |
| 94 | vim --remote ./+foo |
| 95 | |
| 96 | Tell the remote server "BLA" to write all files and exit: > |
| 97 | vim --servername BLA --remote-send '<C-\><C-N>:wqa<CR>' |
| 98 | |
| 99 | |
Bram Moolenaar | f55e4c8 | 2017-08-01 20:44:53 +0200 | [diff] [blame] | 100 | SERVER NAME *client-server-name* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | |
| 102 | By default Vim will try to register the name under which it was invoked (gvim, |
| 103 | egvim ...). This can be overridden with the --servername argument. If the |
| 104 | specified name is not available, a postfix is applied until a free name is |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 105 | encountered, i.e. "gvim1" for the second invocation of gvim on a particular |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | X-server. The resulting name is available in the servername builtin variable |
| 107 | |v:servername|. The case of the server name is ignored, thus "gvim" and |
| 108 | "GVIM" are considered equal. |
| 109 | |
| 110 | When Vim is invoked with --remote, --remote-wait or --remote-send it will try |
| 111 | to locate the server name determined by the invocation name and --servername |
| 112 | argument as described above. If an exact match is not available, the first |
| 113 | server with the number postfix will be used. If a name with the number |
| 114 | postfix is specified with the --servername argument, it must match exactly. |
| 115 | |
| 116 | If no server can be located and --remote or --remote-wait was used, Vim will |
| 117 | start up according to the rest of the command line and do the editing by |
| 118 | itself. This way it is not necessary to know whether gvim is already started |
| 119 | when sending command to it. |
| 120 | |
| 121 | The --serverlist argument will cause Vim to print a list of registered command |
| 122 | servers on the standard output (stdout) and exit. |
Bram Moolenaar | 944697a | 2022-02-20 19:48:20 +0000 | [diff] [blame] | 123 | *{server}* |
| 124 | The {server} argument is used by several functions. When this is an empty |
| 125 | string then on Unix the default server name is used, which is "GVIM". On |
| 126 | MS-Windows an empty string does not work. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | |
| 128 | Win32 Note: Making the Vim server go to the foreground doesn't always work, |
| 129 | because MS-Windows doesn't allow it. The client will move the server to the |
| 130 | foreground when using the --remote or --remote-wait argument and the server |
| 131 | name starts with "g". |
| 132 | |
| 133 | |
| 134 | REMOTE EDITING |
| 135 | |
| 136 | The --remote argument will cause a |:drop| command to be constructed from the |
| 137 | rest of the command line and sent as described above. |
| 138 | The --remote-wait argument does the same thing and additionally sets up to |
| 139 | wait for each of the files to have been edited. This uses the BufUnload |
| 140 | event, thus as soon as a file has been unloaded, Vim assumes you are done |
| 141 | editing it. |
| 142 | Note that the --remote and --remote-wait arguments will consume the rest of |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 143 | the command line. I.e. all remaining arguments will be regarded as filenames. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | You can not put options there! |
| 145 | |
| 146 | |
| 147 | FUNCTIONS |
| 148 | *E240* *E573* |
| 149 | There are a number of Vim functions for scripting the command server. See |
Bram Moolenaar | 1cae5a0 | 2021-12-27 21:28:34 +0000 | [diff] [blame] | 150 | the description in |builtin.txt| or use CTRL-] on the function name to jump to |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | the full explanation. |
| 152 | |
| 153 | synopsis explanation ~ |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 154 | remote_startserver( name) run a server |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | remote_expr( server, string, idvar) send expression |
| 156 | remote_send( server, string, idvar) send key sequence |
| 157 | serverlist() get a list of available servers |
| 158 | remote_peek( serverid, retvar) check for reply string |
| 159 | remote_read( serverid) read reply string |
| 160 | server2client( serverid, string) send reply string |
| 161 | remote_foreground( server) bring server to the front |
| 162 | |
| 163 | See also the explanation of |CTRL-\_CTRL-N|. Very useful as a leading key |
| 164 | sequence. |
| 165 | The {serverid} for server2client() can be obtained with expand("<client>") |
| 166 | |
| 167 | ============================================================================== |
| 168 | 2. X11 specific items *x11-clientserver* |
| 169 | *E247* *E248* *E251* *E258* *E277* |
| 170 | |
| 171 | The communication between client and server goes through the X server. The |
| 172 | display of the Vim server must be specified. The usual protection of the X |
| 173 | server is used, you must be able to open a window on the X server for the |
| 174 | communication to work. It is possible to communicate between different |
| 175 | systems. |
| 176 | |
| 177 | By default, a GUI Vim will register a name on the X-server by which it can be |
| 178 | addressed for subsequent execution of injected strings. Vim can also act as |
| 179 | a client and send strings to other instances of Vim on the same X11 display. |
| 180 | |
| 181 | When an X11 GUI Vim (gvim) is started, it will try to register a send-server |
| 182 | name on the 'VimRegistry' property on the root window. |
| 183 | |
| 184 | A non GUI Vim with access to the X11 display (|xterm-clipboard| enabled), can |
| 185 | also act as a command server if a server name is explicitly given with the |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 186 | --servername argument, or when Vim was built with the |+autoservername| |
Bram Moolenaar | a2a8016 | 2017-11-21 23:09:50 +0100 | [diff] [blame] | 187 | feature. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 188 | |
| 189 | An empty --servername argument will cause the command server to be disabled. |
| 190 | |
| 191 | To send commands to a Vim server from another application, read the source |
| 192 | file src/if_xcmdsrv.c, it contains some hints about the protocol used. |
| 193 | |
| 194 | ============================================================================== |
| 195 | 3. Win32 specific items *w32-clientserver* |
| 196 | |
| 197 | Every Win32 Vim can work as a server, also in the console. You do not need a |
| 198 | version compiled with OLE. Windows messages are used, this works on any |
| 199 | version of MS-Windows. But only communication within one system is possible. |
| 200 | |
| 201 | Since MS-Windows messages are used, any other application should be able to |
| 202 | communicate with a Vim server. An alternative is using the OLE functionality |
| 203 | |ole-interface|. |
| 204 | |
| 205 | When using gvim, the --remote-wait only works properly this way: > |
| 206 | |
| 207 | start /w gvim --remote-wait file.txt |
| 208 | < |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 209 | vim:tw=78:sw=4:ts=8:noet:ft=help:norl: |