VSCodeVim Extension substitute keybindings - visual-studio-code

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.

Related

How to map "Go To Implementation" using VsCode Vim?

I want to map something like gi to open a Typescript interface implementation for example. In VsCode the shortcut is Ctrl + F12 and I tried to add a map like this inoremap gi <C-F12> in .vimrc file but that does not work.
I also tried to do that in my settings.json but still no results.
"vim.insertModeKeyBindings": [
{
"before": ["g", "I"],
"after": ["<C-F12>"]
}
]
How can I do that?
You can achieve that by mapping a shortcut to a VSCode command, not a set of keys. If you look at VSCode shortcuts, you can see that the Go To Definition action is related to a command so you can take that command and map to a vim shortcut in your settings.json file.
In the example I will map gI to editor.action.goToImplementation which is the related command.
"vim.insertModeKeyBindings": [
{
"before": ["g", "I"],
"commands": ["editor.action.goToImplementation"]
}
],
"vim.normalModeKeyBindings": [
{
"before": ["g", "I"],
"commands": ["editor.action.goToImplementation"]
}
]

How to make vim extension in visual code studio to use binding that repeats change for next match

How can I make vim extension in VSC make to use my keybinding?
In my .vimrc I got those lines:
" s* - CRTL + D like behavior
nnoremap <silent> s* :let #/='\<'.expand('<cword>').'\>'<CR>cgn
xnoremap <silent> s* "sy:let #/=#s<CR>cgn
How could I add them to my VSC?
Tried to add to my settings.json something like below, but got no idea how to add it properly every time I got an error:
Failed to handle key `*`: command '"sy:let #/=#s<CR>cgn' not found
"vim.normalModeKeyBindings": [
{
"before": [
"s",
"*"
],
"commands": [
":nohl:let #/='\\<'.expand('<cword>').'\\>'<CR>cgn",
],
"silent": true
},
{
"before": [
"s",
"*"
],
"commands": [
"\"sy:let #/=#s<CR>cgn",
],
"silent": true
},
]

How do I rebind Y to yank to the end of the line when using VS Code's Vim extension?

I'm using VS Code's neovim integration with neovim installed from snap. I want Y to work the same way as D and C. In ~/.config/nvim/init.vim I would add
map Y y$
How do I do this in VS Code? I've tried
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["Y"],
"after": ["y$"]
},
],
and
"vim.normalModeKeyBindings": [
{
"before": ["Y"],
"after": ["y$"]
}
],
but neither worked.
Turns out you need to specify and separate each key into an individual "", so that would be:
"vim.normalModeKeyBindings": [
{
"before": ["Y"],
"after": ["y", "$"]
}
]

Is there a command to enter insert mode / normal mode or other mode in vscodevim?

I'm trying to create a very simple snippet in VSCode and I'm using vim extension.
I succeed to enter insertMode after inserting my snippet. I'm not able to write anything after that.
This is my python.json file:
"Print": {
"prefix": "print",
"body": [
"print($1)$0"
],
"description": "Print statement"
}
This is my settings.json:
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["<leader>", "i", "p"],
"commands": [
{
"command": "editor.action.insertSnippet",
"args": {
"langId": "python",
"name": "Print"
}
},
{
"command": // COMMAND to enter insert mode here
},
{
"command": // COMMAND to type something (eg: `type` but I'm not sure I can use it with vim)
},
{
"command": // COMMAND to quit insert mode here
}
],
}
],
I know I could write arguments to the snippet but it's more for the sake of learning how to play with vim extension as I'd like to create another extension that uses vim one.
This is the end of my first question but I've got a second one:
When I use this keybinding, I want the code to be indented.
For example ("|" represents cursor position):
def my_function():
a = "super string"
|
Applying shortcut
This is what I want:
def my_function():
a = "super string"
print(|)
This is what I get:
def my_function():
a = "super string"
print(|)
Q1:
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["<leader>", "i", "p"],
"commands": [
{
"command": "editor.action.insertSnippet",
"args": {
"langId": "python",
"name": "Print"
}
},
],
"after": ["i","t","y","p","e","<ESC>"]
}
],
There is also a command extension.vim_insert might be useful.
Typing out words like this "t","y","p","e" is probably bad practice, just make custom vscode snippets instead.
Q2:
The correct method is to keep the cursor on line 2 and hit the o key, auto-indenting to where you want to be. Rarely will you be in a position where the cursor is on the first character of line 3.
Places where the cursor is likely going to be.
def my_function():
|a = "|super |string"|
After o.
def my_function():
a = "super string"
|

How to delete to blackhole register in VScode's Vim mode?

I really want to use Visual Code and it looks like it has a great Vim mode. Unfortunately I'm one of those people that changed my .vimrc so that deletes, changes and the like would move to the blackhole register so that it wouldn't take over my last yank.
Does anyone know of a way to do something similar for dd or C mappings? I've tried it various ways but can't seem to get it to work.
It seems to only work with a leader like so
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["<leader>","d"],
"after": ["\"", "_", "d", "d"]
},
],
"vim.leader": "<space>",
Doing the following though doesn't work. Any ideas?
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["d","d"],
"after": ["\"", "_", "d", "d"]
},
],
Black hole register mapping functionality has been fixed in the recent PR, just a few months after you posted this question. Update your VScode to the latest stable release and your mappings should work fine.
If you are interested in how to disable cut functionality for d command, here is how to do it. Put this in your settings.json:
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["d"],
"after": [ "\"", "_", "d" ]
}
]
Hope this helps