Question

I sometimes need to write the whole alphabet abcd…z and I hate typing it letter by letter in Vim's insert mode. Does there exist any method to do this more efficiently?

I know about the ga command which gives me the ascii code of the character where the cursor is … but don't know anything about how to mix it with my standard solution to type numbers from 1 to (for example) 5000: a1ESCqqyyp^Aq4998@q

Was it helpful?

Solution

Using set nrformats+=alpha:

ia<Esc>qqylp<C-a>q24@q

Step by step:

ia<Esc>      " Start with 'a'
qqylp<C-a>q  " @q will duplicate the last character and increment it
24@q         " Append c..z

OTHER TIPS

If your shell does brace expansion this is a pretty elegant solution:

:r !printf '\%s' {a..z}

:read! reads the output of an external command into the current buffer. In this case, it reads the output of the shell's printf applied to {a..z} after it's been expanded by the shell.

How about this command:

:put =join(map(range(char2nr('a'),char2nr('z')),'nr2char(v:val)'),'')

Collect the ASCII values of the characters in the range from a to z, then map them over the nr2char() function and insert the result into the current buffer with :put =.

When you leave out the enclosing join(,'') you get the characters on a separate line each.

See

First, set nrformats+=alpha.

Then:

ia<ESC>Y25p<CTRL-V>}g<CTRL-A>k26gJ

Which means:

  • ia insert the initial a
  • Y25p yank the a and duplicate it on 25 lines
  • <CTRL-V> go into visual block mode
  • } go to the last character at the end of the current paragraph
  • g<CTRL-A> incrementally increase each alphabetic character (see help v_g_CTRL-A)
  • k go up one line
  • 26gJ join 26 lines without inserting or removing any spaces

Which leads to:

abcdefghijklmnopqrstuvwxyz

I have found a shorter solution (you don't need to change nrformats beforehand) while solving http://www.vimgolf.com/challenges/5ebe8a63d8085e000c2f5bd5

iabcdefghijklm<Esc>yiwg??P

which means:

  • iabcdefghijklm<Esc> insert first half of the alphabet
  • yiw copy it
  • g?? ROT13 encode (shift by 13 letters) to get the second half
  • P paste the first half

You might try using Vim abbreviations or a full-fledged snippet manager plugin like UltiSnips. It might take a few moments to set up, and you'd have to type that alphabet one more time to define it as an abbreviation or snippet, but after that you'd be able to insert the alphabet or any other common chunk of text much more easily.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top