4

Is there a way to go forward to the first letter, maybe using f other than specifying the letter itself?

In other words I want to skip over whitespace, symbols and digits.

The reason I ask is that I want to make a macro and different sentences might begin with different letters, so I cannot put a particular letter in the macro. I need some way to say 'skip over everything that is not a letter' in the macro.

  • 6
    Is there any reason you couldn't do /\a<cr>? – DJMcMayhem Jul 17 '17 at 17:18
  • then use a search. – Christian Brabandt Jul 17 '17 at 17:37
  • @DJMcMayhem Adding a newline to a macro is tricky enough that it deserves a full explanation, if you're interested. You have to use ^V^M to record it, and that works slightly differently in the terminal than in GVim. – jpaugh Jul 17 '17 at 19:42
  • Do you need it to only find characters in the same line (as implied by your mention of f?) – Rich Aug 17 '17 at 9:31
7

Based on DLMcMMayhem's hint, I found that following search will do what you need:

/\A*\zs\a
  • \A* searches for zero or more non-alphabetic characters (equivalent to [^A-Za-z])
  • \a searches for any alphabetic character (equivalent to [A-Za-z])
  • \zs is a zero-width match (i.e. matches nothing) but tells the regex engine to reset the start of the match to the current position.

    • When moving to a match with n, the cursor will move to this position
    • When hlsearch is turned on, the highlighting will begin at this position

Mapping

If you need to use this frequently, you can set up a mapping in your .vimrc. This can be a bit tricky, because there are multiple ways to insert a keyboard code, some of which will only work in Vim (not GVim).

The easiest, most compatible way is to type out the following characters into your .vimrc, verbatim:

map <silent> <S-F11> /\A\zs\a<cr>
  • map sets up a keyboard mapping
  • The <silent> flag tells map not to echo the command when it executes
  • <S-F11> means Shift+F11

    If you're unsure how to spell a key, you can type Ctrl+V and then the key you want to map to, which will generate a unique code for any key chosen. The codes generated in terminal Vim, however, will be unique to that terminal and are not portable; specifically, they do not work in GVim.

  • Finally comes the keys we want the mapping to type out for us when we use the mapping key

    • The <cr> represents Enter; it's required because you type Enter to perform a search action

Edit

  • 2018-09-17 Add * to regex
  • Ok, to use this in a macro, would I first need to map this to a key and then use the key in a macro, or can I just type this into the macro directly? – Tyler Durden Aug 17 '17 at 17:19
  • @TylerDurden I forgot about the macro. Actually, just type the search as normal while recording the macro. That should work, and will continue to work even if you re-map (or forget to map) the keyboard shortcut. – jpaugh Aug 17 '17 at 17:44
  • @TylerDurden Stopped by to make an obvious (and quite late) edit to my answer. Did you ever find a working solution? – jpaugh Sep 17 '18 at 21:45
  • @jpaugh Why did you add \A* when \a works? With \A* wouldn't the search not work as expected if the first character in the line was alphabetic? – Steve Sep 18 '18 at 0:18
  • @Steve, Tyler needs to match the first alphabetic character, so we must ensure no alphabetic character precedes the \a match. My original regex, \A\zs\a, works in most cases, but does not match an alphabetic character at the beginning of the line. Thus, the *. – jpaugh Sep 18 '18 at 21:58
3

Yes. There's w (mnemonic: word) which moves to the beginning of the next word, and b (backward) which moves to the beginning of the previous word. Then there's e which moves to the end of the next word, and ge which moves to the end of the previous word.

You can change which characters are considered part of a word using the iskeyword option, and many filetype syntax plugins will set this to the characters which are allowed in identifiers for that language.

There's capital letter variants (like W) which counts any non-space character as a word.

  • Downvoter: What did I miss? – jpaugh Jul 17 '17 at 19:43
  • 1
    I did not downvote you, but be aware that 'w' will move forwards to symbols and digits, not just letters. – Tyler Durden Jul 17 '17 at 20:02
  • @TylerDurden It also counts - as its own word, at least by default. Changing its behavior with iskeyword is probably a necessity. (I guess that's what I get for posting an incomplete answer at first.) – jpaugh Jul 17 '17 at 20:06

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.