简体   繁体   中英

Running command in existing shell from gvim

It all starts from a shell. For example I am using urxvt with zsh . There I open some file with gvim . In this case it is a LaTeX file. Now I need to execute some command (for compiling the document, eg pdflatex ).

How can I have the original shell, from where gvim was started, execute that command?

It would also be acceptable if gvim had to open a new shell once and after that execute every future call of the designated command ( pdflatex ) in that shell, while I can still type in it manually.

The problem with running :!pdflatex directly is, that the output is shown, but if it has gotten too long scrolling is not possible and after I press return, it is all gone.

The idea with using a shell means, that the window focus does not have to switch over by default. So in general the output of my command is visible, but unless an error occurs I can just keep on working in gvim . Now if a new shell was spawned everytime I run the command, this kind of workflow would certainly not be possible.

GVIM does not retain a "handle" to the shell that launched it in a way that allows it to send commands back to it. Because of they synchronous execution, you also cannot launch a shell from GVIM, keep feeding it commands while also continue working in GVIM.

I'm afraid you have to use the functionality of your window manager to launch (and then later re-activate) a shell window, and send the commands as keystrokes to it. On Windows, this can be done (eg in VBScript) via WshShell 's Run() , AppActivate() and SendKeys() methods; there are probably similar mechanisms for window control on Linux, too.

If you don't mind having that shell inside your GVIM (emulated, with all its drawbacks), though, there are plugins that enable that.

You might want to use Conque . It has drawbacks (can be slow, not so frequently updated, etc) but at least it works for what you expect.

The Vim Wiki as this recipe, which I think will solve your problem by completely removing the need for an external shell. Here it is, in case the source goes dark:

let b:tex_flavor = 'pdflatex'
compiler tex
set makeprg=pdflatex\ \-file\-line\-error\ \-interaction=nonstopmode
set errorformat=%f:%l:\ %m

Basically, you use :make to compile and the quickfix window ( :copen ) to list errors.

You can run shell commands in vim by calling !command .

For your particular use case of running pdflatex, I added the following shortcut in my /etc/vimrc :

:nmap <F5> :w<CR>:make<CR>

:make will call gnu make in the current directory which will eventually call pdflatex . This is very flexible since the same shortcut will do different thing depending on the directory where you are (if you are coding in C, it will typically call gcc ). But you have to be fluent with make :).

So hitting F5 saves my document and compiles it, and saved me hours of typing for multiple years now :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM