Sunday, May 28, 2017

Posted by beni in , , , | May 28, 2017

6 Bash Productivity Tips


1. Use !!
Typing !! followed by Enter will bring back and execute the last command (same as Ctrl+P followed by Enter).

2. Use Emacs-like shortcuts
Emacs-like shortcuts make use of the Ctrl and Alt keys, and the big advantage of navigating and manipulating text in a Bash prompt is that you wont have to move your fingers away from the typing position (to access the arrow keys, or the Home, Page Up and Page Down keys). Notice that most of those work well inside manual pages too, or when using the Nano editor, or the less and more pagers.

Ctrl+D - or EOF (end-of-file) will quit the current Bash session
Ctrl+P - bring back the last command executed
Ctrl+N - bring the next command executed
Ctrl+U - delete all from the cursor to the left
Ctrl+K - delete all from the cursor to the right
Ctrl+B - go back one character
Ctrl+F - go forward one character
Alt+B - go back one word
Alt+F - go forward one word

3. Make manual pages coloured
I think this tip is a productivity tip because once a manual page is shown coloured, it is easier, clearer to read. Just add the following inside the ~/.bashrc file, where ~ is your home directory (notice that each line starts with export):

export LESS_TERMCAP_mb=$E[01;31m # begin blinking
export LESS_TERMCAP_md=$E[01;38;5;74m # begin bold
export LESS_TERMCAP_me=$E[0m # end mode
export LESS_TERMCAP_se=$E[0m # end standout-mode
export LESS_TERMCAP_so=$E[38;5;246m # begin standout-mode - info box export LESS_TERMCAP_ue=$E[0m # end underline
export LESS_TERMCAP_us=$E[04;38;5;146m # begin underline

Then, type:

source ~/.bashrc

And try any manual page (e.g. man bash). Heres how a coloured man page looks like using the presets above:

Coloured man page

4. Use your own scripts
If you have basic knowledge of Bash scripting, you can create scripts and put them in a directory included in your $PATH. You can use something like ~/bin or ~/usr/bin, where ~ is your home directory.

5. Copy/paste using Shift+Insert
Although this one is not Bash-specific, its very useful when you need to quickly copy/paste some text in your terminal. The same can be accomplished using the middle-click mouse button.

6. Use aliases
One of the powerful features of Bash is that it makes use of aliases. Those are usually short commands which perform some associated, longer command or commands. For example, to update and upgrade your Ubuntu installation you would type sudo apt-get update && sudo apt-get upgrade, and to create an alias for it you would only have to add something like the following inside the ~/.bashrc file:

alias update=sudo apt-get update && sudo apt-get upgrade

Now, each time you type update, the command inside the single quotes will be executed.

Search