> ~ biozz / Blog

NeoVim Telescope Keymap Mnemonic 2023-12-07

My keymap mnemonic for efficient Telescope usage in NeoVim

Reading time: 2m (397 words)

I developed a kind of mnemonic to use Telescope efficiently. It is very simple and has just one principle: a is for all.

Let’s look at the example of “Live Grep” search.

I have a Django project, which is quite big (more than 500k LOC). Sometimes it is hard to find things, even though we follow a set of rules for objects and function names. In order to reduce the amount things, which show up in the output, I use .rgignore config to exclude some of the files. I found useful to exclude two things: **/migrations/* and urls.py. That’s because we have hundreds of migrations and search results are usually polluted with them. We also have docstrings for each endpoint which contain the full URL of the view, that’s why I don’t want urls.py files in the search results.

Here is my keymap for Live Grep search:

vim.keymap.set("n", "<C-f>", function() telescope_builtins.live_grep({}) end, { noremap = true, desc = "Live grep" })

If I want to search migrations, urls and other ignored files I need to pass --no-ignore flag, because I use ripgrep for Live Grep. So to do that I add this keymap:

vim.keymap.set("n", "<C-a>f", function() telescope_builtins.live_grep({additional_args = {"--no-ignore"}}) end, { noremap = true, desc = "Live grep (no ignore)" })

The same goes to “Find Files”. Here are my keymaps:

 vim.keymap.set("n", "<C-p>", function() telescope_builtins.find_files({}) end, { noremap = true, desc = "Find files" })
 vim.keymap.set("n", "<C-a>p", function() telescope_builtins.find_files({ no_ignore = true, hidden = true }) end, { noremap = true, desc = "Find all files" })

You see how “all” keymaps are prefixed with <C-a>? Because a is for all.

Now there is another part of the story - git branch picker. I can’t use C-b, because it is my prefix for tmux, so I use <leader>b. I usually want to see my local branches without the remote ones, so here the keymap:

vim.keymap.set("n", "<leader>b", function() telescope_builtins.git_branches({show_remote_tracking_branches = false}) end, { noremap = true, desc = "Git branches" })

The intuitive keymap for “all git branches” is therefore b prefixed with <leader>a:

vim.keymap.set("n", "<leader>ab", function() telescope_builtins.git_branches({}) end, { noremap = true, desc = "Git branches (all)" })

And this would work for any other keymap which requires local and all scopes.

Keymaps are usually very personal and this setup might not work for you, but it is always nice to see things from the different prospective.

More posts in NeoVim Config series:

There is nothing in here at the moment