5

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.

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Chris
  • 963
  • 1
  • 6
  • 8
  • Related: http://serverfault.com/questions/3743/what-useful-things-can-one-add-to-ones-bashrc and http://serverfault.com/questions/4995/changes-to-etc-bashrc – Adam Gibbins May 05 '09 at 21:30
  • 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 05 '09 at 21:31
  • This should be community wiki – Toro May 06 '09 at 14:53

5 Answers5

7

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.

  • most importantly, press `v` in normal mode (`v`) to start a vim instance on your current command line – sehe Sep 22 '11 at 07:17
1

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
Cian
  • 5,777
  • 1
  • 27
  • 40
1

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

Zoredache
  • 128,755
  • 40
  • 271
  • 413
1

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
}
Rodney Amato
  • 196
  • 1
  • 3
1

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.

Drew Stephens
  • 662
  • 7
  • 12
  • 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 06 '09 at 05:08