214

How do you convert all text in Vim to lowercase? Is it even possible?

| |

10 Answers 10

123
  1. If you really mean small caps, then no, that is not possible – just as it isn’t possible to convert text to bold or italic in any text editor (as opposed to word processor). If you want to convert text to lowercase, create a visual block and press u (or U to convert to uppercase). Tilde (~) in command mode reverses case of the character under the cursor.

  2. If you want to see all text in Vim in small caps, you might want to look at the guifont option, or type :set guifont=* if your Vim flavour supports GUI font chooser.

| |
  • 33
    I think it's worth mentioning that you don't necessarily have to create a visual block to lowercase a block of text. guu will lowercase a line, and gu[motion] will lowercase that motion's worth of text. Likewise, gUU and gU[motion] work the same way, only for uppercase. – Zachary Murray Jul 9 '09 at 10:28
  • 1
    Actually, smallcaps are also possible for most letters, using ᴀʙcᴅᴇꜰɢʜıᴊᴋʟᴍɴoᴘʀsᴛᴜvwxʏz. – gerrit Aug 18 '15 at 12:52
  • 1
    That’s just a hack, though. These are not meant to be used as regular small caps and are not well supported by fonts. (On my desktop machine, the F renders strangely, on my phone it does not render at all, to give an example.) Anyway, interesting part of Unicode, thanks! – zoul Aug 21 '15 at 5:30
  • 2
    This answer is incorrect. user80168 has the correct answer. – Wayne Workman Nov 12 '19 at 4:46
404

I assume you want lowercase the text. Solution is pretty simple:

ggVGu

Explanation:

  1. gg - goes to first line of text
  2. V - turns on Visual selection, in line mode
  3. G - goes to end of file (at the moment you have whole text selected)
  4. u - lowercase selected area
| |
43

Similar to mangledorf's solution, but shorter and layman friendly

:%s/.*/\L&/g

| |
  • 3
    :%s/.*/\L& would suffice (don't need the g flag since .* selects the entire line) – Gordon Gustafson Sep 28 '14 at 19:30
  • 1
    As would :%s/./\L&/g, since /g/ denotes an operation that is global for the line. What gets me, though, is the &. Why is that necessary? – Braden Best Oct 2 '14 at 3:51
  • 1
    & stands here for what was matched by the pattern, so it can be understood as follows: replace .* by \Lowercase(what was matched by the pattern). Note that matching on . globally is slower than matching .* – Camusensei Sep 2 '19 at 9:33
42

use this command mode option

ggguG


gg - Goto the first line 
g  - start to converting from current line    
u  - Convert into lower case for all characters
G  - To end of the file.
| |
27

Many ways to skin a cat... here's the way I just posted about:


:%s/[A-Z]/\L&/g

Likewise for upper case:


:%s/[a-z]/\U&/g

I prefer this way because I am using this construct (:%s/[pattern]/replace/g) all the time so it's more natural.

| |
  • I think there is a Typo in the last example ('likewise for upper case') - the regex should change to [a-z] rather than '[A-Z]' . – monojohnny Nov 18 '13 at 16:13
  • 1
    :%s/./\U&/g also works. Turns out it ignores numbers and non-alphabet characters. What gets me is the &. Why isn't \U (or \L) by itself enough? Can we get an explanation for that? – Braden Best Oct 2 '14 at 3:43
  • 2
    & is a stand-in for the matched string. So \U& capitalizes the matched string so that it may be used for the replacement. – Alec Jacobson Oct 2 '14 at 13:56
23
  • Toggle case "HellO" to "hELLo" with g~ then a movement.
  • Uppercase "HellO" to "HELLO" with gU then a movement.
  • Lowercase "HellO" to "hello" with gu then a movement.

For examples and more info please read this: http://vim.wikia.com/wiki/Switching_case_of_characters

| |
  • I guess there isn't, but there's a less-than-intuitive substitution command for that in @mangledorf 's answer by replacing the desired characters with the escape sequence "\L&" for lowercase, and "\U&" for uppercase. What gets me is the &. \L and \U make perfect sense, but why is the & necessary? – Braden Best Oct 2 '14 at 3:42
10

use ggguG

gg : goes to the first line . gu : change to lowercase . G : goes to the last line .

| |
  • 1
    How does that differ from the answer of Kalanidhi? – MSeifert Jan 14 '17 at 21:14
  • Upvoted to bring this to 0, which is an adequate score for an answer that is a copy of a previous one. @MSeifert Negative scores should be reserved for wrong / harmful solutions – Titou May 24 '18 at 18:59
7

Usually Vu (or VU for uppercase) is enough to turn the whole line into lowercase as V already selects the whole line to apply the action against.

Tilda (~) changes the case of the individual letter, resulting in camel case or the similar.

It is really great how Vim has many many different modes to deal with various occasions and how those modes are neatly organized.

For instance, v - the true visual mode, and the related V - visual line, and Ctrl+Q - visual block modes (what allows you to select blocks, a great feature some other advanced editors also offer usually by holding the Alt key and selecting the text).

| |
6

If you are running under a flavor of Unix

:0,$!tr "[A-Z]" "[a-z]"
| |
2

I had a similar issue, and I wanted to use ":%s/old/new/g", but ended up using two commands:

:0
gu:$
| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.