July 10, 2008

5 Tips to Make Working with the Shell Easier

1. Use Emacs-like shortcuts
^N calls next command, ^P calls previous command
^U deletes all the text entered to the left (use it when you wrote a wrong command and want to delete it fast instead of using Backspace or ^H)
^K deletes all the text entered to the right (from where the cursor is all to the end of line)
^F moves cursor forward one character, ^B moves it backwards
^A jumps to the beginning of the line
^D logs you out (closing the terminal application too if no other session was started inside it - the same with exit)
There are plenty more, here is a list.

Note: ^ is the Ctrl character, hence ^U means press Ctrl+U at the same time.

2. Copy/paste text using middle mouse button or SHIFT+INSERT
To paste some text in the terminal just use the middle mouse button (press the scroll wheel). If you don't have such a button, press left and right buttons at the same time. SHIFT+INSERT does exactly the same thing, just be careful what you copy/paste.

3. Easily count lines, words and characters in a text file with wc
wc -l file.txt will count lines
wc -w file.txt will count words
wc -m file.txt will count characters
You can also feed its input, which comes in handy when you have an essay for example and you want to know how many words it has in it. Type

wc -m

Then paste your text and press ^D when you're finished (End of File).

4. Make aliases to only type brief commands
Aliases make your job easier by creating a custom, short command which will do the work of several commands. Example:

alias dup='sudo apt-get update && sudo apt-get upgrade'

A full tutorial on how to create aliases is here.

5. Create scripts and put them in a directory in your $PATH
You can create handy scripts for whatever tasks you need (for example I use those for audio encoding/decoding, mass-renaming of files and several more). Just make sure to make your script executable and put it in a directory in your $PATH. On Debian for example, directory ~/bin/ is detected and added automatically to the $PATH. Otherwise, you can add it by editing ~/.bash_profile and adding:


Updated: Jul 10, 2008 (Created: Jul 10, 2008)


Ihar Filipau said...

To number 5 I can only add suggestion to use ~/usr/bin instead of plain ~/bin.

If you use shell often, chances are you might end up compiling programs by yourself too. To not to pollute system, I do that in my $HOME under ~/usr.

Another great tip is reflect negative return status of last command in your shell prompt (I hope blogger will not eat some chars):

PS1='[\u@\h:\w]\[`test "$?" -ne "0" && echo -ne "\e[31m"`\]$\[`echo -ne "\e[0m"`\] '

You can test it with "true" and "false" commands. "false" would turn the '$' red, meaning that last command had failed. VERY convenient. Picked up on /. long time ago.

Unknown said...

That shell prompt trick is very cool. I'm now using it in .bashrc ... Thanks!

Anonymous said...

Another way to set the prompt:

- prompt starts at the beggining of the line, so I can enter long commands.
- for user the prompt will be green.
- for root the prompt will be red.

For user prompt, in file /home/user/.bashrc:

#PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS1='\[$(tput setaf 2)\]\u@\h:\w\n$ \[$(tput sgr0)\]'

For root prompt, in file /root/.bashrc:

#PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS1='\[$(tput setaf 1)\]\u@\h:\w\n$ \[$(tput sgr0)\]'

Resulting prompt:


Other prompt colors:

tput setaf:

1 = red (dark)
2 = green
3 = orange (dark)
4 = blue (dark)
5 = magenta (dark)
6 = blue (turqois)
7 = gray
8 = red (bright)
9 = white

Mackenzie said...

That first one is specifically Bourne Again Shell (/bin/bash). The Debian Almquist Shell (/bin/dash) doesn't understand most of those. ^U still works though.