Table of Contents

How to Enable Autocomplete For Command History In Bash and Zsh

How to enable Autocomplete using fzf

fzf is a great utility to enable autocomplete for command line history. Let us first install it using git.

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf

Lets install now.

~/.fzf/install 
Downloading bin/fzf ...
Already exists
Checking fzf executable ... 0.19.0 
Do you want to enable fuzzy auto-completion? ([y]/n) y 
Do you want to enable key bindings? ([y]/n) y
Generate /root/.fzf.bash ... OK
Do you want to update your shell configuration files? ([y]/n) y
Update /root/.bashrc:
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
Added
Finished. Restart your shell or reload config file. source ~/.bashrc 

You will be prompted with above options, say yes to all of the above. After that we need to source ~/.bashrc file.

source ~/.bashrc

Now lets try Ctrl+R command. When you press Ctrl+R, you will be prompted with list of commands from your command line history as shown below.

To select your desired command use either arrow key to move up and down or use Ctrl+k to move up and Ctrl+j to move down. Once you are at your desired command press Enter and command will appear on your command prompt. Press Enter again to execute the command.

To get out of fzf mode just press Ctrl+c

By default fzf shows a list of last 10 commands used. Although we can increase this limit but better way is to use its search functionality. fzf has fuzzy logic search capability. Lets say I want to search for commands which has notebook in it. By simply typing book or even if type for example: "bok", fzf will still show me the list of commands which has book in it. Checkout the below snapshot to see an example.

We can do lot more than with fzf. fzf --help will show you the list of all the things that we can do with fzf. Lets explore couple of options which i find very useful for my day to day use.

Lets say we want to open text file through vim but dont know the exact name. We can use fzf to scan through the list of files.

Example: we want to open a .bashrc file name but for some reason we only remember that file name has bash in its name. We can do following command.

vim bash**<tab>

After running the command fzf will prompt us with list of files which contain bash name in it as shown in the snapshot below

Another very useful feature of fzf is to scan through list of process ids in the kill command. To kill we can do kill -9 <tab>, fzf will prompt you the list of available running processes. Then we can search through the list using its fuzzy logic. Please checkout the snapshot below.

That's it for now.Hope you would find this post useful.



Related Posts

1
2
3
4