VS code VIM extension copy and paste
Asked Answered
H

8

116

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?

Hyperkeratosis answered 9/10, 2019 at 14:4 Comment(2)
You can always copy to the clipboard with "+y And past from the clipboard with "+pCobalt
<C-v> in visual mode will conflict with vertical visual modeBrannon
M
271

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

Matterhorn answered 6/4, 2020 at 18:4 Comment(10)
This answer is better than rebinding <C-c> and <C-v> to false because it keeps the shortcut for visual block mode.Sterrett
Shortcut: Go to settings, search "vim clip", check the tick.Alanis
For some reason this didn't solve the problem in my case. However, the first answer solved it. Do you have any idea why?Sheepfold
@Sheepfold I am facing the similar issueTorquemada
@basilisk, @Torquemada one additional detail about this solution is that it's enabling the system clipboard to be available with the selection register (*). This allows you to paste via the native p command in normal mode (or Ctrl-r * in insert mode). It doesn't allow you to then use Ctrl-v as may be expected.Commeasure
Not being able to use Ctrl-C and Ctrl-V for copy-and-paste in VS Code was driving me nuts. Thanks for posting this!Adrenalin
@Sheepfold It didnt solve for me either!Dabchick
I find that yank reacts strangely after enabling the system clipboard - it solved the problem of not being able to yank in one vscode instance and paste in another (which is great) - but I feel the cost is too high... any other way global yank can be made possible (without using system clipboard) ?Mycobacterium
I don’t like this solution. Quite a lot of Vim commands write to the buffer, which under this option can lead to unexpected writes to the system clipboard. Vim’s buffer is not intended to map to the system clipboard, and many of Vim’s designs assume that the buffer is isolated, so it writes to the buffer in many places.Provitamin
@JasonLee please take a look at this answer in this tread https://mcmap.net/q/187504/-vs-code-vim-extension-copy-and-pasteMatterhorn
L
81

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"]
  }
]
Luiseluiza answered 9/10, 2019 at 15:49 Comment(6)
Do you know why C copy rest the line instead of changing it?Lovage
Yes! Thank you, this solves one of my problems that the yank y key yanks and the delete key d overwrites the register. I have one more issue though: After yanking I want to replace a 'word' with ci' but the ci' copies word into my clipboard 🤯. Do you know how to disable this functionality?Bakerman
I just answered my own question: { "before": ["c"], "after": ["\"", "_", "c"] }Bakerman
To save you some browsing if you're on OS X: the mapping "before": ["<C-v>"] becomes "before": ["<D-v>"] as Vim considers the command key as "D". Open vim and type :help <D- for more details.Hullo
Other useful shortcuts to be preserved from original VS Code would be ctrl+a; ctrl+s; ctrl+x; settings.json: "vim.handleKeys": { "<C-a>": false, "<C-s>": false, "<C-c>": false, "<C-v>": false, "<C-x>": false }Equilibrate
"This can be done by placing the below code in the extension's settings.json file:" Which, afaict, is also the main user settings file. Did I miss something? (Putting it in the user prefs file does seem to work, fwiw.)Rosewood
K
4

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

Kale answered 29/6, 2022 at 2:0 Comment(1)
brilliant, exact match for LunarVim behaviorEscritoire
T
2

You can also access system clipboard with vim

In INSERT mode hit CTRL+R then * or +

Touchhole answered 1/12, 2021 at 16:58 Comment(0)
A
2

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.

Aleen answered 18/3, 2022 at 15:20 Comment(0)
L
2

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"
  }
Lula answered 1/9, 2022 at 20:49 Comment(1)
Yes, but why don't people just use ctrl+v for pasting during vim insert mode? (*neo vim I had to add ctrl+v keybindings.json)Option
U
2

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

Ulema answered 15/12, 2022 at 0:50 Comment(0)
G
1

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

Gynandrous answered 12/5, 2020 at 1:45 Comment(1)
This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Further, it can help reduce the need for follow-up questions. Would you mind updating your comment with additional details? Take a look at the accepted answer for a good example.Shareeshareholder

© 2022 - 2024 — McMap. All rights reserved.