How to Access Functions from .bash_profile in Vim
The aim of this page📝 is to explain how to use functions defined in .bash_profile
within Vim.
1 min readFeb 1, 2024
Explanation
- Functions defined in
.bash_profile
are not directly accessible in Vim by default. - To make the functions available in Vim, they need to be exported from the shell environment.
- Open
.bash_profile
usingvim ~/.bash_profile
. - Define your functions in the file, e.g.,
myFunction() { echo "Hello from myFunction!"; }
. - Add the following code at the end of the file to export all functions:
# Export all functions
for func in $(declare -F -p | awk '{print $3}'); do
export -f "$func"
done
- This code uses
declare -F -p
to list all function names defined in the current shell session. It then loops over each function name and exports it usingexport -f
. - Save and close the file.
- Source the file using
source ~/.bash_profile
to apply the changes to the current shell session. - Now, the functions defined in
.bash_profile
are accessible in Vim. - In Vim, execute the function using
:!myFunction
. - The function will be executed, and the output will be displayed in the Vim terminal.
LINKS
- on
.bash_profile
:https://linuxize.com/post/bashrc-vs-bash-profile/