Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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: >&...
romainl's user avatar
  • 41.1k
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 ...
statox's user avatar
  • 50.1k
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"] ...
statox's user avatar
  • 50.1k
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(...
user938271's user avatar
  • 5,987
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>#...
Matt's user avatar
  • 20.8k
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 ...
Ralf's user avatar
  • 9,257
6 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> ...
Sheharyar's user avatar
  • 761
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 \...
Karl Yngve Lervåg's 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,...
grochmal's user avatar
  • 1,656
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 ...
DJMcMayhem's user avatar
  • 17.7k
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 ...
aignas's user avatar
  • 141
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 ...
D. Ben Knoble's user avatar
  • 26.4k
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 = ...
statox's user avatar
  • 50.1k
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 ...
DJMcMayhem's user avatar
  • 17.7k
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 '...
asoundmove's user avatar
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 ...
dedowsdi's user avatar
  • 6,268
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 ...
Rich's user avatar
  • 32.1k
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 '...
Christian Brabandt's user avatar
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. ...
Tumbler41's user avatar
  • 7,756
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 ...
x-yuri's user avatar
  • 359
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: <...
grochmal's user avatar
  • 1,656
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@:
Francis Niu's user avatar
2 votes

How to generate closing tags for HTML?

Disclaimer This is not directly what you want but I think it can be worth it to mention it here. If you are ready to use an additional plugin, you could try to use a snippet engine. For example, ...
statox's user avatar
  • 50.1k
2 votes
Accepted

VIM command to insert multiline text with argument

Define a list of all the lines you want to insert, and then insert them with either append() or setline(). You'll also need to extract the current indentation. See: :h List :h setline() :h append() :...
Luc Hermitte's user avatar
  • 17.4k
2 votes

In Vim ex mode how to re-use a string from the default buffer in the command

You can use registers (see :h reg) to store a word of a line. For you particular case, you would have to perform the following set of commands: :norm! gg"zyiw :norm! gg"_dd :%s/^/<C-r>z: / The ...
3N4N's user avatar
  • 5,764
2 votes
Accepted

How to add specific string if it is not exists in current file?

Something like this should work: function! AddPHPStrictTypes() " Save the cursor position let cursor_save = getpos('.') " Go to the begining of the file call cursor(1, 1) " Get ...
statox's user avatar
  • 50.1k
2 votes

VIM command to insert multiline text with argument

the system I am working on, its very very difficult if not impossible to get pluggins so I have to do stuff through my vimrc or other method. Basically trying to get a plugin installed could take half ...
filbranden's user avatar
  • 29k
2 votes

Increment the number at the end of variable

An alternative solution if you are using vim 8 using the g<c-A> command: paste the variable n times RxMsgType0 RxMsgType0 RxMsgType0 RxMsgType0 ... n times RxMsgType0 select all line wise e....
lissitz's user avatar
  • 121
1 vote

How to add specific string if it is not exists in current file?

I love the accepted solution. But for anyone else who wants to do this quickly, but not always automatically, here is another solution: iabbrev dst declare(strict_types=1); With this, in insert mode, ...
user49389's user avatar
1 vote
Accepted

Plugin to ignore quote when it's already there?

It's depends on the bracketing plugin you're using. AFAIK, the more popular ones have the feature. (For instance, it's the case of lh-brackets I'm maintaining) Of course, if you're using a mapping ...
Luc Hermitte's user avatar
  • 17.4k

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