2

I want to disable cursorline when leaving the NERD tree window and enable it when entering the window again.

The following setting works fine when using <C-w>w or similar, but not when <CR>. Hitting <CR> opens the file in other window, moves to it, and cursorline remains visible. How can I make it work?

augroup Cursorline
  au!
  au WinEnter * if exists('t:NERDTreeBufName') && bufname("%") == t:NERDTreeBufName | setlocal cursorline | endif
  au WinLeave * setlocal nocursorline
augroup END

Environment:

- vim
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 14 2017 20:07:29)
Included patches: 1-1297
- NERDTree
master (461ea6d2d854100f7954c21453bb3b926ad930e6)
- OS
Linux
- Terminal
mintty on Windows
GNU screen
TERM=screen-256color-bce

.vimrc (simplified for testing):

set nocompatible
syntax enable

set runtimepath^=~/git/nerdtree/

autocmd! BufEnter * call ToggleCursorLine()

function! ToggleCursorLine()
  if (bufname("%") =~ "NERD_Tree_")
    setlocal cursorline
  else
    setlocal nocursorline
  endif
endfunction

2 Answers 2

1

This will work for all window panes not just the NERDTree pane. These lines go above in your .vimrc.

set cursorline 
set cursorcolumn

These lines go below in your .vimrc.

augroup cursor_off
    autocmd!
    autocmd WinLeave * set nocursorline nocursorcolumn
    autocmd WinEnter * set cursorline cursorcolumn
augroup END
1
  • Thanks, but what I want is to enable in NERDTree window, disable in other windows. Your solution makes cursorline always enabled in the active window.
    – aosho235
    Dec 20, 2017 at 4:05
0

If you would like to have this functionality, please but the following code in your .vimrc:

autocmd! BufEnter * call ToggleCursorLine()

function! ToggleCursorLine()
    if (bufname("%") =~ "NERD_Tree_")
        setlocal cursorline
    else
        setlocal nocursorline
    endif
endfunction

This creates a function, that will be executed via autocommand on entering any buffer and set cursorline accordingly. Tested on Mac with vim 8.0.237 in iterm 3.1.5.b2.

3
  • Tried it, but didn't work. cursorline is still highlighted in the NERD tree window after leaving it with <CR>. I'm starting to think that I need to modify NERD tree code.
    – aosho235
    Dec 11, 2017 at 18:36
  • Are you setting cursorline in the .vimrc? What OS/terminal/vim are you using? And you need to delete what you are using now...
    – mike
    Dec 11, 2017 at 18:39
  • bufname('#') =~ 'NERD_tree_\d\+', with this if condition this function works.
    – Dmitrii
    Feb 4, 2022 at 12:29

Your Answer

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

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