0

With the VSCodeVim Extension, I'd like to map a keybinding (leader + "p") to a substitute command that wraps the text on the current line with "print(TEXT)", so that e.g.,

f'The value of `x` is {x}.' 

Becomes

print(f'The value of `x` is {x}.')

But I'm having trouble defining that key binding in VS Code settings.json. Although the following works as expected, moving the cursor to the top of the file:

"vim.normalModeKeyBindings": [
    {
      "before": ["<leader>", "g"],
      "commands": [":1"]
    }
]

Even this simple substitute command does not work:

"vim.normalModeKeyBindings": [
    {
      "before": ["<leader>", "p"],
      "commands": [":s/a/A/"]
    }
]

Instead, the text :s/a/A/ is entered in the command-mode prompt (I can see it!), but the buffer is functionally back in normal mode, so I can't submit the command. If I re-enter command-mode, the substitute command disappears.

For now I have this inane solution:

"vim.normalModeKeyBindings": [
    {
      "before": ["<leader>", "p"],
      "after": [":", "s", "/", "^", "/", "p", "r", "i", "n", "t", "(", "/", "<Cr>", ":", "s", "/", "$", "/", ")", "/", "<Cr>"]
    }
]

But it's ugly and tedious to code, so I'd like to find a better solution for other implementations of similar keybindings with substitute.

Bonus: If you can help me code a keybinding(s) that will effectively toggle the print statement in and out, that would be amazing. So another submission of (leader + "p") would bring us back to

f'The value of `x` is {x}.'

A hack might be to map (leader + "P") to delete the first 6 and final character of the line.

romainl
  • 186,200
  • 21
  • 280
  • 313
tef2128
  • 740
  • 1
  • 8
  • 19

0 Answers0