Rico's Blog

Back

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

The basics - use !! to retrieve last command#

tail /var/log/message
!!
plaintext

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

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

Get all parameters from the last command with !*#

diff src/file1 dest/
cp !*
plaintext

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

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

Even better with ^#

	vi src/en/file.txt
	^en^fr

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

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

cd myproject1 myproject2
plaintext

or even

cd 1 2
plaintext

Batch renaming with zmv#

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

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