How to change the first two uppercase characters on each line to lowercase in Vim?
I have the follo开发者_运维百科wing text:
HELLO TO STACKOVERFLOW
WELCOME TO STACKOVERFLOW
I want the text to be changed as follows:
heLLO TO STACKOVERFLOW
weLCOME TO STACKOVERFLOW
How can I do that in Vim?
Highlight a visual block for the first two colums with CTRL-V
Enter gu
or u
to downcase the selected text, gU
or U
to upcase.
While @Jin provided a good answer for interactive use, here's a way to do it in scripting:
to run to every line of the buffer:
:%normal 0gu2l
or you can specify a line range where to apply the command. This will apply for lines 4 and 5:
:4,5normal 0gu2l
In normal mode:
- if
startofline
is set (:verb set sol?
will tell you) you can use:lgu
CTRL-VG
.
Detail :l
goes to next charactergu
is themake lowercase
operator, expecting a motionCTRL-V
specifies that the motion is blockwiseG
goes to first column in last line.
- if
startofline
is not set, thengu
CTRL-VGl.
(l
goes to next character, and.
repeats the same command).
For changing to uppercase change gu
with gU
, for switching case ensure that tildeop
is set and use ~
instead.
In addition to answers given by @Benoit, @Jin and @progo:
:%s/^../\L&\E/
see :help sub-replace-special
You can use the substitution
:%s/.*\%3c/\L&
which takes advantage of the \%c
search pattern atom matching the
character at a specific column on a line. Using that atom you can
easily adjust the pattern to match whatever number of first characters
on a line.
精彩评论