Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to insert N times the same characters

I can't find if vscode has a such failure. Is there a way to construct a string with N characters? I explain myselft: I need to wrote an empty string like this:

foobar = "1111111111111111";

There is 16 times characters '1'. Is there a way, like in Vim, to construct the line like this: i wrote 'foobar = "' then i'd make a command to repeat 16 times the character 'i'.

I hope it's clear for you.

like image 738
beware Avatar asked Oct 31 '25 07:10

beware


1 Answers

Here is a simpler way to repeat a sequence of commands without having to learn the HyperSnips syntax I mentioned below.

Using an extension I wrote, Repeat Commands, make this keybinding (in your keybindings.json):

{
  "key": "alt+r",
  "command": "repeat-commands.runSequence",
  "args": {
    
    "preCommands": [
      "cursorColumnSelectLeft"  // select the previous character
    ],
    
    "commands": [
      "editor.action.duplicateSelection"  // this command will be repeated
    ],
    // "repeat": 5,                 // hard-code a number if you wish
    "repeat": "${getRepeatInput}",  // ask the user for the number to repeat
  },
  // "when": "editorLangId === perl"   // if you want to limit it to certain files
},

repeat previous character demo using extension

You first type the character you want to repeat and then trigger the keybinding.

You can run any number of preCommands that will not be repeated. Then your desired sequence of commands that actaully are repeated. So this extension can be pretty powerful.



Previous answer:

Here is an easy way using HyperSnips - a snippet extension that can use javascript to produce the output. First the demo:

repeater demo using HyperSnips

The HyperSnips snippet:

snippet `"(.+)\*(\d+)=` "expand" A
``
let origStr = m[1];
let howMany = parseInt(m[2]);
let newStr = origStr.repeat(howMany);
rv=`"${newStr}`
``
endsnippet

This code goes inside a <yourLanguage>.hsnips file to have it run only in that language or all.hsnips to run in all files.

I made it to run inside a "" using this key: (.+)\*(\d+)=

The = is really the trigger - it runs automatically - you could change that to be something else. [The key could be shorter if you didn't care about digits being repeated.]

For more on setting up HyperSnips (which is pretty easy) see LaTeX fraction snippets in VSCode

like image 155
Mark Avatar answered Nov 02 '25 23:11

Mark