4

I have the following 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?

ib.
  • 26,410
  • 10
  • 77
  • 98

5 Answers5

16

Highlight a visual block for the first two colums with CTRL-V

enter image description here

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

enter image description here

Benoit
  • 73,313
  • 23
  • 201
  • 230
Jin
  • 11,769
  • 3
  • 33
  • 40
  • 8
    or `~` to invert case. Be cautious when sending screenshots, your window is half-transparent, have special care if you have sensible data behind (here no worries) – Benoit Oct 05 '11 at 07:04
  • 1
    @Benoit Yeah I will, but I don't have anything to hide :) Thanks for the edit and the `~` tip. – Jin Oct 05 '11 at 07:09
  • +1 for anyone else using VsVim in Visual Studio, the `~` trick works (`gu` and `gU` didn't work for me). – Eric Mutta Dec 28 '21 at 22:42
9

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
mike3996
  • 16,360
  • 7
  • 64
  • 78
  • 6
    +1, you can also specify the columns that you want to work with. `:%normal 15|gu3l` will downcase columns 15 to 17 for all rows. – Jin Oct 05 '11 at 06:57
5

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.

ib.
  • 26,410
  • 10
  • 77
  • 98
Benoit
  • 73,313
  • 23
  • 201
  • 230
2

In addition to answers given by @Benoit, @Jin and @progo:

:%s/^../\L&\E/

see :help sub-replace-special

Ves
  • 1,172
  • 1
  • 8
  • 18
1

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.

ib.
  • 26,410
  • 10
  • 77
  • 98