7

When I'm working in vim, I usually have multiple buffers on screen at once and need to switch between them and open new files a lot. I've installed the Command-T plugin to facilitate fast file switching but I'm having a few issues with it.

Whenever I press Command-T, it only shows files in the same folder (and sub folders) of the file I am currently editing.

I want it to show me all of the files in my project i.e.:

Project Folder: ~/project1/ Current Buffer: ~/project1/front/js/file1.js 

When I press Command+T in the buffer, I want it to display files starting from the project folder.

Thanks.

3
  • Is there any sign that can be used to identify the main project directory? Like a certain pattern in the directory name, or a file/directory that only exists in the main directory of each project? Commented Nov 22, 2011 at 11:25
  • No, But I could make a change so that there is eg. a file called project in the root. I guess this could be used in a recursive search? Commented Nov 22, 2011 at 11:37
  • Possibly the harpoon plugin fits your needs of switching files within projects. Commented Dec 22, 2021 at 8:54

5 Answers 5

4

Put the following function in your .vimrc:

function! FindProjectRoot(lookFor) let pathMaker='%:p' while(len(expand(pathMaker))>1) let pathMaker=pathMaker.':h' let fileToCheck=expand(pathMaker).'/'.a:lookFor if filereadable(fileToCheck)||isdirectory(fileToCheck) return expand(pathMaker) endif endwhile return 0 endfunction 

That function will search upward from to current file for a parent folder that contains a file or folder with the specified name, and returns that path. That way, you can put a project file in the project's root folder, and send FindProjectRoot('project') as an argument to Command-T

Or even better - call that file project.vim, and use it to load specific settings and keybindings for each project.

EDIT that function will only work on linux. Use this function instead:

function! FindProjectRoot(lookFor) let pathMaker='%:p' while(len(expand(pathMaker))>len(expand(pathMaker.':h'))) let pathMaker=pathMaker.':h' let fileToCheck=expand(pathMaker).'/'.a:lookFor if filereadable(fileToCheck)||isdirectory(fileToCheck) return expand(pathMaker) endif endwhile return 0 endfunction 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Idan, This looks like the sort of thing I want - I can use it in conjunction with @David Brown's answer to find the project root.
3

The problem you have is because CommandT searches in present working directory. You can see what your working directory is with the :pwd command. When you open a file with the gui it sets your present working directory to the directory that file contains. As romainl said you can simply :cd into your project's root directory and then CommandT will search from there (remember to :CommandTFlush)

Alternatively you could use ctrl-p (which I personally prefer since it doesn't require compiling a C helper program). It has an option ctrlp_working_path_mode option changes the working directory to the first ancestor that contains one of the following directories or files:

.git/ .hg/ .bzr/ _darcs/ root.dir .vimprojects 

Comments

3

:help command-t-usage recommends to :cd into your project folder before using it.

You can also pass it a path as argument as in

:CommandT .. 

3 Comments

Hi, I understand that; just seems like a lot of hassle when trying to quickly open files.
There are a few nice answers in this thread. I agree with you, the default way seems like a hassle but you can do something like nnoremap <leader>tt :CommandT ..<CR> to make the whole thing easier.
On a side note, I find that using tags and gf are enough to satisfy most of my in-project navigational needs.
2

Disclaimer: I wrote the following plugin

I would suggest to use codepath.vim if you are used to store all your code in the same dir. So, you could do the following:

map <silent> <C-T> :CommandT <c-r>=codepath#path()<CR><CR> imap <silent> <C-T> <ESC>:CommandT <c-r>=codepath#path()<CR><CR> 

And CommandT will open itself in the root dir of your project.

1 Comment

it's very useful, it was case
0

I don't know about the Command-T plugin, but you could try set path=<project_path> to set Vim's search path for files.

If not, setting :cd <path> would probably work, but that's rather a drastic solution!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.