Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 18 down vote accepted

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")'
share|improve this answer
    
Thank you! I see don't have this feature compiled in, so I'll recompile. Hopefully it's the same in neovim. – Lawrence Mar 24 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 – user9433424 Mar 24 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. – Lawrence Mar 24 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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