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

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.

Related

How to highlight multiple words on VIM and modify them all? Like CTRL+D does on VS-Code?

On VSCode, if you highlight a text, you can do "CTRL+d" and it will highlight the next matching text and add a cursor there.
You can then start typing/deleting and it will affect all the cursors.
How can I do this in VIM?
Note: I know the search and replace function, this is too slow to type, is there something as easy or almost as easy as it is on VSCode?
:%s/foo/bar
Visual explanation on VSCode:
Highlight the word you're looking to replace:
CTRL+d two times, which highlights them all:
Now modify them all at the same time:
I want this will help you.
You can do the below steps to change all selected words.
< SHIFT > + # // select the all words on your cursor.
:%s//NEW_WORD/g
If you want to change all 'const' to 'AAA',
move to one of the 'const' words and press < SHIFT >+'#'.
And type the command ":%s//AAA/g".
Move the cursor on top of a word in your code.
Type gb to add another cursor. This puts Vim into Visual
mode and ready to operate on the word you have selected.
Type gb to continue adding cursors until you’re done.
Now you can perform an action in Visual mode (delete,
change, etc).
Go back to Normal mode with <ESC>

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.

neovim vscode replace word under cursor

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

Delete whitespace in Visual Studio Code in arbitrary selection (aka join words) (not trim trailing whitespace)

It seems like this should be a simple question, but I can't figure out how to delete all the whitespace within an arbitrary selection in Visual Studio Code (another way of saying this is to join all characters within a selection).
Note: I'm not asking how to trim trailing whitespace although this function could be used to manually to that.
It seems like there should be a built in way to do this, but if there isn't can someone point me to an extension that will do this?
I wasn't able to find one yet.
You can do it fairly easily without an extension.
Copy the selection
Ctrl + N (opens new unnamed blank document)
Paste
Ctrl + H (replace dialog), space, Ctrl + Alt + Enter (replace-all)
Now copy the update string to where you want it, and close the document.
Here's an extension (remove-whitespace-aka-join-words) I built to do just this:
https://marketplace.visualstudio.com/items?itemName=tnrich.remove-whitespace-aka-join-words
Just adding this here for 2022. I use one of these two methods. The first I don't have to remember the Regular Expression formula:
Method 1:
Sometimes this method will strip all spaces in your code (which isn't intended). If that occurs, try the next method. I often try this method first.
Select an empty space, and push CTRL + F, or Apple key + F on mac. This will highlight all matching characters, spaces.
Push the Replace All button to replace them all with nothing.
Hit CTRL + SHIFT + P' or Apple key + Shift + P', and select Format Document.
Method 2:
Found here: https://www.trainingdragon.co.uk/blog/how-to-remove-empty-lines-in-visual-studio-code/
Push CTRL + F, or Apple key + F on mac. Select the Use Regular Expression button, or push Apple key + option + R to toggle it on/off.
Add ^(\s)*$\n to the find input box.
Push the Replace All button to replace them all.
Depending on your selection you might be able by simply
Select the first space
Ctrl+D to select the next space, and repeat for all your spaces
Delete
Here is a technique that could be of more general use. Make a keybinding like so:
{
"key": "shift+alt+y", // or whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/\\s//g}" // replace spaces with nothing
},
"when": "editorTextFocus && editorHasSelection"
},
Then select your text and trigger this command. [Or make it a snippet and trigger it through the command palette Insert Snippet command.]
This could be modified to use for many things very easily - in part because often the regex will be quite simple as it will operate within already selected text.
You can also just do a Find/Replace easily:
Find: just a space
Replace: with nothing
Make your selection
Enable the Find in Selection option - the "hamburger-like icon" to the right

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.