Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.)

share|improve this question
See: – 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
up vote 1 down vote accepted

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.

share|improve this answer

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";;
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .