13

Can I change a variable in an instance of vim from another shell (without sending keys through tmux)?

I ask because I would like to use latexmk's compiling_cmd, success_cmd, and failure_cmd options to show compilation status on the statusline.

Perhaps I could have vim look for the existence or non-existence of certain files (using files as boolean variables)? But this seems a bit dirty; hopefully there is a better way.

1 Answer 1

19

If you launch your first Vim session with the argument --servername VIM:

$ vim --servername VIM

then you can send any command from any other shell with the argument --remote-send.

For example, if you use your statusline to display the value of some variable g:myvar, and you have the following lines in your vimrc:

let g:myvar = "foo"
set stl+=%{g:myvar}

Then you can change its value (:let g:myvar = "bar") from another shell:

$ vim --remote-send ':let g:myvar = "bar"<cr>'

The statusline should reflect the change of the value.


As your last comment says it, if your Vim instance is not in normal mode, the previous command will not work as expected. There may be simpler, but here are 2 possible solutions:

1) add the key <esc> to be in normal mode
2) use the argument --remote-expr instead of --remote-send to evaluate an expression and write a small helper function whose code will assign the new value to your variable

First solution:

$ vim --remote-send '<esc>:let g:myvar = "bar"<cr>'

Second solution:

Define the following function in your vimrc:

function! Myfunc(value)
    let g:myvar = a:value
endfunction

Then, in your shell:

$ vim --remote-expr 'Myfunc("bar")'
3
  • Thank you! I see don't have this feature compiled in, so I'll recompile. Hopefully it's the same in neovim.
    – Toothrot
    Mar 24, 2016 at 13:49
  • 3
    @Lawrence I may be wrong, but I think neovim doesn't implement the +clientserver feature (github.com/tpope/vim-dispatch/issues/163#issuecomment-175383047). If that's the case, maybe this plugin would help you: github.com/mhinz/neovim-remote Mar 24, 2016 at 14:09
  • It seems that this has the same effect as sending keys with tmux, and depends on knowing which mode the vim server is in (the example assumes normal mode). Ideally, I'd want to be able to set a variable directly.
    – Toothrot
    Mar 24, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.