neovim vscode replace word under cursor - visual-studio-code

I am using neo vim extension in vscode.
I want to replace the word under cursor. Usually it is just a minor change to the existing word, so I have it available in the replace part. I have the below line in my init.vim
nnoremap ^ :%s/\<<C-r><C-w>\>/<C-r><C-w>/g
When I invoke the binding with ^, the appropriate command appears as shown in image below.
However on pressing return no changes take effect. If I place the same command in the command window by pasting or typing and hit return, then it works.

This mapping didn't work for me either, but I've found a workaround:
nnoremap <leader>sr :<C-u>call VSCodeNotify('actions.find', { 'query': expand('<cword>')})<CR>
So ,sr opens the quick find window with the word under cursor. Then you have to push Ctrl-h to get the replace field also.
Unfortunately the same with editor.action.startFindReplaceAction doesn't work.
The same works for workbench.action.findInFiles but not for workbench.action.ReplaceInFiles

{
"before": ["<Space>","s","r"],
"after": ["*",":", "%","s","/","/"]
},
this works for me using regular vim in VSCode

Related

Is there a particular key to enter one character and exit insert mode in vim vscode extension?

I find myself adding a single letter during edits. Is there any way to add a character and exit insert mode just like replacing r but not deleting the character in Vs code vim extension?
pressing i <character> <Esc> each and every time defeats the purpose of being productive.
I have also looked for mapping and tried but not sure how they work.
Any help is appreciated.
From normal mode, you can put yourself in insert mode, type a space, exit back to normal mode, then put yourself in replace mode. That way you find your cursor on a blank space in replace mode, where you can simply type the character you want to add.
Here's how to do it (replace "your-desired-mapping-here" by the mapping that you like, for example: M)
vscode vim plugin way: (add this to your settings.json)
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": ["<your-desired-mapping-here>"],
"after": ["i", " ", "<ESC>", "r"]
}
]
vim way: (add this to your .vimrc)
nnoremap <your-desired-mapping-here> i <esc>r
note: please notice the space after i in the vim way.

disable vscode change tab on 'gt'

I use VIM extension in VSCode. One of my favourite commands is to use gt<character> (ie: 'gtc' to jump to the nearest 'c' character) to jump to a specific character. In a recent update, when I press "gt", VSCode changes tab rather than allowing me to complete my vim command. Does anyone know how to disable this behaviour? Is it coming from VSCode or the VIM extension?
In vim gt is used to go to next tab and gT is used to go to previous tab. As vscode vim implements the functionality of vim, you can expect the same behaviour in Vscode if you are using the the vim plugin.
The feature you are looking for can be accomplished by f i.e. to go to next c character press fc.
You can use ; to cycle forward in the direction of the search and , to cyle opposite to direction of the search.
For example, ; goes to next occurrence of same character in the line and , goes to previous occurrence.
Then there is F to go to previous occurrence of a character. Here the direction of search is backwards, therefore ; goes to previous occurrence of same character in the line and , goes to next occurrence.
If you're using VSCodeVim, their GitHub page has examples for things like this. Simply edit your settings.json to remap "g,t" to "f" in normal mode:
"vim.normalModeKeyBindingsNonRecursive": [
{ "before": ["g", "t"], "after": ["f"], },
]
You may or may not want to do the same for "vim.visualModeKeyBindings" if you use it while making selections as well.

How to select whole line in VSCode

