Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS code VIM extension copy and paste

Is there a normal way to copy and paste in vs code using vim extension?

I've tried mapping VIM register commands to the shortcut commands I'm used to (ctrl + c for copying and ctrl + v for pasting), but the results are pretty weird and I'm not sure how to do this correctly.

While using vim the key bindings were quite simple, vimrc file:

map <C-c> "+y map <C-v> "+p 

Now I try to migrate those to vs-code by editting json.settings file:

{     "vim.visualModeKeyBindings": [         {             "before": ["<C-c>"],             "after": ["\"", "+", "y"]         },         {             "before": ["<C-v>"],              "after":  ["\"", "+", "p"]         },     ], } 

I want this to operate both in visual mode and in normal mode (for pasting), and be able to copy and paste from clipboard using these shortcuts.

How to do this correctly? Is there another way to do this?

like image 508
manish ma Avatar asked Oct 09 '19 14:10

manish ma


People also ask

Is it possible to copy and paste text into VSCode using Vim?

Note that you can yank text to the clipboard of your OS and also paste from it into VSCode using Vim. Tried with: Vim extension for Visual Studio Code 0.11.5 and Visual Studio Code 1.22.2

How do I enable copy and paste in VS Code?

In the latest version of VS code (on Linux, flatpak version, 1.68.1) and vim addon (at the time of writing), this can be easily enabled by ticking the " Vim: Use System Clipboard ". Note: You can open settings by Ctrl+, then search for 'vim clipboard' Use vs code default copy, paste, delete line.

How to yank to and paste from the system clipboard in Vim?

Visual Studio Code has a decent Vim extension. I use it by default and sometimes wish I could yank to and paste from the system clipboard. Thankfully, this feature is available in the Vim extension, but turned off by default. To turn on this feature, set the vim.useSystemClipboard setting to true.

What is vscodevim?

VSCodeVim is a Vim emulator for Visual Studio Code. For a full list of supported Vim features, please refer to our roadmap. Our change log outlines the breaking/major/minor updates between releases. Report missing features/bugs on GitHub. VSCodeVim is automatically enabled following installation and reloading of VS Code.


2 Answers

Vim - extension config flag

Paste the following inside your VS Code's settings.json file:

"vim.useSystemClipboard": true 

Access VSCode settings.json file:

  1. Press Ctrl + , (or go to File > Preferences > Settings)
  2. Click the icon: "file with arrow" in the top right corner

VSCode access settings json file


Settings found in VSCodeVim/Vim repository quick-example

like image 133
Brampage Avatar answered Oct 08 '22 19:10

Brampage


Rather than rebinding, you can simply stop the vscodevim extension from handling Ctrl-C and Ctrl-V entirely, which then allows VSCode to handle them natively. This can be done by placing the below code in the extension's settings.json file:

"vim.handleKeys": {     "<C-c>": false,     "<C-v>": false } 

This will work regardless of which mode you're in, and will perfectly accommodate the system clipboard. I'm not sure if the <C-c> is necessary, but the <C-v> definitely is, as <C-v> is the standard Vim chord to enter visual block mode.

As an aside, your rebind method is perfectly valid; it just requires a bit more code:

// For visual mode "vim.visualModeKeyBindings": [   {     "before": ["<C-c>"],     "after": ["\"", "+", "y"]   },   {     "before": ["<C-v>"],      "after":  ["\"", "+", "p"]   } ], // For normal mode "vim.normalModeKeyBindings": [   {     "before": ["<C-c>"],     "after": ["\"", "+", "y"]   },   {     "before": ["<C-v>"],      "after":  ["\"", "+", "p"]   } ] 
like image 30
Das_Geek Avatar answered Oct 08 '22 19:10

Das_Geek