13
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:
>&...
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 ...
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 ...
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 ...
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 ...
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"]
...
7
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(...
7
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>#...
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:
...
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
...
6
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 ...
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>
...
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,...
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 \...
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 (...
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 ...
4
votes
Can I write Japanese (SKK) text in Vim?
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 ...
4
votes
How to write 1 or 0(binary string) randomly in a file using vim?
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 = ...
4
votes
Accepted
Insert character until column number
Well, doing this in vimscript is not that hard. For example, the following command will do what you describe to the line your cursor is on:
:exec 'norm '.(&cc - strlen(getline('.'))).'A='
The ...
4
votes
In Vim ex mode how to re-use a string from the default buffer in the command
Using ex
Ex is the command-line successor to the venerable ed, a line-oriented editor.
You can access ex-mode from vim by typing Q (unless you have it mapped). gQ
gives an improved ex-mode. See :help ...
3
votes
Accepted
How to copy each line 19 times, incrementing two different numbers by 180?
You can record a macro, carry out your instructions, close the macro, then run it.
In your case you may start with the cursor at the beginning of the line to copy.
Press qa to start recording macro '...
3
votes
Add line at the end of text
Here is a different version. Call Vim from the directory with all files:
vim *.txt
Now you can do :bufdo call append('$', 'abc') | upd, which will run through your buffer list and add a single line '...
3
votes
Add line at the end of text
I would probably use the argument-list for this.
First you have to add all files to the argument list and then you can use argdo to operate on all of them.
So first let's populate the argument list. ...
3
votes
How can I create a /* comments section with /// like in XCode?
Write a function that returns the string and call it.
function! InsertHeader()
let l:header = "/*!\n"
\. "* @author Robbie Yi JIANG, " . strftime('%d-%b-%Y %H:%M:%S') . "\n"
...
3
votes
Can I write Japanese (SKK) text in Vim?
I'm trying to summarize here the experience of the other two users, while trying to keep it simple. Some issues were resolved (e.g. eskk now supports nvim). Also, I'm using pathogen which somewhat ...
3
votes
In Vim ex mode how to re-use a string from the default buffer in the command
ex -s +'norm! gg"adiwdd' +'exec printf("%%s/^/%s : /", @a)' +'x' ex_txt
norm! gg"adiwdd delete black in 1st line to register a, delete first line
exec printf("%%s/^/%s : /", @a) add content of ...
3
votes
Accepted
In Vim ex mode how to re-use a string from the default buffer in the command
@D.BenKnoble has already given you a terrific answer explaining all the ins and outs of batch mode, but in case you were looking for a one liner similar to the one you already tried, here's one that I ...
2
votes
How to insert repetitive text with minor difference in vim?
I'll argue that this is a job for the visual block (Ctrl+v). First I would write all distinct words:
wordA
wordB
wordC
wordD
wordE
Starting with the cursor on the first w I would then perform:
<...
2
votes
How to copy each line 11 times, incrementing the last "1" in each line from 2-12
Save the following macro and use it for each line:
qqyy11p/\/\zs1<CR><C-v>10jg<C-a>q
The main difference over the other answers is that this makes use of g<C-a> on a column ...
2
votes
How to generate random numbers?
Vim v8.1.2342 add rand and srand function.
I prefer this way to generate 20 random numbers in range 0-100 in current buffer:
:put =rand()%100
20@:
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
text-generation × 32vimscript × 5
macro × 4
functions × 3
enumerate × 3
gvim × 2
cut-copy-paste × 2
filetype-tex × 2
comments × 2
vimrc × 1
regular-expression × 1
buffers × 1
terminal × 1
autocmd × 1
autocompletion × 1
visual-mode × 1
cursor-motions × 1
ex-mode × 1
repeated-commands × 1
vimscript-python × 1
line-numbers × 1
unicode × 1
shell × 1
filetype-html × 1
abbreviations × 1