Vimscript: Run Command in Terminal
~ 2 minute read.
A more recent vim feature is :term, the ability to have a terminal
inside vim. This super useful feature now allows you to switch between
your code and asynchroneously run compile commands very easily.
Since I use Visual C++ on Windows, I need to run vcvarsall.bat in
my console to allow CMake to properly find the compiler. To do so I
now set up a vim command :Vcvarsall which does this for me so that
I don’t have to remember the path. Using term_sendkeys(buf, keys),
you can easily send text into the terminal from a vim command:
let $VCVARSALL = 'C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/vcvarsall.bat' command! Vcvarsall call term_sendkeys(bufnr("%"), "\"%VCVARSALL%\" x64\<CR>")
The first line is the “easy way”, just an environment variable containing the path.
Since that still requires a bit of awkward % typing, I defined the command to do it
for me. bufnr("%") is just a way to get the current buffer. Hence this command
only works through C-W : in a terminal buffer.
Written in 5 minutes