Executing Bash functions from within Vim - how can I do it? [duplicate]

落爺英雄遲暮 提交于 2019-12-21 04:13:40

问题


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:

  1. How can I let Vim know about ~/.bashrc functions?
  2. How do I setup a keyboard shortcut in ~/.vimrc for a function in ~/.bashrc?

Thank you very, very much.


回答1:


Try using :!bash -c t2p in Vim. If your alias is limited to interactive shells also add the -i flag.




回答2:


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>



回答3:


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



回答4:


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}


来源:https://stackoverflow.com/questions/6162809/executing-bash-functions-from-within-vim-how-can-i-do-it

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