Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making backwards motions always inclusive?

Tags:

vim

Background about exclusive motions for those who need:

I've always been annoyed by how motions work in Vim after an operator. Recently I heard this is because of them usually being "exclusive": they leave out the rightmost character as opposed to "inclusive" operations which would pick up everything under the cursor between start and end points. For some reason almost all motions in operator pending mode seem to work this way, for instance pressing "dl" will only delete the character under the cursor (leaving the rightmost second character out), d0 at the end of a line will leave the last character on the line unaffected. Some motions like "d$" or "de" then again break this pattern and also include the rightmost character. More at Motion.txt.

I find the concept of exclusivity unnecessary and confusing. I can understand that when you are "searching" for the next occurrence of a word, a mark or a search result, you'll usually want to leave the actual target of the motion unselected when operating. Exclusive motions achieve this. But the inconsistency of the exclusivity concept is weird. Main problem is when you move somewhere backwards: the operation will leave the rightmost character, the starting character, untouched, and still will operate on the target of your movement no matter what you do, unlike when searching forwards. For example, you'd expect "edb" to delete a word completely, but it doesn't. Meanwhile "bde" does. "dl" deletes only the character under cursor (instead of including also the one to the right) and "dh" only one character on the left. This inconsistency is annoying, and seemingly makes it look like Vim secretly is keeping the cursor actually between letters on the left side of the cursor block.

Luckily, using "v" after the operator toggles exclusivity for characterwise operations, so I've been able to fix some motions such as b and h with onoremap vb. But it's only a partial solution; Many "searching" movement commands may move either backwards or forwards, so making them include the rightmost character when moving backwards is impossible without making forward movement undesirably affect the first character of the target of motion.

My question in short: Is there any simple way to make all operations work inclusively (as to include the character under the starting point of the cursor) when moving backwards relative to cursor position, without affecting forward motion?

(Even better would be entirely forgetting the whole exclusivity thing and having "v" toggle whether to include the target of the motion (so "dn" would delete from cursor till the start of the search result, "dvn" would delete from cursor to the end of the result), but that would probably mean editing the source.)

Sorry for long post, it was hard to explain and I'm annoyed.

like image 494
Wiener Boat Avatar asked Feb 10 '14 20:02

Wiener Boat


1 Answers

In most scenarios described by you, vim text-objects would certainly be better candidates to use rather than motions.

like image 140
Dhruva Sagar Avatar answered Nov 13 '22 12:11

Dhruva Sagar