This is a short blog post for people that use command line a lot and may be interested how to use it a little more effectively.
From my experience, after a few years of using a CLI (command line interface) most commands I enter are very close variations of the commands I have enter in the past. For example when I need to connect via ssh using a gateway or want to check some statistics from a GIT repository, I probably have done something similar in the past. From what I see, many people use CLI history just for commands that they entered a moment ago - they only use
up arrow key to access the most recent commands. For me, having a convenient way of searching for any command that I have used over past years is very useful. I don’t have to remember the exact syntax of complicated ssh connection or how to do a partial dump from a database. I just have to roughly remember that I did something similar before and then just search the history for the particular command.
Most UNIX-like systems nowadays have CLI history enabled by default. It’s just a text file. For ZSH it usually is stored as
~/.zsh_history and for Bash
~./bash_history . By default you can also search the history using
ctrl + r in the terminal. The problem is that default search is not really useful, as you don’t have fuzzy searching, it shows only one command etc. The game-changer here is to use fzf. It is a command-line fuzzy finder which is so fast that you can use it to find a file in your home directory. So basically the whole idea is to replace the default search provided by the shell with the fzf.
To make it work you just need to install fzf and its shell extensions. After that you will have super fast fuzzy-search for your history.
For macOS it is as easy as:
brew install fzf /usr/local/opt/fzf/install
A good idea is also to check in you shell configuration the size limit of the history file. With fzf you don’t have to worry even if you have there ten of thousands commands. My configuration for history settings can be found here.
I remember that to install capybara-webkit gem, I needed to add some additional stuff last time I needed it. I need to install it right now so I can either google how to do it, check my notes (if I have one) or just search the history.
I use it not only for complicated commands, but I also rarely use aliases for commands, as typing e.g.
;gp [ctrl + r] (especially if
ctrl is mapped to
capslock) is fast enough.
There are two more things that can come in handy if you need something from the history. The first thing is that in the history file a timestamp is stored along with every command. So you can search for commands that you used for example on January 6th 2016.
$ hist .zsh_history 2016,01,06 2016,01,07 : 1452091521:0;cd tmp : 1452091523:0;wget https://raw.githubusercontent.com/junegunn/vim-journal/master/journal.txt : 1452091525:0;nvim journal.txt : 1452091541:0;nvim : 1452091550:0;cd .. : 1452091555:0;nvim .nvimrc : 1452091562:0;nvim tmp/journal.txt : 1452091580:0;gnvim tmp/journal.txt : 1452091743:0;mkdir notes : 1452091752:0;cp tmp/journal.txt notes : 1452091754:0;cd notes : 1452091761:0;nvim journal.txt
You can find the
hist (just a simple ruby script) command here.
Or you are at the stand-up meeting and you you need a glance at what commits you made on a particular day?
hist .zsh_history 2016,01,21 2016,01,22 | grep commit : 1453414043:0;git commit -am "Add PURE_SET_TITLE option" : 1453414152:0;git commit -am "Better copy"
It can be helpful to use
grep if you need a context of the command:
grep capybara-webkit .zsh_history -C3
To show three commands before and after the command.
If you use history a lot and want it to be clean and tidy, a nice thing to remember is that you can use [space] in front of the command to prevent it from saving in the history.
I hope that using history in your Shell will increase your productivity. If you use the Shell history in other ways, I’m looking forward to see it in the comments.
For example I think it’d interesting to have stored the directory of which the command was used.
Thank you for reading!