从 Vim 中执行 Bash 函数 - 我该怎么做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6162809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Bash functions from within Vim - how can I do it?
提问by
Possible Duplicate:
Commands executed from vim are not recognizing bash command aliases
I have the following function in my ~/.bashrcfile (on my Ubunut box)
我的~/.bashrc文件中有以下功能(在我的 Ubunut 盒子上)
# convert tex to pdf, no bib
function t2p {
latex && latex && dvips -o .ps && ps2pdf .ps && rm -f .dvi }
# convert tex to pdf, with bib
function tb2p {
latex && bibtex && latex && latex && dvips -o .ps && ps2pdf .ps && rm -f .dvi }
For example, to convert a tex file f.texto a pdf file and bibtex it in the right order, I call tb2p f. This works very well if I'm in a Bash terminal. However, to get to the Bash prompt from within Vim I actually have to execute the command :shfirst.
例如,要将 tex 文件f.tex转换为 pdf 文件并按正确的顺序进行 bibtex,我调用tb2p f。如果我在 Bash 终端中,这非常有效。但是,要从 Vim 中进入 Bash 提示符,我实际上必须先执行命令:sh。
To simplify the above procedure I tried to execute the functions t2pand tb2pinside Vim by :!t2p f. Vim then tells me that it cannot find the function t2p. I did some Googling and read that I should put these functions into the file /etc/bash.bashrcto make them visible to Vim. Unfortunately, this didn't work in my case. Vim still doesn't know about the function.
为了简化上述过程,我尝试通过:!t2p f在 Vim 中执行函数t2p和tb2p。Vim 然后告诉我它找不到函数t2p。我做了一些谷歌搜索并读到我应该将这些函数放入文件/etc/bash.bashrc 中,以使它们对 Vim 可见。不幸的是,这在我的情况下不起作用。Vim 仍然不知道这个函数。
At the end of the day I would like to be able to call my functions from within Vim by using a keyboard shortcut. My questions are therefore as follows:
归根结底,我希望能够使用键盘快捷键从 Vim 中调用我的函数。因此,我的问题如下:
- How can I let Vim know about ~/.bashrcfunctions?
- How do I setup a keyboard shortcut in ~/.vimrcfor a function in ~/.bashrc?
- 我怎样才能让 Vim 知道~/.bashrc函数?
- 如何在~/.vimrc 中为~/.bashrc 中的函数设置键盘快捷键?
Thank you very, very much.
非常非常感谢你。
回答by Adam Byrtek
Try using :!bash -c t2pin Vim. If your alias is limited to interactive shells also add the -iflag.
尝试:!bash -c t2p在 Vim 中使用。如果您的别名仅限于交互式 shell,则还要添加该-i标志。
回答by DigitalRoss
Vimruns bashcommands with the -cargument, which makes the shell non-interactive and non-login, and that bypasses reading the startup files.
Vim运行bash带有-c参数的命令,这使得 shell 非交互式和非登录,并且绕过读取启动文件。
You can override this with an environment variable.
您可以使用环境变量覆盖它。
From man bash:
从人 bash:
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV
in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to
read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
Alternatively, vim itself can be asked to run command shells as login shells. From vim :help shellwe get the following:
或者,可以要求 vim 本身将命令 shell 作为登录 shell 运行。从vim :help shell我们得到以下信息:
On Unix the command normally runs in a non-interactive
shell. If you want an interactive shell to be used
(to use aliases) set 'shellcmdflag' to "-ic".
For Win32 also see |:!start|.
回答by romainl
Try adding set shell=bash\ --loginto your .vimrc.
尝试添加set shell=bash\ --login到您的 .vimrc。
To convert the current file, type :!tb2p %, the %will be expanded by Vim for you when executing the script.
要转换当前文件,请键入:!tb2p %,%Vim 将在执行脚本时为您展开该文件。
Once it's working, you can add a mapping to make the whole process even faster:
一旦它工作,你可以添加一个映射来使整个过程更快:
nnoremap <F12> :!tb2p %<CR>
回答by skeept
You can always define your functions in a separate file and put that file in a folder in your PATH environment variable. In my case my personal functions that I would use go to ~/bin
您始终可以在单独的文件中定义函数,并将该文件放在 PATH 环境变量中的文件夹中。就我而言,我将使用的个人功能转到 ~/bin
In your case for t2p:
在您的 t2p 情况下:
create a file t2p in ~/bin with the contents:
在 ~/bin 中创建一个文件 t2p,内容如下:
#!/bin/bash
# convert tex to pdf, no bib
latex && latex && dvips -o .ps && ps2pdf .ps && rm -f .dvi
then make the file executable:
然后使文件可执行:
> chmod +x t2p
make sure ~/bin is in your path, by putting the following in your ~/.bashrc:
通过将以下内容放入 ~/.bashrc 中,确保 ~/bin 在您的路径中:
export PATH=${HOME}/bin:${PATH}

