While using vim I have trouble finding my cursor quickly after entering another buffer, especially if I have many splits open.

I fixed this by writing a function which is called by an autocmd when a buffer is entered. The function matches and unmatches a few characters around the cursor, waiting in between using sleep.

However, this gets annoying when switching quickly and a lot, or e.g. when opening a help tag, since my function is called after entering the buffer but before jumping to the help tag, so it appears late.

How can I achieve my goal without significantly slowing down vim?

EDIT: I worked around the help tag part by checking against the help filetype in my highlight function. However this is still not optimal.

This is my function:

function! HlCursor()
    let l:pat = '\v.{0,3}%#.{0,3}'
    " Let it blink for 250ms
    let l:match = matchadd('Todo', l:pat)
    redraw | sleep 250 m
    call matchdelete(l:match)
  • How do you think we will fix your function if you don't show it? – romainl Jul 27 '14 at 7:47

An alternative to your blocking sleep would be to only remove the highlighting on a following event. To do that, after the matchadd(), just define another (fire-once; i.e. one that deletes itself) autocmd that clears the highlighting, and return from the function. Suitable events would be WinLeave,CursorMoved,CursorMovedI,CursorHold,CursorHoldI.


How about using the built-in "cursorline" and "cursorcolumn" options? Then, you can use two autocmds: a BufEnter or WinEnter autocmd to do setlocal cursorline cursorcolumn and a CursorHold,WinLeave autocmd to do setlocal nocursorline nocursorcolumn.

  • I see now the two-autocmd method is basically what Ingo suggests to fix the delay issue, but this will also allow fixing that the wrong position gets highlighted when jumping to tags. – Ben Jul 28 '14 at 13:42

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.