Search code examples
vimctrlp

How to keep previous directory in CtrlP?


Using the CtrlP plugin, I'm typing .. several times to get to the root directory of a project, then perform the search from there.

However after I reopen CtrlP it still searches in the directory of the current file.

How can I get it to stick to the directory that I originaly set ?


Solution

  • By reading its documentation, of course.

    • If you want CtrlP to always open in your project's root directory, if applicable, just add this line to your ~/.vimrc:

      let g:ctrlp_working_path_mode=2
      
    • But you could also use both :CtrlP to browse in the current directory and :CtrlpRoot to browse in the root of your project.

    • Or simply use :CtrlPLastMode to launch CtrlP in… the last mode.

    • If your project doesn't fit CtrlP's definition of a project, a simple mapping could be enough:

      nnoremap <key> :CtrlP /path/to/project<cr>
      

      or you could use this option:

      let g:ctrlp_root_markers = ['']
      

    edit

    My answer was based on the version of CtrlP currently installed on my machine.

    The latest version adds an optional argument to :CtrlPLastMode: --dir which does exactly what you are looking for:

    :CtrlPLastMode --dir
    

    The g:ctrlp_working_path_mode values are different, too. I believe r is the equivalent of 2.