Running command in existing shell from gvim

最后都变了- 提交于 2020-01-05 07:24:19

问题


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, e.g.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.


回答1:


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 (e.g. 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.




回答2:


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.




回答3:


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.




回答4:


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 :).



来源:https://stackoverflow.com/questions/12088976/running-command-in-existing-shell-from-gvim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!