Navigation and search in Vim/Neovim

Alex
4 min readJul 24, 2024

--

Good navigation and search across the project are essential features for any modern code editor or IDE. In this article, I will consider a few Vim plugins that will help you implement these features in your configuration. We will also glance at buffers.

Firstly, upgrade your Vim/Neovim version to the latest and install the plugin manager if you don’t have it. I will use vim-plug, but you can choose any manager you like.

Let’s start with the simple but essential plugin for almost everybody who uses Vim. This plugin is called vim-easymotion. It allows navigating through the visible area of the editor using a combination of keys. To start it, we should run Leader + s + any letter you can see in the visible area and want to put a cursor on it.

The suggested letters weren’t random. Plugin selects to be convenient to use with touch typing techniques. By default, the Leader key is \ , but you can change it in the configuration.

Easy motion plugin has many features. You can visit the documentation to read about them and see a visual example of how the plugin works. For the demonstration, we will use the basic configuration. It will be more than enough for a good start.

We should add a few lines to the configuration file to install the plugin. I use Neovim, and in my case, this file is called init.vim. If you use Vim, add it to the .vimrc.

call plug#begin('~/.local/share/nvim/plugged')
Plug 'easymotion/vim-easymotion'
call plug#end()

Save the file and reload the configuration using source command source ~/.config/nvim/init.vim . After this, we should run :PlugInstall to download the plugin, and after you see it finished, close the window using :qcommand.

The last thing we should add to the configuration is a combination of keys to invoke the plugin. We need to add only one line of code for this, and then we can use it.

map <Leader> <Plug>(easymotion-prefix)

The following plugin I want to look into is Nerdtree. It provides convenient and powerful navigation between file trees. Also, it has much more features like creating, removing, moving files, etc. We can add this plugin like the previous one. Just put this line in plugin manager, reload file and run :PlugInstall .

Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }

Then, we need to add configuration. I will not dive deep into this plugin, and I will add basic configuration. If you want to extend it, you can visit documentation.

"NERDTree
let NERDTreeMinimalUI = 1 " Remove Bookmarks and Press ? for help sections
let NERDTreeAutoDeleteBuffer = 1 " Remove buffer after removing file

" We will open nerdtree automatically If open vim in folder
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | exe 'cd '.argv()[0] | endif

map <C-n> :NERDTreeToggle<CR> " Ctrl + n - Show/hide nerdtree menu
nmap <Leader>r :NERDTreeFocus<cr>R<c-w><c-p> " \\ + r - I use it for updating tree, for example after adding files using terminal, omitting nerdtree
nnoremap <silent> <Leader>v :NERDTreeFind<CR> " \\ + v - Find and reveal the file in the NERDTree window

After you save the configuration, you can open an existing project and test Nerdtree features. You can press ? or open documentation to get information about plugin.

One more thing I like about editors is Bookmarks. I will not describe them here because they are a big topic, and it’s better to write a separate article about them, but I highly recommend looking into this in the documentation.

Let’s install a plugin that will connect Git and Nerdtree. nerdtree-git-plugin is a very simple plugin that will show modified files in the Nerdtree navigation. Just install a plugin like the previous ones without any configuration.

Plug 'Xuyuanp/nerdtree-git-plugin'

If you also like icons, I recommend adding vim-devicons to the configuration. Also, to make it work, you need to install Nerd Fonts and then enable it for your terminal.

Plug 'ryanoasis/vim-devicons'

Frequently, we want to open the latest file that we worked on. To achieve this, we can add vim-startify, and it will run without any configuration when you use Vim or Neovim.

Plug 'mhinz/vim-startify'

The last plugin I want to show is fzf.vim. It enables global search in your project. It’s powerful and has many features that can simplify search within the project. Installation is simple:

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'

Then, we should install ag, which we will use for the text search. Installation is straightforward, and you can find guide in the documentation.

Let’s add the configuration for our search.

"Fzf search
silent! nmap <C-P> :GFiles<CR> " Ctrl + p - Search commited files within the project
nnoremap <Leader>b :Buffers<CR> " \\ + b - Navigation through existing buffers
nnoremap <Leader>h :History<CR> " \\ + h - Hiles history
nmap <Leader>F :Ag<Space> " \\ + F - Global files search

Also, it would be nice to simplify the buffers workflow. If you haven’t worked with buffers before, you can think of them as opened tabs. Currently, you can switch between them using \\ + b, but usually, it’s not enough. Let’s extend it by adding configurations.

" buffers
map gn :bn<cr> "g + n - Next tab
map gp :bp<cr> "g + p - Previous tab
map gd :bd<cr> "g + d - Close tab
nnoremap <leader>co :w <bar> %bd <bar> e# <bar> bd# <CR> "\\ + с + o - Close all other tabs

For now, it’s more than enough to be a good start. It’s an essential plugins that you can extend or install new ones to meet your needs.

--

--

Alex
Alex

No responses yet