简体   繁体   中英

Insert same text multiple times on one line

I often have to insert sequences like

:1, :2, :3, :4, :5

into a plain text file. That's easy to do with 5 items like in my example but it becomes tedious if I have something like 42 items. Is there a possibility that I only type the colon and have it repeated a number of times on the same line?

I can do the numbering using an extension so it would be nice if multiple cursors would be placed after the inserted colons immediately.

See Is there a way to insert N times the same characters for some options on inserting repeated text.

Using the HyperSnips approach this snippet in your plaintext.hsnips file will do what you want:

snippet `:\*(\d+)=` "expand" A
``
let howMany = parseInt(m[1]);
let start = 1;
let newStr = '';
for (let index = 0; index < howMany; index++) {
    newStr += `:${start++}, `;
}
rv=`${newStr.slice(0,-2)}`
``
endsnippet

重复片段的演示

Since it is just javascript, you can make that snippet as generic as you want.

You can use the extension Regex Text Generator . You write a Regular Expression that generates your needed text

Type the following in the Generate Box

(:{{=j[0]+1}}, ){10}

You are left with the , at the end

To place the multiple cursors:

  1. Select the generated text
  2. Open the find dialog: Ctrl + F
  3. Enable Find in Selection : Alt + L
  4. Search for :
  5. Add cursors on all selections: Alt + Enter
  6. Move cursor after selection: ArrowRight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM