0

I was looking through the default mappings for square brackets, and I really don't need most of that functionality. I would like to disable these mappings, then use nnoremap [ [[ and nnoremap ] ]] to be able to jump between functions/definitions with one keystroke. Is this possible?

Perhaps I can turn on set timeout and set timeoutlen=0 for an individual remap...

  • You asked this one earlier, but the other one has an answer (which should work for both). – Rich Aug 18 '17 at 16:55
  • Actually, the solution in the other thread turned out not to work (try it for yourself) for brackets, although it did work for < and > perhaps because the latter waits for a motion, while the former is a special command-prefix. Can we re-open this? Not sure how. – Luke Davis Aug 18 '17 at 17:29
  • No I'll take your word for it. Apologies! Nominated for reopening. :) – Rich Aug 18 '17 at 18:41
4
nnoremap <nowait> ] ]]
nnoremap <nowait> [ [[

actually does work in plain text files, the problem is that many filetypes have custom <buffer> mappings for [[/]]. In this case, a global <nowait> map fails because there are still <buffer> mappings with that prefix. We need to make our mapping <buffer> as well. However, there are problems

  • We cannot use nnoremap <nowait><buffer> [ [[ because this would use vim's default [[ not the appropriate filetype's.
  • We cannot use nmap <nowait><buffer> [ [[ because this would create a recursive mapping with itself.

A solution is to use <buffer> maps with :normal:

nnoremap <silent><nowait> [ [[
nnoremap <silent><nowait> ] ]]

function! MakeBracketMaps()
    nnoremap <silent><nowait><buffer> [ :<c-u>exe 'normal '.v:count.'[['<cr>
    nnoremap <silent><nowait><buffer> ] :<c-u>exe 'normal '.v:count.']]'<cr>
endfunction

augroup bracketmaps
    autocmd!
    autocmd FileType * call MakeBracketMaps()
augroup END

Note that many filetype's [[/]] implementations don't take a count.

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.