Drakevr's Corner

Me in Hashtags: #sysadmin, #programming, #networking, #student, #Linux

30-and-1 Zsh tricks you may not know...

Jan 7, 2012 - 3 minute read - Comments - ZshShelltricks

Or if you do.. Well done! you really know your way around obscure and vast manpages. :-)

Among the millions of zsh features i will be presenting you with some pretty useful ones for daily use.

  1. esc-.
    insert the last argument of the previous command in history, repeat to cycle through commands/arguments in history.
  2. esc-'
    quote the whole line. (for use with su -c, ssh et al).
  3. esc-q
    clear the current line and insert on next blank prompt (useful for inserting interim commands).
  4. !"
    got a command with many ! but too tired to escape all of them in order to not call upon history? just prefix it with a !” ( “ left intentionaly unclosed).
  5. =zsh
    expand to the full path in $PATH ( eq to which zsh ).
  6. !$
    use last command’s argument.
  7. !$:h
    use last command’s argument, strip a level, add more :h to traverse the levels.
  8. vi =(foo)
    process substitution, use vi to edit the output of foo.
  9. <file
    is like “more < file”.
  10. >file
    is like “cat > file”.
  11. >>file
    is like … you guessed it, “cat >> file”.
  12. !:t another
    history exp trick, this one strips the path in the previous command giving you the basename only.
  13. !-2\$:r
    talking about history lets get a little more complex. second to last command, first parameter without file extension.
  14. for i (*.c) { ... }
    pretty much self-explanatory, shorter way to write a for loop.
  15. vared
    edit variables from the comfort of your zsh prompt.
  16. hash -d name=path
    another awesome feature is the ability to have named directories. you create one like that and you can cd to it from anywhere using cd ~name.
  17. echo $foo:e
    get the extension of $foo (the opposite of :r).
  18. repeat n
    run a command n times.
  19. alias -s tex=vim
    now running somefile.tex in zsh will open somefile.tex with vim.
  20. tetris
    all work and no play makes jack a dull boy! thus zsh has a tetris mode to help you relax at times. just load the module and assign it to a key or something!
  21. zftp
    when dealing with a lot of ftp stuff, this module can save you hours by automating the process through shell scripts.
  22. ^r
    search history by greping the history file, ( can be “/” if using vi input mode).
  23. ^a
    go to the beginning of line.
  24. ^e
    same for the end of it.
  25. zmv
    the Z shell Programmable rename/move .. a BEAST that with enough loving can trully perform miracles.
  26. zle
    the Z shell line editor .. another module that needs a book of its own.
  27. ls /u/s/d/m/e<tab>
    the shorter version for ls /usr/share/docs/mutt/examples
  28. you can setup directory specific configs for use with different projects, ref: mika’s conf on mika’s blog.
  29. ls -l *(.L-100)
    list all files in the current dir that are less than 100bytes in size (ignore directories).
  30. ls -l *(Lm+50)
    list all files and dirs in the current directory that are more than 50 Megabytes in size.

Bonus:
{% codeblock lang:sh %} kapow(){ touch ~/.pow/$1/tmp/restart.txt; if [ $? -eq 0 ]; then; echo “pow: restarting $1” ; fi } compctl -W ~/.pow -/ kapow {% endcodeblock %}

this gem from Christofer Sexton will let you quickly restart an app in pow.

Tags:

The Haiku Tutorial is Here! Of tabs and spaces.

comments powered by Disqus