Jul 8 2010

UNIX (and bash) command line tricks

Category: zvolkov @ 08:06

Just a random selection of keyboard shortcuts and other productivity tricks for bash.

Keyboard shortcuts: (use bind -p to see all current mappings)

  • Ctrl+a -- begining of line
  • Ctrl+e -- end of line
  • Ctrl+u -- cut head
  • Ctrl+k -- cut tail
  • Ctrl+y -- paste
  • Ctrl+_ -- undo (may require pressing Shift, depending on your keyboard mappings)
  • Ctrl+L -- clear screen
  • Alt+b -- skip to next word boundary (if Alt doesn't work on all terminals, see the following post or try Esc followed by the char instead)
  • Alt+f -- skip to prev word boundary
  • Alt+d -- delete next word
  • Alt+backspace -- delete prev word
  • Ctrl+w -- cut word
  • Ctrl+R -- search command history
  • Alt+. -- repeat last command's argument

Add following to .inputrc:

  • "\e[3~": delete-char -- enable normal Delete function (as opposed to both Backspace and Delete working as Backspace)
  • "\C-?": delete-char -- same as above but works on virtual terminal (not just on SSH)
  • "\e[1~": beginning-of-line -- enable Home button
  • "\e[4~": end-of-line -- enable End button
  • "\e[1;5D": backward-word -- enable Ctrl+Left (only works on SSH terms)
  • "\e[1;5C": forward-word -- enable Ctrl+Right (only works on SSH terms)

History tricks:

  • alias h = 'history 25' -- add to .bashrc (call .bashrc from .bash_profile using the ".")
  • PS1 = "\!:\W\$" -- add to .bash_profile to set prompt to [History#]:[CurrentDir]$
  • !$ -- substitute last argument of last command
  • !123 repeat history number 123
  • export HISTIGNORE=$'[ \t]*:&:[fb]g:exit:ls' -- add to .bashrc, this way exact dups, ls, fb, fg, exit, and lines starting with space won't be added to history
  • PROMPT_COMMAND='history -a' -- add this and next line to .bashrc to make bash save history immediately, this way history will work right with two simultaneous terminals
  • shopt -s histappend

Tab-completion:

  • set completion-ignore-case on -- add to .inputrc to enable case-insensitive completion
  • set show-all-if-ambiguous on -- display all completion options on single Tab rather than on double-Tab
  • case $- in -- install bash-completion and add this to .bashrc to get Tab-completion for command arguments (the path can be different)
       *i*) [[ -f /etc/bash_completion ]] && . /etc/bash_completion ;;
    esac

grep:

  • grep -options 'word' filename -- general syntax
  • -A4 -B4 -- show +- 4 lines of context
  • -v -- NOT match
  • -i -- case insensitive
  • -w -- separate word
  • -n -- show line numbers
  • -r -- recursive

Other useful stuff:

  • alias cd='pushd > /dev/null' -- add to .bashrc to map cd to pushd, this way the next alias bd can be used to go back
  • alias bd='popd'
  • find . -type f -exec grep "word" /dev/null {} \; --find word recursively in all files
  • sed 's/old/new/g' filename -- find and replace

Tags:

Comments

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading