Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you yank between two different symbols in vim?

Tags:

vim

Given I have:

"foo === BAR.BAZ"

If my cursor is in the middle of the string I want to yank (say 'R', in this case), and I want to yank all of BAR.BAZ, how would I do that easily?

I can't just use yiw because it breaks on the .. I need to yank between <space> and "

This is just one example. I realize that there are ways to make vim yank words separated by periods, but I'm looking for a more general solution for cases that involve more complex symbols.

Something like "yank between <first_occurrence_of_left_symbol> <first_occurrence_of_right_symbol>"

Which in the above case would be:

yb<space>"

like image 626
AgmLauncher Avatar asked Nov 01 '25 10:11

AgmLauncher


1 Answers

You can define your own text object like this:

onoremap I :exec 'norm vT'.nr2char(getchar()).'ot'.nr2char(getchar())<cr>

Then, to do your example, you would type

yI<space>"

The mnemonic I think of is "(d)elete (I)nbetween <char 1>, <char 2>". Thankfully, I cannot be used as an argument to an operator, so this doesn't override anything. Of course, you can pick a different key if it would be more intuitive to you.

The convenient thing about doing this as an onoremap mapping, is that this gives you the ability to use any operator on that text. So d, c, gu, y, and many others will work just fine.

While you're at it, you can add a mapping for around two characters also, it's a trivial modification:

onoremap A :exec 'norm vF'.nr2char(getchar()).'of'.nr2char(getchar())<cr>
like image 120
DJMcMayhem Avatar answered Nov 04 '25 04:11

DJMcMayhem