The mycommand is a function in my lib/work.sh file.

cat .bashrc
source /home/lib/work.sh

It means that mycommand was sourced in automatically.
mycommand can execute in terminal.
Now to edit a file with vim test.txt,shift+" to enter into vim's command mode ,input !mycommand ,

/bin/bash: mycommand: command not found

shell returned 127

Press ENTER or type command to continue

Why mycommand can't found in vim?


There are different types of shells - Difference between Login Shell and Non-Login Shell? (look for the .bashrc part)

Not all of them are sourcing .bashrc thus work.sh isn't sourced.

To solve your problem you need to export your function to make it available to a subshell.

Can I "export" functions in bash?

myfun() {
    echo "Hello!"

export -f myfun




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.