Reputation: 1244
I was changing some words to uppercase, I hit viw~
to change the case of the word from lowercase to uppercase. I moved foward a word and hit .
to repeat the action on the next word and I noticed that it affected the case of some of the letters of the word ahead and on other times it would not change case of the entire word.
Here is an example done with vim -u NONE
on a file with a single sentence
this is a test sentence
with my cursor at the start of the sentence, I hit viw~ my sentence is now:
THIS is an example test sentence
I move forward by 1 word with w and hit . to repeat the action. My sentence is now:
THIS IS An example test sentence
w.
THIS IS aN Example test sentence
w.
THIS IS aN eXAMple test sentence
w.
THIS IS aN eXAMple TEST sentence
The same behaviour happens when I instead capture the actions as a macro instead of using .
I suspect vim is just changing the case of the same number of letters as was in the first word, but why? Why does viw
not work in macros?
Upvotes: 1
Views: 67
Reputation: 172698
I suspect vim is just changing the case of the same number of letters [...]
You're right. In order to do what you expect, Vim would have to remember how you created the visual selection. In your example, that was easy (iw
), but you may apply multiple text objects and movements, use o
to move to the other side of the selection, modify that, and so on. That would be very hard to recreate elsewhere, and to avoid inconsistent behavior, Vim consistently acts stupid and just uses the previous width of the selection when redoing with the .
command.
If you want to apply an operation to a certain text object or motion, skip visual mode and instead use the corresponding motion-mapping; e.g. g~iw
instead of viw~
.
Upvotes: 4
Reputation: 196781
The action was repeated on an area equivalent to the area covered by the previous command. This has nothing to do with macros.
From :help .
:
Note that when repeating a command that used a Visual selection, the same SIZE
of area is used, see visual-repeat.
and from :help visual-repeat
:
When repeating a Visual mode operator, the operator will be applied to the
same amount of text as the last time:
- Linewise Visual mode: The same number of lines.
- Blockwise Visual mode: The same number of lines and columns.
- Normal Visual mode within one line: The same number of characters.
- Normal Visual mode with several lines: The same number of lines, in the
last line the same number of characters as in the last line the last time.
The start of the text is the Cursor position. If the "$" command was used as
one of the last commands to extend the highlighted text, the repeating will
be applied up to the rightmost column of the longest line.
One of Vim's strengths is that you don't need to select text before doing many actions. In this case, you should use the :help g~
operator, which will be repeated with .
in a more intuitive way:
g~iw
instead of:
viw~
Upvotes: 5