GNU+Linux command memo

Zsh : Edit and repeat the last command

Répéter une version éditée de la dernière commande entrée, à l’aide de la syntax sed / vim de remplacement de chaîne de caractères.

You can repeat your last command with !! in Zsh and you can substitute an element of this laste command via the sed / vim-like syntax :s/to-remove/replace-by-this.

# cfdisk /dev/sda
# !!:s/sda/md127 (1)
# cfdisk /dev/md127
1 Re-run the cfdisk command modifying the matching part of the path

And as reported by Copenhas on Coderwall in 2017 :

  • you can apply several substitutions :

    • :s/aaa/bbb/:s/bbb/ccc will chain the calls and end up with 'ccc'

    • :gs/aaa/bbb will replace all occurences of 'aaa'

  • you can apply substitutions on shell variable content

    • echo ${PATH:gs/bin/sbin/}

  • you can use a simpler notation for substitution on 1st match only

    • ^aaa^bbb will achieve the same than : :s/aaa/bbb