Monday, May 16, 2011

Map F1 to Esc in Vim

If you are like me, you press F1 key instead of Esc quite often.  It can be especially annoying when using Vim.  Add the following line to your .vimrc:

imap <F1> <Esc>

Now pressing F1 is treated as if you pressed Esc.  When you really need help you can use :help command instead.

Saturday, May 14, 2011

pushd, popd, and dirs commands

If you spend a lot of time in the command line, you will have to cd to different directories frequently.  Instead of the cd you can use pushd command to switch to a different directory.  pushd command pushes the new directory into a "directory stack".  Later you can return to your original location using popd command.  Both pushd and popd print the latest contents of the directory stack.  You can also use dirs command see the directory stack.  Items in the directory stack are indexed from 0.  You can reference any directory in the stack by typing ~ followed by the index.  A sample session using the directory stack (user input is in bold font):
~% pushd /tmp
/tmp ~
/tmp% pushd /etc
/etc /tmp ~
/etc% pushd /var
/var /etc /tmp ~
/var% dirs
/var /etc /tmp ~
/var% popd
/etc /tmp ~
/etc% pwd
/etc% dirs
/etc /tmp ~
/etc% cd ~1
pingala /tmp % dirs
/tmp /etc ~
As the directory stack becomes larger, finding the index of a directory in the stack becomes harder.  I use -v flag to print the numeric index along with each item:
/tmp% dirs -v
0       /tmp
1       /etc
2       ~
If you are like me, you wouldn't remember to use pushd.  For users like us, the zsh has an auto_pushd option.  When it's set, all cd commands are treated like pushd commands.  With that option enabled, I use the shell normally, but zsh remembers the directories I have recently been to.  I can use dirs -v any time to get a list of recent directories and use the ~ notation to return to one those directories.

Sunday, May 8, 2011

Controlling permissions of files on a drive

If you have mounted FAT or NTFS partitions on your Linux machine, you'd have noticed this: all files and directories on those partitions have their permissions set to 777, meaning anyone can do anything.  I get annoyed when files that are not commands have execute permissions.  In addition to being a security risk, I find them aesthetically displeasing.

There's an easy solution, of course.  We can make all files from that partition non-executable by setting an appropriate fmask value in its fstab entry.  Mine looks like this:
/dev/sda3 /mnt/win ntfs-3g defaults,fmask=111 0 0
I have set the fmask to 111, which is execute permission for owner, group, and everyone. Since masks specify what operations are not permitted, no file in that drive would be executable.

There's also a dmask that restricts directory permissions.

Saturday, May 7, 2011

/tmp is your friend

Do you end up with a lot of unnecessary files in your home directory, e.g. in your ~/Downloads folder?  Make it a habit to use /tmp for files that you don't want to keep for a long time.  By default, Ubuntu empties  /tmp every time it boots up so you don't keep accumulating junk.  (If you'd rather keep your temporary files for a longer period, you can change TMPTIME variable in your rcS file.)