I'm trying to set hlsearch on by default when I enter a buffer in vim.

Running :set hls manually upon entering a buffer performs the action I expect. However, when I add autocmd BufEnter * set hls to my .vimrc (and reload vim), nothing happens upon opening a buffer. Adding a colon in front of set doesn't change the behavior.

For context, I'm trying to use this script, but have it set "on" by default. I tried both of the tips mentioned in the "load at startup" section of the page with no success.

I removed everything else from my .vimrc, so it's not interference from something else.


I'm on Vim 7.4, 'huge' version, acquired with homebrew.

Here are the contents of my .vimrc when I tested the call AutoHighlightToggle() option. It doesn't highlight on start, but after two z/ presses works properly.:

nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>

function! AutoHighlightToggle()
   let @/ = ''
   if exists('#auto_highlight')
     au! auto_highlight
     augroup! auto_highlight
     setl updatetime=4000
     echo 'Highlight current word: off'
     return 0
    augroup auto_highlight
    au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
    augroup end
    setl updatetime=500
    echo 'Highlight current word: ON'
  return 1

call AutoHighlightToggle()
  • 1
    What is your vim version? call AutoHighlightToggle() works as expected for me. Vim 7.4.
    – thiagowfx
    Commented Jul 27, 2016 at 3:32
  • Is autocmd a shortcut of autocommand? I always use :au as a short form. You can rewrite and try. Also, after BufEnter, give *.*. Try and Tell.
    – SibiCoder
    Commented Jul 27, 2016 at 4:35
  • Added an edit with more details. Also tried the :au BufEnter *.* set hls, which does not work either. I've done other commands prefixed with autocmd BufEnter * which did work properly.
    – zplizzi
    Commented Jul 27, 2016 at 16:44

2 Answers 2


The command :se hls turns on an option only. You will visually see nothing change. See:

:help 'hlsearch'

(The quotes are meant to be typed.)

If what you are trying to do is get Vim to start highlighting the last used search string every time a new buffer is entered, that command won't help you.

Unfortunately Vim has no inverse of the :nohlsearch command, which tells Vim to stop highlighting the last searched string, and it appears that Vim refuses to let you kluge it in an autocmd (i.e. marking the cursor position, running the last search again, then returning to the mark). Vim seems to assume that an autocmd should never trigger search highlighting.

If you need this feature you should email the developers' list or the Vim maintainer directly and ask for a :hlsearch command to be added.

  • :com Hlsearch :let v:hlsearch=1 Commented Jul 29, 2016 at 9:24
  • @ChristianBrabandt - Good to know, but still doesn't work in an autocmd. It'd be nice if there were an override.
    – Heptite
    Commented Jul 29, 2016 at 16:19

Another option is to start vim with parameters:

nvim -c ":call AutoHighlightToggle()" -c "set hls" $@

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .