Skip to main content

Tips and Tricks - 1

So, I decided to start a new post type, namely Tips and Tricks.

These will constitute random — as in not pertaining to a particular topic — tips. But, are short enough, that they do not warrant full individual posts. Mainly the things which I have recently discovered, or had long forgotten :P. But also, the things which I regularly use and think can be useful for others.

So, let's start with the first post of this kind.

*Drum Roll*

1. Bash

1.1 Overriding aliases temporarily

Most shell users are probably familiar of aliases. Interacting with a shell becomes extremely convenient using aliases.

Now, sometimes we name our aliases after the actual commands themselves. For example, I have an alias rm='rm -iv'.

But, what if I wanted to run the original command?

One way to do that is by specifying the full path to the binary. But, there is an easier way to do the same. Simply prepend a slash before the alias, and the unaliased command will run instead. Example:

$ \rm

1.2 Accessing $HOME of a different user easily

Probably everyone knows that tilde ~ stands for the home directory of the current user. And, you can perform various actions such as $ cd ~ or $ ls ~ using that.

Now, here comes the cool part: By appending a valid username to the tilde, you can perform the operations on that user's HOME. Example:

(Assuming you are logged in as root and noob is a valid user)
# ls ~noob

1.3 Alternate between two directories

If you need to alternate between two directories frequently, you can use:

$ cd -

Here - maps to the $OLDPWD variable.


1.4 Prevent accidental exit from the shell

If you are a Vim user, then you have probably typed Ctrl-D in bash accidently, and quit the shell at least once. I used to do this a lot. Thankfully, you can put:

set -o ignoreeof

in your .bashrc to disable this behaviour. Now, you'll have to type exit in the shell, to exit it.


1.5 Prevent exiting from the shell if background jobs are running

To prevent bash from exiting if you have a backgrounded job running, put:

shopt -s checkjobs

in your .bashrc.

Now, when you type exit, the shell will list the status of any backgrounded jobs instead. If you really want to quit, you can type exit again, and the shell will quit irrespective of the status of any backgrounded jobs.

2. Show bus and speed of usb devices

The lsusb command can be used to show the usb ports and devices of a computer.

Using:

$ lsusb -t

you get the usb device hierarchy in a tree format.

With the output of the above command, you can figure out which device is connected to which bus. Another benefit is that you can find out the speed of the bus, as well as the speed at which the device is operating.

3. Get a grand total of occupied and free space for all mounted partitions

Normally, df is used to get a list of all the mounted partitions and their individual disk space statistics.

You can also get a grand total of the disk space statistics of the mounted partitions using:

$ df -h --total
4. Vim: Highlight a particular column

To highlight a particular column in Vim, use:

:set colorcolumn=<column number>

This is very useful if you want to find out the length of lines. For example, if you want to find out the lines which have a textwidth greater than 79, you can do:

:set colorcolumn=79