I am using VSCode on a Mac.
Does anyone know how to select the entire line that the cursor is on? I know about Command+I, but that only selects what appears to be the whole line, which is not always the whole line if I have word wrap enabled.
I am looking for something like Sublime Text's "Expand Selection to Line" command.
All you need to do is put the cursor anywhere on the line, do not make any selection at all and then do the desired command (Cut, copy, or paste).
When no text selected, VS Code will automatically select the entire line.
just triple click the end of the line it will select the entire line
Triple click at any point on the line
Click once on number of the line
Press Command + L
An alternative to what people have posted is, when your cursor is at the start/end of the line, you can hit shift + end/home respectively.
I find this useful for wrapping a line in curly braces/quotes/etc. whereas the other answers include spaces in the select so whatever you're wrapping it in will be wrapped around that whitespace.
Install the MetaGo extension and use the "metaGo: selectLineDown" command, which will come installed already overriding the "expandLineSelection" command.
This extension has many additional commands that you'll likely find useful as well, including moving up/down over code blocks, centering the active line, and going to any character on the screen.
Now, when I press Command+I, the whole line is selected. I am guessing this was caused by an update to VS Code, but I am not sure.
Ctrl + L on Windows or Command + L on Mac to select the whole line in VS Code.
You can use your mouse to select the whole line by triple-clicking on the line but the better way is to click on the line number to select the whole line or multiple lines.
Tripple click at any point on the line
In case you're wondering why Cmd+L is not working, there might be a chance that there are duplicate shortcuts. You can find out by opening Keyboard Shortcuts in VSC and remove the one that's not needed.
I know its old but for anyone seeking, you can press Alt + arrow up/down to duplicate your cursor to other lines and then without selecting anything copy and paste multiple lines.

Delete all words in the current line and leave it a blank line in vscode

In VSCode, we can use a shortcut to delete the current line. But sometimes I want to rewrite the current line, so I don't want to delete the line when we are deleting all the words. Is there any way to delete all words but don't delete the current line?
There is a useful command deleteAllLeft and deleteAllRight which are unbound by default. So try in your keybindings.json:
{
"key": "ctrl+shift+backspace",
"command": "deleteAllLeft"
}
It will delete any indentation on the left as well - until your formatter kicks in however you have it set up.
I looked through Keyboard Shortcuts of VS Code, it doesn't have such thing as deleting contents of a line and leaving the empty line.
You can use one of the following combinations:
Home then Shift+End (or End then Shift+Home) to select the contents, then Delete or start typing (as you want to rewrite the line)
Ctrl+Shift+K to delete line, then Ctrl+Shift+Enter to insert new line above. This will move the cursor to the empty line so you can start typing the new content.
The simplest way is Shift +Del ,
but delete the current line
You can use Ctrl+x and Ctrl+Shift+Enter.

Is it possible to auto-indent only the current line on Visual Studio Code?

On Atom (and many other editors), there is the auto-indent command which allows us to auto-indent the line the cursor is on. Is there an equivalent in Visual Studio Code ?
I know there is the formatter action on Visual Studio Code but from what i have seen, it can be used only to :
format a selection (ctrl-K ctrl-F)
format the hole document (ctrl + shift + I)
I would like to be able to format the line the cursor is on without reformating the whole document and without having to make a selection.
Basically, i would like to configure the [TAB] key so that when i press [TAB], it auto-indents only the line the cursor is on :
if there is nothing written on the line, it just put the cursor at the right place so that when i start writting, the code is correctly indented.
if there is already something written on the line, it audo-indents the line
Is it possible ?
So I have skimmed through the source code and seems there is no setting currently available to make this happen. There is a lot of work happening in pipeline for indentation
https://github.com/Microsoft/vscode/issues/17868
VSCode use Monaco Editor under the hood
https://github.com/Microsoft/monaco-editor/issues/612
The current python configs are located in
https://github.com/Microsoft/vscode/tree/master/extensions/python
I tried, but understanding how all this integrates and works together just to fix one indent issue was just overwhelming. So I would just for the time being open a enhancement request with VScode and let the experts take a call and do the job
Allow me to humbly suggest that you are looking for the solution in the wrong place.
I would suggest the following setting:
"editor.formatOnType": true,
You have focused on "tab" doing the correct indentation. But with this setting you need not press the tab key at all. Just type the line with a normal return at the end. Visual Studio Code will then indent (and format) that line correctly.
If you install the extension emacs-tab, you can do this:
{
"key": "tab",
"command": "emacs-tab.reindentCurrentLine",
"when": "editorTextFocus"
}
Which, as far as I understand you, does exactly what you want (and doesn't format the line in other ways, such as breaking it if it is too long, and so on).
This extension worked for me, and allows typing Tab with the cursor mid-line to get proper indentation similar to what I was used to on Emacs.
Note that it basically does the same as the extension recommended in this answer but at the time of this writing that extension appears to be unmaintained and has some open issues.