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.

Pavol Kutaj
1 min readFeb 1, 2024

--

hAIku prompt: Rembrandt formatted / the search for tools in workshop / by group of stone masons

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 using vim ~/.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 using export -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

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com