Fork me on GitHub
#vim
<
2022-07-13
>
Martynas Maciulevičius15:07:34

Hey. Is there a way to make nvim . repeat key rerun the visual selections without forcing the range-based "memory"? What I mean by that is this situation:

bbbbb"[CURSOR]aaaaa"bbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
In here our cursor is on the first line which is shorter than the second. If we press keys vi" then it selects the region between the " signs. And then if we replace the selected region with character "c" by pressing rc it all works great. But then when we go down into the second line and press . this happens:
bbbbb"ccccc"bbbbb
bbbb"acccccaaaaaaa"bbbbbb
How can I make it so that it would actually replay my commands instead of compiling it into some kind of a range command? It would replace the " together with bbb if the second string would be longer. It remembers the coordinates and doesn't look at what it replaces. I remember that this was also the same on the regular vim and it was a weird thing that I couldn't make sense of. This works correctly for non-visual things like deletion if you do it without visual mode:
bbbbb"[CURSOR]aaaaa"bbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
Press di", then j. to repeat it on the next line:
bbbbb""bbbbb
bbbb""bbbbbb
And this also works incorrectly when I do it for multiline selection:
bbbbb"a[CURSOR]aaaa"bbbbb
bbbbb"aaaaa"bbbbb
bbbbb"aaaaa"bbbbb
bbbbb"aaaaa"bbbbb
bbbb"a[SECOND_CURSOR]aaaaaaaaaaaa"bbbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
Press Ctrl+v to enter block selection mode, press jje to select a block up to the ending of the aaaaa , press rc to replace into c. Great. Now the second part. Move the cursor to "SECOND_CURSOR" and press . . Result:
bbbbb"acccc"bbbbb
bbbbb"acccc"bbbbb
bbbbb"acccc"bbbbb
bbbbb"aaaaa"bbbbb
bbbb"accccaaaaaaaa"bbbbbb
bbbb"accccaaaaaaaa"bbbbbb
bbbb"accccaaaaaaaa"bbbbbb
bbbb"aaaaaaaaaaaaa"bbbbbb
😕

Leaf Garland02:07:53

Repeating visual mode changes has a specific behaviour (see :https://neovim.io/doc/user/visual.html#visual-repeat) that is not so useful for your particular example. Usually we can make easily repeatable changes by using operators, as in your example di" which can be repeated. Unfortunately replace is not an operator (probably because being able to type ri to replace a character immediately was seen as more useful). I haven't used them but there are some plugins for vim that provide a replace operator (search for 'vim replace operator'), they might help.

Martynas Maciulevičius04:07:35

Thanks. I'll try to look for something like this.

Martynas Maciulevičius15:07:54

TIL: go to a number. Press Ctrl+A or Ctrl+X . And it's also repeatable. Why is this a thing... Actually... it's something I'd use in a macro :thinking_face:

dave16:07:23

I was just about to say, this sounds like a good use case for a macro!

dave16:07:14

Something like f"lvi"rcj0

dave16:07:47

Go forward to ", visual select inside quotes, replace each character with c, go down to the beginning of the next line, rinse and repeat

Martynas Maciulevičius18:07:25

Yes, I did it with macros when I used vim. But now I use spacemacs and they support this same behavior without a macro. So I want to try nvim now and they have this weird thing again...

Martynas Maciulevičius18:07:28

So basically... why would I want to type more keystrokes for the same effect and even record a macro when I can have better defaults...