20

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

Maria Matejka
  • 203
  • 2
  • 6
  • 8
    ... or go low-tech `:inoreab abc abcdefghijklmnopqrstuvwxyz` :) – glts Aug 07 '13 at 18:31
  • 1
    If you really need to type the whole alphabet often then an abbrev is the way to go. It will be much easier to invoke then having to remember a programmatic way of doing it. – Randy Morris Aug 07 '13 at 19:04
  • BTW, if you want to copy the sequence from @glts, then make it vertical text, run this on that line: `s/\(.\)/\1\r/g`...just had to do that... – JakeD Jul 14 '18 at 02:54

6 Answers6

35

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
Nikita Kouevda
  • 5,508
  • 3
  • 27
  • 43
  • 2
    If you didn't post here about `nrformats` that option would definitely be the last i get to know about! What did you used this option for so that you are aware of it? – user1146332 Aug 07 '13 at 20:24
  • Is it possible to use the increment in some way for written numbers, such as `one`, `two`, `three`, etc? – nilon Nov 29 '18 at 21:32
18

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.

glts
  • 21,808
  • 12
  • 73
  • 94
7

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

glts
  • 21,808
  • 12
  • 73
  • 94
3

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
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
2

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
serycjon
  • 510
  • 5
  • 14
  • It works beautifully. But why vim has 'g??' like function? Does this need for any other reason without code golf? – JongHyeon Yeo Aug 20 '20 at 13:53
  • 1
    apparently "on Usenet it used to be a common way to encode spoilers" (https://stackoverflow.com/a/25224825/1705970)... Also don't forget vim has stuff like ":smile"... – serycjon Aug 20 '20 at 19:07
0

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.

jgarbers
  • 231
  • 3
  • 13