Some More Bash Knowledge

Today I learned some more about Bash from here.

  • First some key strokes
    • Alt + BackSpace will wipe off a word at a time (unlike Ctrl + W, which would wipe off a positional parameter at a time)
    • Ctrl + U will wipe off the entire command line.
    • Ctrl + T will exchange the last two typed characters (I guess this is for fast mistyping)
  • We all know that bash history files get overwritten by the last closing bash window. In a sense we lose all the commands typed in the previous incarnations of bash. To solve this issue two things need to be done:
    • Keep a line shopt -s histappend in the .bashrc file.
    • export a variable PROMPT_COMMAND=’history -a’. This will cause the bash to execute the history -a command upon return of the prompt.
  • Incremental searching of commands (not Ctrl + R,which is a reverse match anywhere kind of search), is possible by first typing in the command’s first few letters and then pressing Up or Down keys. For this, the .inputrc file needs to be populated properly
    • “\e[A”: history-search-backward
    • “\e[B”: history-search-forward

    When we do this, the Up and Down keys are over-rided. Their original functionality is still available through Ctrl + P and Ctrl + N

  • One can repeat the command bits (here the argument given to the last command) through Alt + .. This will auto-fill the current position with the last command’s last argument.
  • Some shortcuts for the arguments and commands:
    • !* is a shortcut for all the arguments of the last command
    • !:0 is the last command name
    • !^ and !$ are the first and last arguments, while the nth argument can be accessed through !:n
    • (Gets more clumsy now..) !-n is the nth command that got executed and this itself can be used as an unit and combined with the above shortcuts to access their arguments.
  • Insert-option macro – to automatically position the cursor after the first word of the previous command – so as to add an extra option. Keep the following line in the .inputrc file:
    • “\M-o”: “\C-p\C-a\M-f “ (ctrl-p for last command, ctrl-a for the start of the line..)
    • As it is visible, we can change the macros, if we want
  • One common use with functions could be to imitate the existing graphical commands while keeping a & at the end so that all the graphical commands will automatically start in the background.
  • ~- (tilde+dash) is a shortcut for the last working directory.
  • Bash can cope with one missing, one extra characters or a pair of characters transposed, by having the following option in .bashrc:
    • shopt -s cdspell
  • One can change to the frequently working directories, by keeping their parent directories in CDPATH environment variable. It is recommended to keep “.” first so that the cd command would work properly for the current directory. For example, it can be cd videos.
  • A couple of options in .inputrc can help autocomplete work even more seamlessly:
    • set match-hidden-files off (do not autocomplete hidden filenames)
    • set completion-ignore-case on (go ahead and complete names that have been typed in the wrong case)
  • By having IGNOREEOF=1 exported from .bashrc, Ctrl + D has to be pressed twice to exit the shell – just for convenience.
  • Another useful feature is programmable completion, where the file names are completed automatically depending on the type of the command. Need to look more into this in free time.

This is a great deal of new information about Bash that can harness the productivity of the Bash [power] user.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s