开发者

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

How to change the first two uppercase characters on each line to lowercase in Vim?

Enter gu or u to downcase the selected text, gU or U to upcase.

How to change the first two uppercase characters on each line to lowercase in Vim?


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: lguCTRL-VG.
    Detail :
    • l goes to next character
    • gu is the make lowercase operator, expecting a motion
    • CTRL-V specifies that the motion is blockwise
    • G goes to first column in last line.
  • if startofline is not set, then guCTRL-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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