Possible Duplicate:
Commands executed from vim are not recognizing bash command aliases
I have the following function in my ~/.bashrc file (on my Ubunut box)
# convert tex to pdf, no bib
function t2p {
latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi }
# convert tex to pdf, with bib
function tb2p {
latex $1 && bibtex $1 && latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi }
For example, to convert a tex file f.tex to 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 :sh first.
To simplify the above procedure I tried to execute the functions t2p and tb2p inside 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.bashrc to make them visible to Vim. Unfortunately, this didn't work in my case. Vim still doesn't know about the function.
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:
- How can I let Vim know about ~/.bashrc functions?
- How do I setup a开发者_运维百科 keyboard shortcut in ~/.vimrc for a function in ~/.bashrc?
Thank you very, very much.
Try using :!bash -c t2p
in Vim. If your alias is limited to interactive shells also add the -i
flag.
Vim
runs bash
commands with the -c
argument, which makes the shell non-interactive and non-login, and that bypasses reading the startup files.
You can override this with an environment variable.
From man 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 shell
we get the following:
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|.
Try adding set shell=bash\ --login
to your .vimrc.
To convert the current file, type :!tb2p %
, the %
will be expanded by Vim for you when executing the script.
Once it's working, you can add a mapping to make the whole process even faster:
nnoremap <F12> :!tb2p %<CR>
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
In your case for t2p:
create a file t2p in ~/bin with the contents:
#!/bin/bash
# convert tex to pdf, no bib
latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi
then make the file executable:
> chmod +x t2p
make sure ~/bin is in your path, by putting the following in your ~/.bashrc:
export PATH=${HOME}/bin:${PATH}
精彩评论