Search all files in Linux and open it in Vim directly
FZF combine with VIM |
First things first, you'll need to install FZF and Silver Searcher. You can do this easily with the following command:
sudo apt-get install fzf silversearcher-ag
Once you have these tools installed, the next step is to tweak your .bashrc
file to set up the right environment variables. This ensures that FZF respects your .gitignore
file by default. Open your .bashrc
file with your favorite text editor:
nano ~/.bashrc
Add the following line to the file:
# For respecting gitignore
export FZF_DEFAULT_COMMAND='ag -g ""'
This command tells FZF to use Silver Searcher (ag) to list files, respecting the .gitignore
.
Next, let's set up a function in your .bashrc
to open selected files in Vim directly from FZF. Add this function to the same file:
# For opening vim on item selected
fzfv() {
local file
file=$(eval "$FZF_DEFAULT_COMMAND" | fzf) && vim "$file"
}
This function runs the FZF_DEFAULT_COMMAND
, which lists all files (while respecting .gitignore
), and pipes the results to FZF. When you select a file from the FZF interface, it opens in Vim.
After adding these lines, don't forget to reload your .bashrc
to apply the changes:
source ~/.bashrc
Now, whenever you want to search for files and open them in Vim, you can simply type:
fzfv
This setup has made my life a lot easier, and I hope it does the same for you. Happy coding!