Performing Bash Functions from Vim - How Can I Do It?

Possible duplicate:
Commands executed from vim do not recognize bash command aliases

I have the following function in my ~ / .bashrc file (in my Ubunut block)

# 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 tex f.tex to pdf and bibtex in the correct order, I call tb2p f . This works very well if I am in a bash terminal. However, to get to the bash prompt from Vim, I need to run the command : sh first .

To simplify the above procedure, I tried to execute the t2p and tb2p functions inside Vim with :! t2p f . Then Wim tells me that he cannot find the t2p function. I did some Googling and read that I have to put these functions in the /etc/bash.bashrc file to make them visible to Vim. Unfortunately, in my case, this did not work. Vim still does not know about the function.

At the end of the day, I would like to be able to call my functions from Vim using a keyboard shortcut. So my questions are:

  • How can I tell Vim about ~ / .bashrc functions?
  • How to configure a keyboard shortcut in ~ / .vimrc for a function in ~ / .bashrc ?

Thank you very much.

+7
source share
4 answers

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

+4
source

Try adding set shell=bash\ --login to your .vimrc.

To convert the current file, type :!tb2p % , % will be expanded by Vim for you when you run the script.

After it works, you can add a mapping to make the whole process even faster:

 nnoremap <F12> :!tb2p %<CR> 
+1
source

Vim runs bash commands with the -c argument, which makes the shell non-interactive and invalid and bypasses reading boot files.

You can override this with an environment variable.

From bash man:

  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 may be prompted to run shells as input systems. 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|. 
0
source

You can always define your functions in a separate file and put this file in a folder in the PATH environment variable. In my case, my personal functions that I will use go to ~ / bin

In your case for t2p:

create a t2p file 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 executable:

 > chmod +x t2p 

make sure ~ / bin is in your path by putting the following in your ~ / .bashrc:

 export PATH=${HOME}/bin:${PATH} 
0
source

All Articles