Chuck
Chuck

Reputation: 4902

Can I prevent a terminal command from being run twice in a row?

I have a script that does a lot, setting up a new development server, pushing source to it, putting sample data on it, synchronizing the name of the server with my current git branch.

Then, once it's set up, I have an alias that pushes new source code to it.

I run the first command a few times a day. I run the alias all the time. And my muscle memory has me making a change to the source, going to the terminal, pressing the up arrow, and pressing return. 90% of the time, this is exactly what I want.

But, after setting up the server, and then making a change to the code, that muscle memory is betraying me, and I end up starting to spin up a new development server. Yes, I just press Ctrl-C to stop it, but it's still annoying.

Is there any way to prevent the server setup command from being run twice in a row?

Upvotes: 0

Views: 109

Answers (1)

tg0h
tg0h

Reputation: 665

If you are feeling adventurous, you can rebind your enter key to use this widget

  • If the current buffer is empty, it repeats the last command.
    • However, if the last command is "ls" (replace with your command), it does not repeat it.
  • If the current buffer is not empty, then it just runs the standard accept line widget (.accept-line) to execute whatever you have typed in your prompt.
accept-line() { 
  if [ -z "$BUFFER" ]; then
    local prevCommand=$(fc -ln -1)
    if [ $prevCommand = "ls" ]; then
      # do nothing
    else
      zle up-history
    fi
  fi
  zle ".accept-line"; 
}
zle -N accept-line

In zsh how do I bind a keyboard shortcut to run the last command?

Upvotes: 1

Related Questions