Fast way to type or change to AlTeRnAtInG CaPs?


Substitution is one way...



  1. \v : use "very magic" mode
  2. (.) : first dot is our "odd" character; enclosed in capture group 1
  3. (.?) : second dot is our "even" character; enclosed in capture group 2; have to make it optional in order to capitalize last char of line if it's "odd"
  4. \u : convert to uppercase the character that follows which is...
  5. \1 : ...the "odd" character
  6. \l : convert to lowercase the character that follows which is...
  7. \2 : ...the "even" character
  8. g : ensure that this is repeated for every pair of characters on the line.

You could use a macro, started on the first letter of the word (and assuming it's all lower cast to begin with (if not do guu to put the line into lower case):


Then just do @a to replay the macro (and thereafter @@ to repeat). This won't ensure that the first letter of each word in a sentence is of the same case though:

SoMe aLtErNaTiNg tExT

If you need to exchange upper and lower case in some non-alternating pattern, i.e change this:

SoMe wEIRd capiTaliSation Going on herE

to this:


you can do g~~ (thanks to @Rich for the tip)

  • Your second example (exchanging text of line) you can do inside Vim by typing g~~
    – Rich
    May 29 at 11:22
  • 1
    @Rich Thanks - I never realised there was a g~ command
    – mattb
    May 29 at 11:28
  • You're missing the final q to stop recording the macro. You need to execute it at least once (with @a) before you can repeat it (with @@). You can use a count to repeat it enough times, e.g. 999@a. At the beginning, you might want to use guu to lowercase the whole line (not just the current word.)
    – filbranden
    May 29 at 18:57
  • 1
    Ah yes @filbranden you're right, thanks - I was surprised when @@ worked for me (I thought I'd discovered a new behaviour I was previously unaware of), but I must have been using an old recording of the same macro when I was testing it for the answer. Updated the answer now.
    – mattb
    May 29 at 20:43

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.