manish ma
manish ma

Reputation: 2028

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?

Upvotes: 129

Views: 81087

Answers (8)

Coder Gautam YT
Coder Gautam YT

Reputation: 2147

For mac users,

add this into the settings.json

"vim.handleKeys":{
  "<D-c>": false
}

Access VSCode settings.json file:

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

icon

Upvotes: 2

Carlos Vallejo
Carlos Vallejo

Reputation: 3980

If you use Linux (or a terminal itself) you must know that for copy and paste you add the shift key in the middle, that is:

ctrl + shift + c to copy

ctrl + shift + v to paste

Thus, for me is more simple to remember this, and add it to the configuration, because it helps me to see VS Code as a "terminal".

Steps:

  1. F1
  2. Preferences: Open Keyboard shortcuts (JSON)
  3. Add this
 {
    "key": "ctrl+shift+c",
    "command": "editor.action.clipboardCopyAction"
  },
  {
    "key": "ctrl+shift+v",
    "command": "editor.action.clipboardPasteAction"
  }

Upvotes: 2

Brampage
Brampage

Reputation: 6954

Vim - extension config flag

Tick the checkbox in settings by searching for "vim clip".

or

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

Upvotes: 298

user115625
user115625

Reputation: 198

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'

enter image description here

Upvotes: 5

Jaigene Kang
Jaigene Kang

Reputation: 164

I have found that one can use CTRL+INSERT / SHIFT+INSERT successfully with VS Code VIM to copy to/from the system clipboard without stumbling over the VIM buffers.

For context, I'm running VS Code on WSL2 on Windows.

Upvotes: 3

Highmastdon
Highmastdon

Reputation: 7520

You can also access system clipboard with vim

In INSERT mode hit CTRL+R then * or +

Upvotes: 2

ReversalMinutes
ReversalMinutes

Reputation: 131

Use vs code default copy, paste, delete line.

"vim.normalModeKeyBindingsNonRecursive": [
        {
            "before": ["d","d"],
            "commands":["editor.action.deleteLines"],
            "when":"textInputFocus && !editorReadonly"
        },
        {
            "before":["y"],
            "commands":["editor.action.clipboardCopyAction"],
            "when":"textInputFocus"
        },
        {
            "before":["y","y"],
            "commands":["editor.action.clipboardCopyAction"],
            "when":"textInputFocus"
        },
        {
            "before":["p"],
            "commands":["editor.action.clipboardPasteAction"],
            "when":"textInputFocus && !editorReadonly"
        }
    ],
    "vim.visualModeKeyBindingsNonRecursive":[
        {
            "before":["y"],
            "commands":["editor.action.clipboardCopyAction"],
            "when":"textInputFocus"
        },
        {
            "before":["y","y"],
            "commands":["editor.action.clipboardCopyAction"],
            "when":"textInputFocus"
        },
        {
            "before":["x"],
            "commands":["deleteRight"],
            "when":"textInputFocus"
        },
    ]

https://github.com/VSCodeVim/Vim/#key-remapping https://code.visualstudio.com/docs/getstarted/keybindings

Upvotes: 1

Das_Geek
Das_Geek

Reputation: 2845

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"]
  }
]

Upvotes: 82

Related Questions