Linux Commandline Tips and Tricks

A small guide to a more productive command line.

Use Zsh

Zsh has some really fancy features. I am not sure to which degree bash has caught up. But if it can’t do the following things you should seriously consider switching:

Powerfull Tab-Completion

Assuming there is a file called Somefile and a folder called _posts in the current directory.

Ignores Case:

ls somefi<Tab> completes to Somefile

Ignores minor typos:

ls Someif<Tab> completes to Somefile
cd post<Tab> completes to _posts

Save tabs - instead of using:

cd w<Tab>/co<Tab>/l<Tab>

it is possible to use:

cd w/co/l<Tab> completes to workspace/code/lovely

More cd magic:

Assuming you are in the directory /tmp/foo/something and you want to switch to /tmp/bar/something. All you need to type is:

cd foo bar

Zsh also has quite a lot of predefined cmd-line-tools argument completions:

zsh_git_checkout zsh_git_checkout

And the awesome scp-remote completion:

zsh-scp-remote-completion

Oh, and let’s not forget about global aliases:

alias -g N='2> /dev/null'
find / -name "foo" N

Is basically equivalent to

find / -name "foo" 2> /dev/null

Don’t waste time tuning your zsh config - take defaults:

There are two wonderful projects that ship with a lot of nice defaults:

oh-my-zsh and zprezto

Use one of them.

Use Fasd

Navigating around directories becomes even easier by utilizing a tool like autojump or fasd

Fasd ranks which folders and files are accessed by “frecency” and provides a way to quickly access them.

Some examples from the fasd github page

v def conf       =>     vim /some/awkward/path/to/type/default.conf
j abc            =>     cd /hell/of/a/awkward/path/to/get/to/abcdef
m movie          =>     mplayer /whatever/whatever/whatever/awesome_movie.mp4
o eng paper      =>     xdg-open /you/dont/remember/where/english_paper.pdf
vim `f rc lo`    =>     vim /etc/rc.local
vim `f rc conf`  =>     vim /etc/rc.conf

And shown in action:

fasd

Use Ranger

Ranger is a curses based file manager with “vi-like” keybindings.

It shows the contents of the parent folder, the current folder and the contents of the selected file or folder. It’s especially useful to explore stuff in cases you don’t exactly know what you’re looking for.

Of course you can also do the usual stuff expected from a file manager. Like copying, pasting or deleting. And calling regular shell commands.

ranger

By default ranger will exit to the directory you were in the moment you started ranger. But that behavior can be changed:

# after ranger quits, cd to the chosen directory.
ranger() {
    tempfile='/tmp/chosendir'
    /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
    test -f "$tempfile" &&
    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
        cd -- "$(cat "$tempfile")"
    fi
    rm -f -- "$tempfile"
}

So far so good.

If the world doesn’t end tomorrow and some people consider this useful I might be willing to write a second part.

Thursday, December 20, 2012 » Linux Improvement Productivity Tools