I'd like to be able to highlight the current line the cursor is at, but not for files from certain directories. That's because I noticed the rendering becomes slow if I highlight the lines within a Rails project - probably because I use rails.vim and its slowing down things.

So, is there a way to highlight current line, but not when I'm in, say, ~/Work/my_rails_project_1 or ~/Work/my_rails_project_2?


I assume by "Highlight current line" that you're referring to the set cursorline option?

Sounds like a job for autocmd's

I'd do something like:

autocmd BufEnter * call UnhighlightLine()

function! UnhighlightLine()
   if (expand('%:p') =~ '/Work/my_rails_project_1\|/Work/my_rails_project_2')
      set nocursorline
      set cursorline

autocmd BufEnter * will trigger when you enter a new file

expand('%:p') Gives the current files path.

I have two paths separated by \| meaning it can match either one. If you want more paths just continue to separate with \|.

  • Unfortunately, I can't say this has worked. It highlights cursorline in all files now, including the ones in the said dirs. – snitko May 6 '16 at 3:18
  • There could be a few things going on. You might try surrounding the expand() function in an fnameescape() function. This just makes sure things are properly formatted for a directory. For more specific help, it might help to see the output of :echo expand('%:p') on your system. This will make sure we're crafting the regular expression correctly. – Tumbler41 May 6 '16 at 4:18

This is a job for autocmds. This works for me:

" set by default
set cursorline
" unset for specific cases  
autocmd BufEnter ~/Work/my_rails_project_1/*,~/Work/my_rails_project_2/* setlocal nocursorline

The *matches path separators, so this will apply to all directories inside ~/Work/my_rails_project_1/ and ~/Work/my_rails_project_2/. The setlocal will restrict the change to matching buffers.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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