neovim - Vim combination to select and change the word in this situation - Stack Overflow

admin2025-04-16  2

What is the proper Vim motion/action combination to perform a change or selection of a word 'patterns' while standing in the middle of it with the cursor? For example being on a second 't', like patterns.

function escape_special_patterns(text)

Are there any better options for this situation than just (for example) 4hct(

  • I'm aware and actively using ciw/viw/caw/vaw, but in this case the 'word' is the whole function name.
  • Also options like ci_ and vi_ are not applicable.

What is the proper Vim motion/action combination to perform a change or selection of a word 'patterns' while standing in the middle of it with the cursor? For example being on a second 't', like patterns.

function escape_special_patterns(text)

Are there any better options for this situation than just (for example) 4hct(

  • I'm aware and actively using ciw/viw/caw/vaw, but in this case the 'word' is the whole function name.
  • Also options like ci_ and vi_ are not applicable.
Share Improve this question asked Feb 3 at 16:16 pavel.brilliantpavel.brilliant 475 bronze badges 5
  • set iskeyword-=_ and then ciw – phd Commented Feb 3 at 16:37
  • 1 A simple T_cw should do. And it's not that much longer or more complicated than any of the ciw family. – Friedrich Commented Feb 3 at 16:40
  • @phd unfortunately couldn't get how will it affect the whole normal setup from the short help section for 'iskeyword' - will your suggestion override the default setting? – pavel.brilliant Commented Feb 3 at 18:05
  • 1 @pavel.brilliant It depends on how often you edit parts of keywords between underscores. I do it very seldom so I always have _ in iskeyword and isident. May be you'd want to remove it only for some file types. – phd Commented Feb 3 at 18:25
  • 1 @phd, if you can't find a proper duplicate you should write a short answer don't you think? – romainl Commented Feb 4 at 8:38
Add a comment  | 

2 Answers 2

Reset to default 2

set iskeyword-=_ to remove underscore from iskeyword and then ciw.

The problem with this approach is how to decide when _ must be included in iskeyword and when it must be excluded. Perhaps it's for file type plugins to decide.

Instead of trying to come up with a one size fits all solution, I'd use the small but useful commands Vim offers:

  • jump to a specific character, and
  • change until a specific character.

Starting with the cursor on a 't' of "patterns" (and how did you get there, anyway?), you can use T_ (or Fp) to move back to the start of "patterns". Then, use ve to Visual-select until the end of the word or cw to cut it and enter Insert mode.

Some variations do exist, all of the following will work:

T_ve
evT_
Fpve

and some more.

:help motion.txt is your friend.

转载请注明原文地址:http://www.anycun.com/QandA/1744762320a87259.html