56 votes
Accepted

How can I generate a list of sequential numbers, one per line?

Use :put and range(): :put =range(1,100) To avoid the blank line at the top (kudos to romainl), use :0put: :0put =range(1,100)
user avatar
  • 2,423
42 votes
Accepted

How to add permanent line numbers to a file?

In pure Vim fashion: :%s/^/\=line('.').". " Explanation: :%s/^/ " the substitution will be applied to the beginning of every line \= " the rest of the replacement part is ...
user avatar
  • 32.5k
24 votes
Accepted

How to generate random numbers?

Starting with Vim 8.1.2342, you can make use of the rand() and srand() functions. Here is an example: let g:seed = srand() echo rand(g:seed) % 100 " to echo a random number between 0-99 For ...
user avatar
20 votes

How can I generate a list of sequential numbers, one per line?

In addition to Undo's pure-vim :put =range(1,100) (which actually leaves you with a blank line up top), you can, depending on your OS, use one of its commands. E.g., on a Unix/Linux box: %!seq 1 100 ...
user avatar
  • 1,815
19 votes

How can I generate a list of sequential numbers, one per line?

For the record, and definitely not the shortest way (see @Undo's awesome solution), but sequence of keystrokes will do it too: i1EscqaYpCtrl+aq98@a Let me break that down for you: i1<Esc> -- ...
user avatar
  • 2,618
13 votes

How to generate random numbers?

Here is a pure vimscript solution. I did not create it, it was developed by Charles E. Campbell. You can find a Github repo with his code here. The algorithm uses 3 seeds generated at Vim startup and ...
user avatar
  • 45.9k
12 votes

How to generate closing tags for HTML?

Here is a simple ad-hoc solution to put in ~/.vim/after/ftplugin/html.vim: inoremap <buffer> > ></<C-x><C-o><C-y><C-o>%<CR><C-o>O Breakdown: >&...
user avatar
  • 32.5k
11 votes

How to add permanent line numbers to a file?

One nice thing about Vim macros is that they can recurse (they can invoke themselves): Clear out register q: qqq Add the number to the first line: ggI1. (don't forget the space!) Move back to start ...
user avatar
  • 27.3k
10 votes

How to copy each line 11 times, incrementing the last "1" in each line from 2-12

You could do this by recording a macro, then using the global ex command to execute the macro n number of times for each line in the file. After recording the macro, undo the changes done while ...
user avatar
  • 3,119
10 votes

How to add permanent line numbers to a file?

Add numbers to all lines It's possible to use :%!nl -ba or :%!cat -n commands which will add line numbers to all the lines. On Windows, you've to have Cygwin/MSYS/SUA installed. Add numbers to ...
user avatar
  • 16.5k
9 votes
Accepted

How can I create a /* comments section with /// like in XCode?

The method in my earlier answer doesn't transform well to dynamic content. This is where snippet plugins like UltiSnips and SnipMate come in. I'll provide a demo of UltiSnips here. Install it using ...
user avatar
  • 23.7k
8 votes

How to generate random numbers?

This is a method found in the vim-randomtag plugin, which is based on reading ... current time microseconds, usable when you just want some number, you don't care much about randomness quality, or ...
user avatar
  • 9,250
8 votes

How to add permanent line numbers to a file?

I like to use the vim global command to accomplish tasks like this. This applies to adding iteration to the beginning of a line or modify a symbol in the text. It looks more complicated than the other ...
user avatar
  • 7,992
8 votes
Accepted

How to copy each line 11 times, incrementing the last "1" in each line from 2-12

Here's a substitution that solves the problem: :%s/\(.*\)1\(.*\)/\=join(map(range(1, 12), 'submatch(1) . v:val . submatch(2)'), "\n") The substitution matches each line that contains "1" and ...
user avatar
  • 861
7 votes

How can I generate a list of sequential numbers, one per line?

Here is a different approach, that needs a newer Vim (something like 7.4.800) This assumes an empty buffer and '1' in register a. First enter 100 1 into your buffer, "a100P. Then visually select ...
user avatar
7 votes

How to insert repetitive text with minor difference in vim?

You can use the following function: function! GenerateLines() let firstpart="similar text part 1" let secondpart="similar text part 2" let words=["wordA","wordB","wordC","wordD","wordE"] ...
user avatar
  • 45.9k
6 votes
Accepted

How do I automatically add a character at end of line?

As has been stated, your list of exceptions is by no means comprehensive and you will probably find that this often creates more problems than it solves. However, it's still doable and it was kind of ...
user avatar
  • 2,818
6 votes

How to generate random numbers?

You can use the rand() and srand() functions, provided your Vim binary includes the patch 8.1.2342. As an example, to generate 10 random numbers separated by newlines: let seed = srand() echo range(...
user avatar
  • 5,457
6 votes

How to copy each line 11 times, incrementing the last "1" in each line from 2-12

Two ways: Use a macro! Starting with 1stlineblahblahblah/nt/1blah 2ndlineblahblahblah/nt/1blah 3rdlineblahblahblah/nt/1blah With your cursor on the first line qqyyp$?\d<CR><Ctrl-a>q ...
user avatar
  • 1,470
6 votes

How can I create a /* comments section with /// like in XCode?

One way would be to create a file containing this snippet, and read it when you type ///. For example, create ~/.vim/snippets/my_header.snip containing this header. Then define this mapping: ...
user avatar
  • 23.7k
5 votes

How to copy each line 11 times, incrementing the last "1" in each line from 2-12

I find it easiest to use macros for one-shot tasks like that. Just start recording a macro with q + name of register and do the task once. I see one repeatable task here: Select the line yank paste (...
user avatar
  • 249
5 votes

How to add permanent line numbers to a file?

I1. <esc>^qqyWjP^<C-a>q This numbers the first two lines, and you can press @q to number subsequent lines (or type ex. 18@q if you want to number 20 lines total). Explanation: I1. <...
user avatar
  • 13.6k
5 votes

How to add permanent line numbers to a file?

A modification of romainl's answer: :%s/^\(\d\+\. \)\?/\=line('.').". " This will not just add line numbers, it will also replace exiting line numbers it they're already there. Of, if you've ...
user avatar
5 votes

How to insert repetitive text with minor difference in vim?

My strategy in such cases will vary, but it often follows the following steps: Write the unique word(s), e.g. wordA wordB ... wordK Either a) do a search and replace, e.g. :%s/\w\+/similar text \...
user avatar
5 votes
Accepted

Can I write Japanese (SKK) text in Vim?

OK, this took several days of struggle against the skk.vim plugin mentioned by @SatoKatsura. All the documentation is in Japanese therefore I still do not know of most of its features, and, certainly,...
user avatar
  • 1,596
5 votes

How to generate closing tags for HTML?

See vim-closetag It's exactly what you're looking for. From it's README: If this is the current content: <table| Now you press >, the content will be: <table>|</table> ...
user avatar
  • 671
5 votes

Copy text from one buffer to another using Vimscript

You can use appendbufline() like: call appendbufline("other.txt", "$", "Appended Line") or if you want to work with buffer numbers: call appendbufline(3, "$", "Appended Line") See :help ...
user avatar
  • 8,510
5 votes
Accepted

Create indented comment box vim

Well, "put text into a box" is a well-known Vim excercise. I'm not sure where I saw it for the first time, but the most elegant solution, IMO, is to use :h v_r So it becomes: nnoremap <leader>#...
user avatar
  • 15.4k
4 votes

How can I generate a list of sequential numbers, one per line?

I know this question is really old, but there's another way you can do it also. Try this: 99o<esc>:%s/^/\=line('.')<cr> 99o<esc> will just open up 100 blank lines. Then, we run a ...
user avatar
  • 16.5k
4 votes

How to add permanent line numbers to a file?

I think the chosen answer is the best, but in the sprit of variety, I'll offer an alternative using an external program: :%!cat -n This will filter your entire buffer (as denoted by %) through the ...
user avatar
  • 7,140

Only top scored, non community-wiki answers of a minimum length are eligible