10

I use both CtrlP and NERDTree in my Vim. When I open a file using the CtrlP search feature, it sometimes opens in the NERDTree window (and always if the NERD window is the active one).

How can I stop CtrlP from opening files in NERDTree, and force it to open them in the main window? I've tried going through the CtrlP docs in Vim, but couldn't figure out a way.

These are all the NERDTree and CtrlP related configs in my .vimrc:

let g:netrw_liststyle    = 3
let NERDTreeShowHidden   = 1
let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files -co --exclude-standard']

command E Ex

map <C-t> :NERDTreeTabsToggle<CR>
nmap <Leader>r :NERDTreeFocus<cr>R<c-w><c-p>:CtrlPClearCache<cr>

5 Answers 5

8

Finally found a way to do this that doesn't involve closing NERDTree all the time.

I made a function that cycles through the open windows until it finds a writable buffer, then runs ctrl-p there:

function! CtrlPCommand()
    let c = 0
    let wincount = winnr('$')
    " Don't open it here if current buffer is not writable (e.g. NERDTree)
    while !empty(getbufvar(+expand("<abuf>"), "&buftype")) && c < wincount
        exec 'wincmd w'
        let c = c + 1
    endwhile
    exec 'CtrlP'
endfunction

let g:ctrlp_cmd = 'call CtrlPCommand()'

Should work for any panel, for example MiniBufferExplorer.

1
  • Excellent, thanks for sharing. Have you considered maybe contributing that to Ctrl-P directly?
    – bobylito
    Jul 30, 2017 at 16:41
4

I've tripped on this one too many times too:

I've solved it by remapping the <c-p> to closing NERDTree (if open) and then opening CtrlP.

Put this in your .vimrc:

let g:ctrlp_map = ''                      
nnoremap <c-p> :NERDTreeClose\|CtrlP<CR>  

Explanation: The first line makes CtrlP not overwrite your custom mapping. And the second one runs close on NERDTree before opening CtrlP.

3

From @jonasl answer, you could also do:

let g:ctrlp_cmd = ':NERDTreeClose\|CtrlP'
2

To expand on @DJ Madeira's answer, I made this function reusable, since I am also using ctrl+l for MRU list

" CtrlP
" Use this function to prevent CtrlP opening files inside non-writeable 
buffers, e.g. NERDTree
function! SwitchToWriteableBufferAndExec(command)
    let c = 0
    let wincount = winnr('$')
    " Don't open it here if current buffer is not writable (e.g. NERDTree)
    while !empty(getbufvar(+expand("<abuf>"), "&buftype")) && c < wincount
        exec 'wincmd w'
        let c = c + 1
    endwhile
    exec a:command
endfunction

" Disable default mapping since we are overriding it with our command
let g:ctrlp_map = ''
nnoremap <C-p> :call SwitchToWriteableBufferAndExec('CtrlP')<CR>
nnoremap <C-l> :call SwitchToWriteableBufferAndExec('CtrlPMRUFiles')<CR>
1

Functions from other answers didn't work for me, but I found a simple solution that works if you always keep NERDTree open like I do. There's no command to unfocus NERDTree, but we can focus it and then switch to the previous window to ensure it's unfocused. Note that this will cause it to open if it wasn't.

let g:ctrlp_map = ''
map <C-P> :NERDTreeFocus<CR>:wincmd w<CR>:CtrlP<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.