0

I have created a bash function, in its own .sh file, which I want to use from within Vim, and later use if from within the lightline config file. I have tried many ways, but I cannot make it work.

Specifically, I have tried with the !source command, but it doesn't work. Since I'm using a macOS, I have added the sourcing of the function script in the proper place in the /etc/bashrc file, so the sourcing will happen in a non-interactive shell.

Still nothing.

My command is called myTest and it is:

#!/usr/bin/env bash

function myTest{
    echo "OK"
}

and is at the test_function.sh file.

Since I have read the relevant question of Why doesn't vim recognize bash self-made functions, I have copied the test_function.sh to /etc/ and I have at my /etc/bashrc

if [ -z "$PS1" ]; then
    source /etc/test_function.sh
    return
fi

This is why in MacOS, the non-interactive shells read the /etc/bashrc or /etc/profile files.

Also I have added the sourcing of the test_function.sh file in the /etc/profile file, having:

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
else
    source /etc/test_function.sh
fi

When I'm trying to do in Vim :!myTest I'm always getting Command not found.

Any help?

4
  • I am not sure I understand the questions. I believe the shell executed by vim is not an interactive shell, so it won't read bashrc files. You can change this by adding /ic to shellcmdflag option Commented Apr 4, 2022 at 11:48
  • In MacOS, the non-interactive shells run the /etc/basrhc or /etc/profile/. That is why I'm mentioning this.
    – Xxxo
    Commented Apr 4, 2022 at 12:17
  • 1
    Why not create a script instead and place it somewhere in your PATH? Then calling it from any shell and from tools like Vim should just work
    – D. Ben Knoble
    Commented Apr 4, 2022 at 14:45
  • Are you sure? That's against the documented behaviour for at least bash. In any case, I would use :set verbose=5 then run a custom command like :!true and observe the shell command that is being issued. From there you can further debug on how to source your functions (or add parameters to the 'shellcmdflag' and other related options Commented Apr 5, 2022 at 6:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.