An Easy Way to Generate Aliases in Bash

I was looking at this quick bash tip and found that there is a better way to create aliases, than what is mentioned in the article:

function z(){
   eval alias $varName=”cd\ $cwd”

so, its usage will then become, say, “z webd” which will take the current dir and assign an alias webd to cd to it. Instead of remembering g1, g2 etc., we can remember the aliases with meaningful names. What do you think?


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.