As some of you may already know, I am a die-hard fan of productive editing. That is probably because I don’t find myself very fast, on a keyboard. So, I am always trying to find ways to improve my editing speed. And when I ain’t surfing on the web, I am either typing stuff in my shell or my editor. So today, I would like to share a few tricks I uses in my default shell, Z Shell.
The shell history can be a powerful tool. If you find yourself typing
commands again, and again, and again, you probably can use it at your
advantage. You probably already know about
Ctrl+R, which is bound
history-incremental-search-backward command in most shells.
Personally, I don’t find it very useful since it tries to find a match
everywhere, but it’s better than cycling through the history with the
Up/Down keys. In fact, anything is better than the Up/Down keys. So,
why not rebind them to something more useful, like
history-search-backward? Well, that is easy. With Zsh, you need to
add these two line to your
bindkey '\e[A' history-search-backward bindkey '\e[B' history-search-forward
In fact, if you’re using Emacs key-bindings, you don’t even need to do
Meta+N are already bound to these
two functions. Incidentally, Steven Harms is advocating to enable
this feature by default in Ubuntu,
for Bash’s users. Personally, I am not sure if it’s really necessary
to make it a default. I am not a fan of modifications in
either. But, I will leave that discussion for another blog post.
Now, that we have functional Up/Down arrow keys, can we do more? Yes,
we can! Let me introduce one of my favorite features of Zsh,
preemptive auto-completion. If you’re tired typing
TAB a zillion
times a day, you will love this one. This feature implements
predictive typing using history search and auto-completion. Again, to
enable it, just copy these lines to your configuration file:
autoload predict-on zle -N predict-on zle -N predict-off bindkey '^Z' predict-on bindkey '^X^Z' predict-off zstyle ':predict' verbose true
Here, note that
predict-off, are bounded to
Ctrl+X Z respectively. That means you can turn it
on/off, whenever you need to. You will find useful to turn it off when
you edit the middle of a command, since it can confuse the prediction.
But other than that, it’s great.
Sometime, the shell editor is not enough for me — I need something
more powerful when I edit long commands. So, I use another cool
built in function of Zsh, called
edit-command-line. With this feature, I can
edit the current command with an external editor, defined by the
$EDITOR. To enable it, just copy-and-paste
autoload edit-command-line zle -N edit-command-line bindkey '^Xe' edit-command-line
So, when I think the command will be long, like a for-loop. I just
Ctrl+X e, which launches, on my system,
am always running Emacs with its server,
therefore the shell command is instantaneously loaded into a Emacs
buffer. Then when I am done, I close the Emacs session with
and the command appears in my shell. It is just sweet.
Even if you’re a master with your editor, nothing beats a short alias, or a shell script. I keep a full directory of useful scripts, to automate my daily tasks. At first, writing scripts feels a bit awkward. If you’re like me, you will always worry that your scripts might go terribly wrong, and eat your data. That’s totally normal, but don’t be a fool. Automating your tasks, even the most trivial ones, will save some of your precious time. Unlike scripts, which can really do some heavy automation, aliases are just a shell convenience, like auto-completion. Personally, I am not a big fan of fancy aliases. (I tend to use functions for the more fancy things.) Anyway, here some of my favourite aliases:
# Set up aliases
alias c=clearalias d='dirs -v' alias e=$EDITOR alias grep=egrep alias h=history alias j=jobs alias po=popd alias pu=pushd alias ss='screen -Rx' # Global aliases -- These do not have to be # at the beginning of the command line. alias -g M='|more' alias -g L='|less' alias -g H='|head' alias -g T='|tail' # Go to parent directories without `cd' setopt autocd alias -g ...='../..' alias -g ....='../../..' alias -g .....='../../../..'
I certainly have a ton of shell tricks, but I will keep them for my other blog posts. So, that’s all folks!