3

I have not found much use for ()[]{}, and so I want to map them so it does a find command, jumping to the previous instance of that particular character.

Thus, ) will jump forward to the next ), placing the cursor on top of it. ( will jump backward to the previous (, again, placing the cursor on top of it.

Problem is, f stops at linebreaks, and ?/ would eliminate my highlight.

Is there a solution for this issue?

2

You can use the search() command for this. Something like:

nnoremap ) :call search(')')<CR>      "Next
nnoremap ( :call search('(', 'b')<CR> "Previous

Edit: Origial response before edit:

nnoremap ) yl:call search('<C-r>"\C')<CR>      " Next instance
nnoremap ( yl:call search('<C-r>"\C', 'b')<CR> " Previous instance

This will yank the character under the cursor and then use the search command to go to the next instance.

  • nnoremap ( Make a non-recursive normal mode mapping for (
  • yl Yank character under cursor
  • :call search('...') Calls the search command
  • <C-r>" Inserts the contents of register " (The character we just yanked)
  • \C Makes the search case sensitive
  • 'b' Search backwards
  • <CR> The Enter key (Simply executes the call)

See :help search() for more info.

  • nnoremap ( :call search('(', 'b')<CR> " Next instance I tried adapting your code, because I do not want yank a character under my cursor. I just want to jump to that bracket. Can not seem to make it work though, because the cursor most of the time does not land on the actual character, just the line. Could not get reliable results with the find command appended either. – Akiva May 4 '17 at 17:53
  • I see. I recommend editing your question as it's really not clear that you want to search for just (. nnoremap ( :call search('(')<CR> should work for you for next. 'b' is for searching backwards. – Tumbler41 May 4 '17 at 18:04

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.