tags:

views:

137

answers:

3
+2  Q: 

vim multiline f

Hi,

Anyone knows how to quickly find the next occurrence of a character (like the f command) but multi-line? I.e. to quickly jump to the next occurrence of some character in a file?

Thanks!

Yaniv

+7  A: 

Isn't that what "/" does?

If you're looking for the next "x" then do /x while in command mode.

Then you can hit "n" to advance to the next x, and then the next x, etc.

There are lots of vim cheat sheets out there with all kinds of tips.

Peter Recore
That's exactly what / does! After you've done one search you can also hit "n" to go to the next occurrence.
GWW
dang it you beat me to the "n" clarification.
Peter Recore
just a shame you couldn't answer with a single character ;)
KevinDTimm
Thanks! I actually knew of / and was wondering if you know of a quicker way (sorry I didn't say that in the original question). Instead of typing / and then needing to type <enter> is there a single key way to do that? Thanks!
Koko
hmm... "fx" is two keystrokes. hitting "/x<enter>" is three keystrokes. after the first match, you can advance to subsequent hits with one more keystroke in both cases. who besides a vi(m) user could be upset at such inefficiency :) Are you always searching for the same character, or type of character? If so there may be solution for you, especially if it is programming related, like "move to the next curly brace"
Peter Recore
@Koko - the answer is, yes, there is one character to find the next occurrence, the letter 'n'. And that is included in the answer.
KevinDTimm
+2  A: 

There's an example of redefining f to ignore case in the 'eval.txt' help file.

:h eval.txt

(search for "ignore case" a couple times)

We can modify that to do what you want. Add the following function to your ~/.vimrc file or better yet, create a plugin file: ~/.vim/plugin/find-char.vim (create the directories if you do not already have them).

function FindChar()
    let c = nr2char( getchar() )
    let match = search('\V' . c)
endfunction

Then, in your ~/.vimrc add the following line:

nmap f :call FindChar()<CR>

Now, f should work like you want it to.

BTW, this was tested with Vim 7.2 on Ubuntu 10.04.

Curt Nelson
this will not work if you are searching for `$`, `\`, `^` or `/` among others (also maybe star, interrogation mark, etc. You could `let command = "normal! /\V" . c . "^M"`. Still this won't work for backslash.
Benoit
@Benoit: Ahh, special characters, I should have known better. I've updated the response.
Curt Nelson
You could skip the if statement if you just prepend every search pattern with '\V' -- function FindChar() let c = nr2char( getchar() ) let match = search('\V' . c)endfunction
Kimball Robinson
If you want the "last search" to be set to the character, add this to the function: call setreg("/", c) -- this way, the "n" and "p" commands are useful thereafter.
Kimball Robinson
You may also want to write the function and mappings so that the F, t, and T commands are similarly affected.
Kimball Robinson
A: 

You could make a mapping to go to the next character under the cursor:

:map f yl/\V<c-r>"<c-m>

the \V will make sure symbols are matched literally.

Also, you may want to look at the * and # commands, which are already defined for you, although they might not be what you want.

Kimball Robinson