Share this post
FaceBook  Twitter  

1. Introduction

Your shell - bash in this particular case - offers several ways to make your typing life easier and more efficient. It's a known fact that some people are averse to typing hours at a time, so those shell features we are about to talk about help you type as less as possible given the situation. This article will present some nifty tricks that will make your typist life a bit easier and more efficient.

2. Bash tricks

We will start with some easy to use tools and advance onwards to more evolved ones. The first and probably more widely-known feature is the tab completion. In bash, it works for executables, as long as they're in the $PATH, as well as directories/files. What does this mean? If you type a command partially and you're too lazy to type it all, press the Tab key on your keyboard and the shell will show you any possible continuation to what you started. If there is only one possible continuation, the shell will complete it. Usually you don't really need to use completion on traditional Unix-imported commands (mv,ls,cp,...) because, as you can see, they're short but easy to remember : mv stands for move, ls stands for list and so on. However, there are more modern commands that in time become tedious to type, again and again. You can either use an alias to shorten it, or simply use tab-completion. When it comes to directories, the situation is the same, except that modern versions of bash don't autocomplete paths you don't have access to.

If you find yourself typing the same command often, you can use the shell's history feature so you don't have to retype it over and over again. Up-arrow and down-arrow allow easy history browsing (^P and ^N doing the same thing -- previous and next) and you can even modify some command you typed beforehand. If you repeat a lot of commands, and don't want to see lots of consecutive instances of the same command in your history, type export HISTCONTROL=ignoreboth in your shell. Consult your shell's manual page for more information and/or for how to make this change permanent, user-specific or system-wide.

By default, bash uses Emacs-style keybindings for searching in the history or maneuvering on the command-line. So if you are already familiar with the editor, the better. If not, you'll learn an introductory course that will be useful when you start learning Emacs. The reverse search is accomplished by using ^R (reverse), so forward search is ^S (search). If you press ^R for the first time and can't find what you want, just press it again until you get what you came for. Regarding maneuvering, ^A gets you to the beginning of a line, ^E takes you to the end of it, and Alt+B and Alt+F (back and forward) move back and forth one word at a time, just like Ctrl+left/right arrow do. ^K deletes text from the cursor to the end of the line, Alt+Backspace deletes backwards one word at a time and ^X plus Backspace deletes all text from the cursor to the beginning of the line. Transposition (interchanging the positions of two characters) is made with ^T, and if you need to change the case of the character under the cursor, press Esc + U. Finally, if you want to delete your history, in cases such as sensitive information on a shared machine, type history -c. If you want a full editor to edit a command, ^X and ^E (eXecute Editor) will launch $EDITOR, and if none defined, Emacs.

3. Exercises

1.Create a directory somewhere in your $HOME. Create, using your editor, or touch(1), a few files that start with the same letter or group of letters. For example, efficient and effedrine, docile and documentation, and so on. The type ls eff<Tab>, then ls e<Tab>. Play around to see how completion works and get used to it.

2.Type history and look at the commands you just typed. Find out where is your history saved and how to access a certain position in it without an editor.

3.Think about a situation, other than the one we spoke of already, when you'd need to erase your history.

4.What should be edited so you can change the keybindings of the shell to vi-style?

5.Practice with maneuvering through the command line. How many keybindings do Bash and Emacs have in common?


Who are we? is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.


You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.