53

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)


40

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 an expression line('.').". " " the expression returns the current line number concatenated with a dot and a space See :help \= and :help line(). Using an ...


21

There is no built-in facility for this, so you'll need to use something external. UNIX Shell (/bin/sh) Calling: strings -n 1 < /dev/urandom | tr -d '[:space:]' | head -c15 with system() is a good way. You can get only numbers by replacing tr with grep: strings -n 1 < /dev/urandom | grep -o '[[:digit:]]' | head -c15 You can use this in Vim like so:...


18

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 The above works by piping the entire (empty) buffer to seq, which ignores its input and just outputs the numbers 1 to 100. Vim then replaces the entire buffer ...


17

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> -- insert the number 1, then get back out to command mode qa -- start recording a macro in register "a" Y -- copy the current line p -- paste the current line (...


13

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 generate a pseudo-random number based on calculations and permutations applied to the seeds: " Randomization Variables " with a little extra randomized start ...


13

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: ></ insert '></' <C-x><C-o><C-y> use the built-in omni-completion to finish the closing tag <C-o>% ...


10

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 recording, or there will be n + 1 additional lines for the first line, and n for consecutive lines. Record the macro to the a register with qayyp$?\d<CR><...


9

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 of line and start recording a macro: 0qq Copy the number: yW Move down a line and paste the number: +P Move back to the start of the line and increment the ...


9

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 have security concerns etc.: function! s:randnum(max) abort return str2nr(matchstr(reltimestr(reltime()), '\v\.@<=\d+')[1:]) % a:max endfunction


9

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 your favourite method from How do I install a plugin in vim/vi? Now, make an UltiSnips directory in your .vim or _vimfiles directory. In it, place a c.snippet ...


8

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 captures the text before {c1} and after {c2} the last "1". For each matched line, the range of numbers from one to twelve {n} are mapped to create twelve lines of the ...


7

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 selected lines To add numbers only for selected lines, please select them in visual mode (v and cursors), then when finished - execute the command: :%!nl (ignore ...


7

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 solutions, but is a pretty flexible pattern to use when you have it handy, and is easy to modify without a lot of thought. First, pick your range (which lines ...


7

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"] for word in words let line = firstpart . word . secondpart call append(line('.')+1, line) normal j endfor endfunction In the ...


6

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 fun to write out the code to do so, so here goes: "When hitting Enter, if the line doesn't start with a /*, insert a semicolon :inoremap <CR> <C-R>...


6

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 10@q Which does: qq Start recording a macro into the q register yyp yank the current line, and paste it below $?\d<CR> Go to the end of the line, and ...


6

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: inoremap /// <esc>:r ~/.vim/snippets/my_header.snip<cr>i Or: inoremap /// <esc>:call append(line('.')-1, readfile(expand('~/.vim/snippets/my_header....


5

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 (will put you in the next line) go to end of line go back one word increment by one Now record the required keystrokes to register a, in normal mode: qayyp$b&...


5

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 lines 2 till 100 :2EnterVG. Now press gCtrl+A. Read the help at :h v_g_CTRL-A


5

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. <esc> Number the first line hqq Go back to the start of the line and start recording a macro yWjP Copy the line number to the next line ^<C-a&...


5

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 inserted a line mid-way, it will renumber everything as expected. This works by replacing any number followed by a . and a space at the start of the line with a new ...


5

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 \0 similar text .../ b) or use visual block mode, see :h blockwise-visual. In particular, I woul select the words and use I to insert similar text in front, or ...


5

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, there are better ways to accomplish SKK conversion ("Henkan" in Japanese) in Vim than the one presented here. Yet, it works well enough to be comparable to ...


5

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># I#<Space><Esc>A<Space>#<Esc>yy2P<C-V>$r#2j. I#<Space><Esc> --- add "# " at the start of line A<Space>#<...


4

Vim doesn't offer native random generator, however if you have vim compiled with Python, the following method will append a random digit at the end of your line: :py import vim, random; vim.current.line += str(random.randint(0, 9)) Note: To check if your vim supports Python, try: :echo has('python') (1 for yes). You can also use shell which offers $RANDOM ...


4

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(10)->map({-> rand(g:seed)})->join("\n") To generate 10 random numbers all inferior to 100: let seed = srand() echo range(10)->map({-> rand(g:...


4

Since I was in search for a good way to input unicode Japanese characters in VIM, this post was really helpful. I tried using skk.vim but found it really difficult, because contrary to the OP, I couldn't understand the documentation at all, because I have just started learning Japanese. However, I managed to find an alternative plugin, which seems to be a ...


4

To use a purely vimscript solution you can use some functions I described in another answer of mine: " Randomization Variables " with a little extra randomized start from localtime() let g:rndm_m1 = 32007779 + (localtime()%100 - 50) let g:rndm_m2 = 23717810 + (localtime()/86400)%100 let g:rndm_m3 = 52636370 + (localtime()/3600)%100 function! Rndm() let ...


4

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 appendbufline() for details.


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