Skip to content

Instantly share code, notes, and snippets.

@imikh
Forked from uarun/zsh.md
Created December 29, 2017 01:22
Show Gist options
  • Save imikh/32434dc298d2ae275bfc120ea03a9528 to your computer and use it in GitHub Desktop.
Save imikh/32434dc298d2ae275bfc120ea03a9528 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'

Edit variables on demand: vared PATH 1 ↵ /bin:/home/sergio/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

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

!! space before command to exclude it from history

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