What is the best way to switch between projects in Vim?

I use CtrlP to traverse files inside a project (which can usually be defined as a git repo root), but often find myself with :cd to switch between different projects that seem unnecessarily time-consuming.

I would like vim to be able to remember the different roots of the git repository I visited and quickly jump between them. After that, all the files in the repo will be available for CtrlP, and I can get to where I want.

Is there a way to get what I want with an existing plugin?

+5
source share
4 answers

If you use fugitive.vim , then I may have an option for you.

Put the following in the ~/.vimrc file:

 set viminfo+=! if !exists('g:PROJECTS') let g:PROJECTS = {} endif augroup project_discovery autocmd! autocmd User Fugitive let g:PROJECTS[fnamemodify(fugitive#repo().dir(), ':h')] = 1 augroup END command! -complete=customlist,s:project_complete -nargs=1 Project cd <args> function! s:project_complete(lead, cmdline, _) abort let results = keys(get(g:, 'PROJECTS', {})) " use projectionist if available if exists('*projectionist#completion_filter') return projectionist#completion_filter(results, a:lead, '/') endif " fallback to cheap fuzzy matching let regex = substitute(a:lead, '.', '[&].*', 'g') return filter(results, 'v:val =~ regex') endfunction 

Overview

The idea is that at any time fugitive activates a buffer that the script stores the project directory path in the g:PROJECTS dictionary. Adding ! in 'viminfo' will store capitalized global variables in the viminfo file, thereby making detected projects saved. Once the fugitive discovers the project, the command :Project can be used to :cd into this directory with completion.

Notes and Warnings

  • I have not tested this code. Use as is.
  • Fugitive.vim required
  • Optional Projectionist.vim completion, if available
  • Remember to add paths to g:PROJECTS other ways
  • You must visit the repository so that it can be detected
  • Unable to clear missing project directories
  • Vim has no concept of "project", so there is only so much that can be done
+4
source

Try vim-rooter : when you enter a buffer, it automatically changes the directory to the "root" buffer (for example, git root repo) directory.

+2
source

This tip suggests using headings as β€œfile bookmarks”.

For example, open .vimrc , press mV and close Vim. The next time you want to edit your .vimrc , just press 'V to open it.

+1
source

Just put the two projects in a new directory and open Vim in that directory, then you can use ctrlP to open the file in both projects.

If you do not want to move the project, create a soft link and place it in one directory.

+1
source

Source: https://habr.com/ru/post/1212481/


All Articles