This article shows some hints how to improve your bash shell working experience to reach higher productivity. Just simple shortcuts that are not so well known.
Using the History
The bash history is underestimated when it comes to usability. Here some nice stuff to do with the history.
Search the history
Every command is kept in the history. The simplest way to use the history is using the cursor-up/down keys. Most users are aware or [ctrl]-r. Usually you hit [ctrl]-r (r like reverse search) several times and miss the command, roll your eyes, hit [ctrl]-c and do it again. Why not using forward search with [ctrl]-s in such a case? Well, that suspends your terminal. It comes from the ancient times and is not needed anymore.
Turn off terminal suspension
echo "stty -ixon" >> /etc/profile
Now you can search the history back and forward by using [ctrl]-r and [ctrl]-s.
Using another command with the same last argument
When you i.e. do ls a file and decide to edit it, you don’t need to retype the whole file path or using the mouse to copy-paste it. Use the [Alt]-. (dot) combination. It inserts the last argument used. So after ls -la /tmp/file.txt you type vi [Alt].. Review and hit enter to execute.
You can also reuse other than the last arguments, but this is more complex and does not speed up things a lot, copy-paste with your mouse is usually faster in such a case.
Forgot to sudo?
When you want to cat i.e. /etc/sssd/sssd.conf you need root access. As a normal user, access is denied.
[luc@fedora ~]$ cat /etc/sssd/sssd.conf cat: /etc/sssd/sssd.conf: Permission denied [luc@fedora ~]$ sudo !! sudo cat /etc/sssd/sssd.conf [domain/example.com]
The !! also called bash bang does the trick. It just repeats the same command as used before which all arguments. Be aware that the command is executed immediately.
Bash can copy-paste as well!
Copy-paste is not only available in graphical environments but in the bash shell as well.
If you need to type some different commands all with the same arguments, cut the stuff. Position the curser to the position on the line from where you want to copy and hit [ctrl]-k. When you want to paste, hit [ctrl]-y.
You may also achieve that using othercommand !*. Using !(bash bang) can be dangerous because the command will be executed immediately, the copy-paste method is more safe.
That also works with single words etc. basically everything where you cut or delete some stuff like [alt]-d, [ctrl]-w, [ctrl]-u
Using an editor for copy-paste from websites and word processors
There are a number of reasons why you don’t want to directly copy-paste to a shell. Sometimes the source content has not properly escaped line ends or its just garbage from word processors. You may want to review and edit appropriately before fire the command. There is a super lazy and convenient trick to do so.
The security usecase
Copy-Paste from a Website is a security nightmare. Copy-Paste the following two lines into an editor and you see what I mean.
echo “Dont copy-paste”
The HTML code used for that is:
Sample command<span style="font-size: 0; position: absolute; left: -100px; top: -100px"><br>:echo "Dont copy-paste"</span> Second sample
Nice! Use an editor before pasting anything in a terminal, for the sake of security.
The word processor garbage usecase
Lot of documentation is written in word processors such as Libreoffice, MS-Office and others. They replace double hyphens to a single one and nasty stuff such as single quotes to backticks. Just for a thing called usability.
When copy-paste that stuff, you probably want to review and edit it first.
Set the EDITOR environment variable
If you are too lazy to fire up vim, you can set the EDITOR environment variable to an editor of your choice (vim, emacs, nano, whatever), system wide in /etc/profile or /etc/bashrc. A better idea is to put it in ~/.profile or ~/.bashrc.
echo "export EDITOR=vim" >>: ~./bashrc
Afterwards you can just hit [ctrl]-x-e and vim starts up. When save and exit vim, the command will be executed.
What are my Keybindings?
If you wonder what kind of shortcuts are defined in a shell, a lot are. use bind -p to show them.
Have fun 🙂