Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What are some of your favorite shell tips? Stuff that makes your every day command line use that much easier?

Mine, for bash:

Add this to the .inputrc file in your home dir:

"\e[A": history-search-backward
"\e[B": history-search-forward

Now, when you start typing at the prompt, the arrow keys will search through your history for any other commands that start with what you've already typed.

share

locked by Michael Hampton Apr 12 at 1:34

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

    
    
This is kind of broad and duplicate of other questions. How about changing it to something like, "interactive shell tips, like readline and .inputrc." –  Ian Kelling May 5 '09 at 21:31
    
This should be community wiki –  Toro May 6 '09 at 14:53
comments disabled on deleted / locked posts

5 Answers

up vote 1 down vote accepted

Not a tip, instead more of a meta-tip. There are lots of cool cli reciipies at http://www.commandlinefu.com/

share
add comment

If you are a vi user, try vi mode for bash:

set -o vi

Press ESC to enter command mode. From there, you can navigate the command line using familiar vi commands (hjkl, w, ^, $, i, A, c, d, etc). You can search your command history using /. You can even press v to fire up vi and edit your current command.

See this cheat sheet for a list of supported vi commands.

share
    
most importantly, press v in normal mode (<Esc>v) to start a vim instance on your current command line –  sehe Sep 22 '11 at 7:17
add comment

Here's the ones I use the most in day to day stuff in bash

Keyword shortcuts I use the most - tab of course for tab completion (setup bash completion to make this even better) - up and down for navigating the history - ctrl-a and ctrl-e for start and end of the line - ctrl-r for searching through your history (just start typing the start of the command) - ctrl-g to cancel the search - alt-f move cursor forward a word - alt-b move the cursor back a word - esc then . to add the last argument of the last command to the current command (doing it again goes to the last arg of the command before that) - alt-d delete the word in front of the current position of the cursor - ctrl-w delete the word behind the current position of the cursor - ctrl-u to delete everything from the current position of the cursor to the start of the line - ctrl-k to delete everything from the current position to the end of the line

On top of this, make sure you have a colour ls setup, I add the git branch to my prompt

I also always have the reload function in my .bash_aliases file (which is obviously sourced from my .bashrc or .bash_profile file)

reload()
{
    if [ -f ~/.bashrc ]; then
    	source ~/.bashrc
    else
    	source ~/.bash_profile;
    fi
}
share
add comment

I deal with a lot of different machines so one of my favorites is aliases for each machine that I need to frequently SSH to:

alias claudius="ssh dinomite@claudius"

It is also useful to setup a good .ssh/config and ssh keys to make hopping amongst machines even easier.

Another one of my favorite aliases is for moving up directories:

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."

And some for commonly used variations of ls (and typos):

alias ll="ls -l"
alias lo="ls -o"alias lh="ls -lh"
alias la="ls -la"
alias sl="ls"
alias l="ls"
alias s="ls"

History can be very useful, but by default on most distributions your history is blown away by each shell exiting, and it doesn't hold much to begin with. I like to have 10,000 lines of history:

export HISTFILESIZE=20000
export HISTSIZE=10000
shopt -s histappend
# Combine multiline commands into one in historty
shopt -s cmdhist
# Ignore duplicates, ls without options and builtin commands
HISTCONTROL=ignoredups
export HISTIGNORE="&:ls:[bf]g:exit"

That way, if I know that I've done something before but can't remember the specifics, a quick history | grep foo will help jog my memory.

I often found myself piping output through awk in order to get a certain column of the output, as in df -h | awk '{print $2}' to find the size of each of my disks. To make this easier, I created a function fawk in my .bashrc:

function fawk {
    first="awk '{print "
    last="}'"
    cmd="${first}\$${1}${last}"
    eval $cmd
}

I can now run df -h|fawk 2 which saves a good bit of typing.

share
    
For the SSH alias, I suggest you add a -t parameter (which enables the TTY), so you can run the odd console app, like Screen, Midnight Commander, apt-get, or whatever. For example: claudius screen -dr would connect you to a long-running screen session. –  jhs May 6 '09 at 5:08
add comment

Probably one of the simplest, and most useful things is just learning the readlin shorcuts. So ^h for backspace, ^u to delete a line up to the cursor, ^k to delete the line after the cursor. There's a complete list of the shortcuts here [here][1]http://www.bigsmoke.us/readline/shortcuts.

As well as that, on debian/ubuntu (at least, don't have any other linuxes to test it on, adding this to your .bashrc will enable smart completion in bash, like in zsh, so that arguments to common commands (like find, etc) will be tab completable

if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
share
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.