Search code examples
vimvisual-studio-codevscodevim

VSCodeVim - How to bind text inserts properly?


I'm trying to figure out how to create text inserts in VSCodeVim like:

inoremap <leader>sys <esc>ISystem.out.println(<esc>A);
vnoremap <leader>sys yOSystem.out.println(<esc>pA);

but the only thing I could come up with is some hacky:

"vim.insertModeKeyBindingsNonRecursive": [
    {
        // Console.WriteLine
        "before": ["<leader>", "c", "w", "l"],
        "after": ["<Esc>","I","C","o","n","s","o","l","e",".","W","r","i","t","e","L","i","n","e","(","<Esc>","A",")",";"]
    }

It does operate nicely. Because there are so many separate keys, the delay creates an animation that is very pleasing XD The problem is writing them. I wrote one for For loops as well and it's not a good experience.

There has to be a better and simpler way of doing this! If I have more than one character inside quotes somewhere, it stops the sequence.

Thanks!


Solution

  • You can do it using the command editor.action.insertSnippet.

    {
      "vim.insertModeKeyBindingsNonRecursive": [
        {
          "before": ["<leader>", "c", "w", "l"],
          "commands": [
            {
              "command": "editor.action.insertSnippet",
              "args": { "snippet": "Console.WriteLine();" }
            }
          ]
        }
      ]
    }