Vim/Neovim — Managing Multiple Projects

Managing multiple projects from Vim/Neovim.

Photo by Sigmund on Unsplash

Overview

Normally I work on multiple projects on a daily basis. Most projects are under a common folder and I will be switching among projects from time to time. For Emacs users, projectile makes it very easy for project management and navigation. Using projectile, I can switch projects and fuzzy find files easily. For Vim/Neovim, we also have few options available.

I will be using the dotfiles I developed in my previous article for the walk through.

Tab and Session

The default Vim way is to use tabs for multiple projects. You can create 1 tab for each project.

And to save the session, you can use the built-in session commands (:h session) and then restore the session each time you restart Vim.

You can also use plugins, e.g. vim-startify or vim-obsession to make this process easier.

vim-startify

telescope-project.nvim

telescope-project.nvim is an extension for telescope.nvim that allows you to switch projects.

Under lua/plugins.lua, add the following line to install it.

-- Project
use { 'nvim-telescope/telescope-project.nvim' }

Run :luafile % and :PackerInstall to install it.

In plugin/telescope.vim, add the following line for the key binding.

nnoremap <silent> <leader>fp :Telescope project<CR>

Press <leader>fp now to bring out the Telescope project window.

telescope-project window

From here it gets a bit interesting. Press <ESC> to go into Normal mode and you can use the following keys now.

  • d: delete currently selected project
  • c: create a project (defaults to your git root if used inside a git project, otherwise will use your current working directory)
  • s: search inside files within your project
  • w: change to the selected project’s directory without opening it
  • f: find a file within your project (this works the same as <CR>)

So whenever you are in a Git project, press c and the folder will get added to the list.

You can then switch between multiple projects/folders, perform fuzzy searching and open any files easily.

To help searching for projects to add for the first time, I also define the following custom telescope function in lua/config/telescope.lua

M.switch_projects = function() 
require("telescope.builtin").find_files ({
prompt_title = "< Switch Project >",
cwd = "$HOME/workspace/development/",
})
end

And the key mapping in plugin/telescope.vim

nnoremap <silent> <leader>fx :lua require('config.telescope').switch_projects()<CR>

If the project is not in telescope-project list, I will first search for the file by pressing <Leader>fx. Once I open the file, I can then add the project folder to telescope-project.

For this to work, you may want to set autochdir or use vim-rooter. I use vim-rooter in my setup.

This is definitely a useful plugins to manage multiple projects.

vim-cltrspace

Vim-CtrlSpace is a plug-in for managing

  • your tabs, buffers, files,
  • workspaces (sessions),
  • bookmarks for your favorite projects

Under lua/plugins.lua, add the following line to install it.

use { 'vim-ctrlspace/vim-ctrlspace' }

Run :luafile % and :PackerInstall to install it.

Restart Vim and now when you press Control-Space you should see a window appear at the bottom of your screen.

vim-ctrlspace window

Press ? and you can see all the key bindings.

vim-ctrlspace key bindings

Press B and you will be asked for the project directory to bookmark.

Bookmark a Project Folder
vim-ctrlspace fuzzy search

Now with the bookmarks, you can easily switch projects and perform fuzzy search (use the O command).

There are many more features this plugin provides which you can explore on your own.

fzf.vim

If you are using fzf.vim, you can add the following key mapping. Do change the folder accordingly.

nnoremap <silent> <C-Space> :call fzf#run(fzf#wrap({'source': 'find $HOME/workspace/development -maxdepth 2 -type d'}))<CR>

Here I am mapping Control-Space to use fzf to open a list of folders under my project folder.

fzf — Search Folder

Once I select the folder I can then use fzf again to open a particular file in the the folder.

You may want to combine this with vim-rooter to change the working directory to the project root when you open a file or directory.

Summary

These are the methods that I find helpful to manage multiple projects. With this, I can easily switch and work on multiple projects without leaving Vim.

Do also check out the following articles.

Programmer and occasional blogger.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store