How do you convert all text in Vim to lowercase? Is it even possible?
-
We have a lot of html pages with all characters in uppercase. This saves me time re-typing everything in lowercase. – ksuralta Jul 13 '09 at 5:30
-
2Before the question was edited, it was how to convert of all text in vim to small caps. Obviously, there's a use case for converting text to lowercase. – lemonad Aug 8 '09 at 13:00
-
6VIM website has the answer: vim.wikia.com/wiki/Switching_case_of_characters Thanks. – fnds Nov 9 '12 at 16:43
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
(orU
to convert to uppercase). Tilde (~
) in command mode reverses case of the character under the cursor.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.
-
33I 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, andgu[motion]
will lowercase that motion's worth of text. Likewise,gUU
andgU[motion]
work the same way, only for uppercase. – Zachary Murray Jul 9 '09 at 10:28 -
1Actually, smallcaps are also possible for most letters, using ᴀʙcᴅᴇꜰɢʜıᴊᴋʟᴍɴoᴘʀsᴛᴜvwxʏz. – gerrit Aug 18 '15 at 12:52
-
1That’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
I assume you want lowercase the text. Solution is pretty simple:
ggVGu
Explanation:
- gg - goes to first line of text
- V - turns on Visual selection, in line mode
- G - goes to end of file (at the moment you have whole text selected)
- u - lowercase selected area
-
88
-
18
Similar to mangledorf's solution, but shorter and layman friendly
:%s/.*/\L&/g
-
3
:%s/.*/\L&
would suffice (don't need theg
flag since.*
selects the entire line) – Gordon Gustafson Sep 28 '14 at 19:30 -
1As 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 forwhat 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
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.
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
- 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
use ggguG
gg : goes to the first line . gu : change to lowercase . G : goes to the last line .
-
1
-
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
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).
If you are running under a flavor of Unix
:0,$!tr "[A-Z]" "[a-z]"
-
4The square brackets are superfluous, and once you remove those, the quotes aren't necessary either.
:%!tr A-Z a-z
– ephemient Jul 13 '09 at 4:21 -
The
!
indicates an external shell command, so$ man tr
(as opposed to:help tr
) reveals thattr
is fortranslate
. – Braden Best Oct 2 '14 at 3:47 -
I had a similar issue, and I wanted to use ":%s/old/new/g"
, but ended up using two commands:
:0
gu:$