A few zsh tricks

Here are a few zsh tricks that I learned (and enjoy using) recently:

The basics - use !! to retrieve last command

1
2
tail /var/log/message
!!

The not so basic - get last parameter of last command with !$

1
2
mkdir new_folder
cd !$ # after this command you will be in folder new_folder

Get all parameters from the last command with !*

1
2
diff src/file1 dest/
cp !*

The pretty awsome - substitution with !!:s/from/to or !!:gs/from/to

1
2
vi src/en/file.txt
!!:s/en/fr
diff src/en/file.txt dest/en/file.txt
!!:gs/en/fr        # g for global substitution

Even better with ^

1
2
3
4
5
vi src/en/file.txt
^en^fr

diff src/en/file.txt dest/en/file.txt
^en^fr^:G

Switching between directories with similar structure (added Dec 7, 2013)

let’s say you are in ~/dev/myproject1/src/lib/, and you want to cd to ~/dev/myproject2/src/lib/, all you need to do is

1
cd myproject1 myproject2

or even

1
cd 1 2

Batch renaming with zmv

1
2
autoload zmv
zmv '(*).txt' '$1.dat' # change *.txt to *.dat

see more zmv examples at http://zshwiki.org/home/builtin/functions/zmv