17

My questions is one that I haven't seen answered, the usual question is how to run certain commands to a file.

My question is how to run Vim commands or functions, from outside Vim (i.e. Terminal), these wouldn't affect any file, just Vim itself. Is this is even possible? If so, how?

If this is not possible like this, is there a way to go into Vim, run a command automatically, and then exit when that ends? Or run another command and then exit?

Thanks for your help!

greduan
  • 4,770
  • 6
  • 45
  • 73
  • Do you want to use Vim for batch processing (like sed, awk, ...)? You said these "wouldn't affect any file", though. What's then the use of launching Vim? – Ingo Karkat Nov 23 '12 at 07:35
  • The idea is tu run a couple of maintenance tasks that I have inside Vim, but these don't affect any files. – greduan Nov 23 '12 at 13:16
  • 1
    I'm still curious; what kind of "maintenance" are you doing from inside Vim? (It's a text editor, after all, for modifying text files!) – Ingo Karkat Nov 23 '12 at 13:39
  • 1
    I'm just trying to write a script that will open Vim, run `:BundleInstall!` and get out, it's for first uses in new computers, so that my bundles are installed and ready to go (I use Vundle BTW). If you're curious you can go to my dotfiles repo: github.com/Greduan/dotfiles – greduan Nov 23 '12 at 13:46
  • @Eduan VAM (vim-addon-manager) has better behavior in this case: it will just install missing plugins requiring some `y` answers though (unless configured to do this without confirmation). Plugins are then accessed normally in the current vim session without vim restart. – ZyX Nov 23 '12 at 14:00
  • Thanks for the details; that explains your need. I personally use the _Unison_ tool to synchronize my dotfiles, but your approach is common and valid, too. – Ingo Karkat Nov 23 '12 at 14:51
  • @IngoKarkat Thanks for the tip, I might try Unison out. BTW, why do people keep disliking my questions? It's not like they're bad questions... – greduan Nov 23 '12 at 16:52
  • 2
    Next time, try to give more background, what your final goal is. When a question is only about a small technical step, it's difficult to provide a good answer. You did a lot of clarification in the comments here, and your question shows interest and effort, so it'll certainly get better! Keep up the good work! – Ingo Karkat Nov 23 '12 at 20:44
  • Ah I see. Thanks Ingo Karkat! I'll provide more info next time then. :) – greduan Nov 23 '12 at 20:53

1 Answers1

28

Use

vim --cmd 'Command launched before vimrc' \
     -c 'Command launched after vimrc' \
     -c 'qa!' # Quit vim

. For running a command in an existing vim session you have to use +clientserver feature: run one vim with

vim --servername vim

and others with

vim --remote-send '<C-\><C-n>:Command<CR>'
ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Wow! nice. It works great, however there pops a "hit enter prompt" up. Is there a flag or something to skip this? – The Fool Jan 05 '19 at 19:05
  • 2
    @TheFool Hit enter prompt presence highly depends on which command you are running, it is not always there. Maybe you may get that silenced with `:silent`. There is also an option (`shortmess`) which allows avoiding some hit enter prompts. Also see `:h hit-enter`. – ZyX Jan 07 '19 at 19:12