Configure Nvim with Lua: Fuzzy finder, Telescope (7)
4 min read, Published on Oct 4, 2022An important functionality is the search for words in a file. To search for a string:
/wordToFind
According to the neovim documentation:
You will notice that when you type the ”/” the cursor jumps to the last line of the Vim window, like with colon commands. That is where you type the word ‘wordToFind’. To find the next occurrence of the same string use the “n” command. The ”?” command works like ”/” but searches backwards:
?wordToFind
The “N” command repeats the last search the opposite direction. Thus using “N” after a ”/” command searches backwards, using “N” after ”?” searches forwards.
Suppose you see the word “TheLongFunctionName” in the text and you want to find the next occurrence of it. You could type “/TheLongFunctionName”, but that’s a lot of typing. And when you make a mistake Vim won’t find it. There is an easier way: Position the cursor on the word and use the ”*” command. Vim will grab the word under the cursor and use it as the search string. The ”#” command does the same in the other direction. You can prepend a count: “3*” searches for the third occurrence of the word under the cursor.
There is a plugin that provides us with other facilities such as fuzzy search, file search, etc. telescope.nvim is a highly extendable fuzzy finder over lists.
Today, we going to test telescope.nvim, because it is written in Lua.
Use telescope.nvim
- The first step: we add the plugin telescope.nvim to the plugins.lua file.
- We move to the nvim folder:
cd .config/nvim
nvim lua/configs/plugins.lua
- We add this and save it:
-- ...others configs...
return packer.startup(function(use)
-- ...others plugins...
use { 'nvim-telescope/telescope.nvim', tag = '0.1.0'}
-- ...others configs...
end)
- To use telescope.nvim, it requires the ripgrep, a line-oriented search tool that recursively searches the current directory for a regex pattern, if you want use live_grep. I recommend that you use it.
To install this package, you should read installation ripgrep documentacion to execute the command according to the operating system, examples:
If you’re a macOS Homebrew or a Linuxbrew user, then you can install ripgrep from homebrew-core:
$ brew install ripgrep
- If you’re an Arch Linux user, then you can install ripgrep from the official repos:
$ pacman -S ripgrep
- If you run Debian Buster (currently Debian stable) or Debian sid:
$ sudo apt-get install ripgrep
- If you’re an Ubuntu Cosmic (18.10) (or newer) user, ripgrep is available using the same packaging as Debian:
$ sudo apt-get install ripgrep
- If you’re a Windows Chocolatey user, then you can install ripgrep from the official repo:
$ choco install ripgrep
- If you’re a Windows Scoop user, then you can install ripgrep from the official bucket:
$ scoop install ripgrep
- You should install fd. fd is a program to find entries in your filesystem. It is a simple, fast and user-friendly alternative to find.
- If you’re a macOS Homebrew or a Linuxbrew user, then you can install ripgrep from homebrew-core:
$ brew install fd
- If you’re an Arch Linux user, then you can install ripgrep from the official repos:
$ pacman -S fd
- If you run Debian or other Debian-based Linux distributions:
$ sudo apt install fd-find
- If you’re a Windows Chocolatey user, then you can install ripgrep from the official repo:
$ choco install fd
- If you’re a Windows Scoop user, then you can install ripgrep from the official bucket:
$ scoop install fd
- Install the packages:
:PackerInstall
- Later, we create the telescope file.
nvim lua/configs/telescope.lua
- We add this code and save it:
local ok, telescope = pcall(require, 'telescope')
if not ok then
return
end
telescope.setup()
- To use these settings, we need to call the telescope file in the main file (init.lua)
nvim init.lua
- Add this:
require('configs.telescope')
- Open the keymaps.lua file (where we are going to put the custom keymaps).
nvim lua/configs/keymaps.lua
- Add this:
-- ...others keymaps...
-- Telescope keymaps
keymap("n", "ff", "<cmd>Telescope find_files<cr>", opts) -- find files
keymap("n", "fg", "<cmd>Telescope live_grep<cr>", opts) -- find by words
keymap("n", "fb", "<cmd>Telescope buffers<cr>", opts) -- find opened buffers (tabs)
-- ...others keymaps...
And that’s it!! You can use Telescope!
Directory
We have the directory of folders like this:
nvim
├─ init.lua
└─ lua
└─ configs
└─ options
| └─ init.lua
└─ keymaps.lua
└─ plugins.lua
└─ impatient.lua
└─ colorscheme.lua
└─ nvim-tree.lua
└─ lualine.lua
└─ bufferline.lua
└─ telescope.lua
Conclusion
In this article we learned how to configure telescope.nvim, a highly extendable fuzzy finder over lists neovim plugin. Now you can use ff to search for files, fg to search for words and fg to search for opened tabs. I hope it will be helpful for those who want to experiment.