I'd like to configure Bash to run a particular command before running each command line I enter at the prompt. Specifically, I'd like to tell Vim (which is running in another terminal) to write all open buffers, because in my workflow if anything's unsaved when I leave Vim it's a mistake.

Is there an option for this in Bash? If not, is there an option in Zsh?

(There is a readline-based solution that somewhat fits this problem on another question, but it feels a bit hacky. It'll take it as a last resort.)

  • 1
    See: superuser.com/q/175799/46005 – cYrus Mar 23 '12 at 18:21
  • Are you sure you want to run a command before executing the next one, and not after finishing the previous one? – Daniel Beck Mar 23 '12 at 18:36
  • Yes, I want to switch from my Vim tmux pane to my Bash pane, run a command, and ensure that all files in Vim have (attempted to) save before the command runs. I'm emulating the common GUI editor behavior of saving when losing focus. – Peeja Mar 23 '12 at 18:42

You might use 'precmd' in zsh for that.

Probably you might want to configure backups in Vim though, because doing such kind of stuff in precmd doesn't sound right to me.


I have done similar things using a pseudo-sub-shell using the builtin read as follows:

#public domain fake shell template
while read -p "$PS1" CMD; do
   case "$CMD" in
      *bad_case*|*another_bad_case*)echo bad command: user info logged etc..;;
         eval "$CMD";;

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.