Call bash function using vim external command

后端 未结 4 1967
既然无缘
既然无缘 2021-01-13 22:00

I use vim\'s :! external command function all the time, usually providing % as an argument to the shell command. For example :

:!psql -f %

相关标签:
4条回答
  • 2021-01-13 22:27

    The accepted answer didn't work for me. I'm going to go with setting shcf, as suggested elsewhere:

    :set shcf=-ic
    

    but another solutions is

    !source ~/.bashrc && psql ...
    

    Unfortunately, no solution allows the auto-completion for the command I'm creating to work properly. (The auto_completions suggested are for names of files in my current directory, rather than the ones I specified as follows in .bashrc

    complete -F _generate_foo_completions foo
    
    0 讨论(0)
  • 2021-01-13 22:39

    This answer assumes your vim isn't actually using bash to invoke the remote commands - this can be tested by running :!echo $0 in vim.

    Specifically for vim, add:

    set shell=/bin/bash
    

    to your .vimrc.


    In general, there's two strategies I've found to sometimes work when trying to get other programs to invoke my preferred shell:

    export SHELL=/bin/bash
    

    in eg. the .profile file, or:

    ln -fsn /bin/bash /bin/sh
    

    which updates the sh symlink to point to bash instead.

    On many systems (certainly Ubuntu), /bin/sh is a symlink to a sh-compatible shell (such as bash), rather than the sh shell itself. Given this fact, some programs (I've seen this behaviour in GHC) seem to invoke /bin/sh, so changing where it points to will cause the programs to use bash instead.

    0 讨论(0)
  • 2021-01-13 22:41

    An alternative to exporting your functions (which may no reach Vim is there's a non-Bash shell in between; see here for such a case), you can instruct Vim to start an interactive shell, so that your .bashrc is read. Just pass the -i flag to Bash, via Vim's :help 'shellcmdflag'.

    :set shcf=-ic
    
    0 讨论(0)
  • 2021-01-13 22:46

    Export your functions. That is:

    psql-h1() { /usr/bin/psql -hh1 -d mydb "$@"; }
    
    export -f psql-h1  ### <-- THIS RIGHT HERE
    

    This will make them available to any copy of bash run as a child process, even if it's a noninteractive shell and so doesn't read .bashrc.

    0 讨论(0)
提交回复
热议问题