Skip to content

Instantly share code, notes, and snippets.

@uarun
Created August 17, 2012 03:11
Show Gist options
  • Save uarun/3375600 to your computer and use it in GitHub Desktop.
Save uarun/3375600 to your computer and use it in GitHub Desktop.
Zsh Tips & Tricks

Zsh Tips and Tricks

Argument History

!*        # ... All parameters of the last command
!$        # ... Last parameter of the last command
!^        # ... First parameter of the last command
!:1       # ... First parameter of the last command

!-2:2     # ... Second parameter from 2 commands ago
!:2-3     # ... Previous command's parameters 2 to 3
!:2*      # ... Previous command's parameters 2 onwards
!:2-      # ... Previous command's parameters 2 onwards omitting last

!$:h      # ... Last parameter, strip one level
!$:h:h    # ... Last parameter, strip two levels

!?ls      # ... Last 'ls' command

Directory Substitution

cd ~/projects/sample/src/main/java/com/arun/examples/
cd java scala

The last command will put you in ~/projects/sample/src/main/scala/com/arun/examples/ (Now that's Hot !)

Previous Command Substitution

r apple=orange
!!:s/apple/orange/     # ... Subst previous command replacing first 'apple' with 'orange'
!!:gs/apple/orange/    # ... Subst previous command replacing all occurrences of 'apple' with 'orange'
@Chinggis6
Copy link

Chinggis6 commented Aug 23, 2017

In bash and zsh you can opt to use ^apple^orange for the same purpose described at the bottom as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment