1

For example I'm in python code and want to jump between classes:

nnoremap <buffer> [c /^\s*class\ <CR>

How to prevent them from highlight in more elegant way than :nohl at the end of command each time?

romainl
  • 172,579
  • 20
  • 259
  • 291
kAldown
  • 580
  • 1
  • 7
  • 26

1 Answers1

1

You can avoid highlighting search matches by using the :help search() function or writing your own function.

With search()

nnoremap <buffer> <silent> [c :<C-u>call search('^\s*\zsclass\s')<CR>

With your own function

" with ':help :normal'
function! JumpToNextClass()
    normal! /^\s*\zsclass\s
endfunction

" with ':help search()'
function! JumpToNextClass()
    call search('^\s*\zsclass\s')
endfunction

nnoremap <buffer> <silent> [c :<C-u>call JumpToNextClass()<CR>

But none of that really matters since Vim already comes with ]] and [[.

romainl
  • 172,579
  • 20
  • 259
  • 291
  • big thanks @romainl for sharing info about `[[`, but I'm wondering, is it possible to patch this and behave in nested definition (in class methods, it won't work for python for ex). – kAldown Sep 03 '17 at 10:30
  • The default Python ftplugin already has a function that's mapped to `[[` and `]]` which, I'll assume, is already tuned for Python. That function is already pretty generic and can be extracted easily into an even more generic utility function that you can tweak at will. – romainl Sep 03 '17 at 10:59