A few zsh tricks

Computers, Linux, Ubuntu Add comments

I’ve started playing around with the zsh shell, and I like it a lot more than bash. A few weeks ago I participated in the “history meme” where I used this command in zsh:

cat ~/.histfile|awk '{a[$1]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head

to print out my top 10 used commands.

A slightly shorter command in zsh is:

history 1|awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head

which works for anyone, even if they have their history file in a different location. The command “history 1″ prints all commands in the history from the 1st one.

Everyone, whether they are new to Linux or a guru forgets to put “sudo” in front of system commands once in a while. I often forget when I want to edit a file in /etc or need to use apt-get. Typing out the command again can be long-winded, as can pressing up to get the command again, and then reverse tabbing to the beginning of the line to type “sudo “. zsh gives an easier way:

sudo !!

Adding “!!” after anything will simply put whatever you type before the !! at the beginning of the last used command, and then execute it. It’s much faster. Additionally, just executing “!!” on the command line will execute the last command, without any prefix. You can use !! to prefix any command you want.

Finally, if you use a lot of terminals at once (like myself) it will be useful to share the history across them. Usually only the most recently opened terminal saves history. Adding these to your .zshrc file will enable all instances of zsh to store history, and update their history every time a command is executed.

setopt EXTENDED_HISTORY
setopt SHARE_HISTORY
setopt APPEND_HISTORY
setopt INC_APPEND_HISTORY

Popularity: 3% [?]

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • NewsVine
  • Slashdot
  • Netscape
  • Technorati
  • blogmarks
  • Spurl
  • YahooMyWeb
  • BlinkList
  • Reddit
  • TailRank
  • feedmelinks
  • Furl

One Response to “A few zsh tricks”

  1. penguinator Says:

    Excellent tips… i had heard about the sudo one, but forgot what the command was… thanks!

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login