Search all files in Linux and open it in Vim directly

FZF combine with VIM

I've recently discovered a neat trick that combines FZF and Silver Searcher (ag) to make my development workflow smoother. If you're like me and you want to respect your .gitignore while using FZF, and you also want to open files in Vim straight from FZF, here's a simple guide:

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!

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously