Override VSCode copy command. #1337, #616.

This commit is contained in:
johnfn 2017-02-28 08:22:35 +08:00
parent 1db7fc80d2
commit a0a64232fc
5 changed files with 72 additions and 0 deletions

View File

@ -159,6 +159,11 @@ Or bind `<leader>w` to save the current file:
* Have VSCodeVim start in Insert Mode rather than Normal Mode.
* We would be remiss in our duties as Vim users not to say that you should really be staying in Normal mode as much as you can, but hey, who are we to stop you?
### overrideCopy
* Override VSCode's copy command with our own, which works correctly with VSCodeVim.
* If cmd-c or ctrl-c is giving you issues, set this to false and complain at https://github.com/Microsoft/vscode/issues/217.
* Type: Boolean (Default: `true`)
#### useSystemClipboard
* Enable yanking to the system clipboard by default
* Type: Boolean (Default: `false`)

View File

@ -186,6 +186,14 @@
"command": "extension.vim_ctrl+c",
"when": "editorTextFocus && vim.useCtrlKeys && !inDebugRepl"
},
{
"key": "ctrl+c",
"win": "ctrl+c",
"linux": "ctrl+c",
"mac": "cmd+c",
"command": "extension.vim_cmd+c",
"when": "editorTextFocus && !vim.useCtrlKeys && vim.overrideCopy && && !inDebugRepl"
},
{
"key": "ctrl+a",
"command": "extension.vim_ctrl+a",
@ -284,6 +292,11 @@
"description": "Use system clipboard for unnamed register.",
"default": false
},
"vim.overrideCopy": {
"type": "boolean",
"description": "Override VSCode's copy command with our own copy command, which works better with VSCodeVim. Turn this off if copying is not working.",
"default": true
},
"vim.insertModeKeyBindings": {
"type": "array",
"description": "Remapped keys in insert mode. Allows mapping to vim commands or vscode actions. See README for more."

View File

@ -1542,6 +1542,54 @@ class CommandCmdVInSearchMode extends BaseCommand {
}
}
/**
* Our Vim implementation selects up to but not including the final character
* of a visual selection, instead opting to render a block cursor on the final
* character. This works for everything except VSCode's native copy command,
* which loses the final character because it's not selected. We override that
* copy command here by default to include the final character.
*/
@RegisterAction
class CommandOverrideCopy extends BaseCommand {
modes = [ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["<D-c>"];
runsOnceForEveryCursor() { return false; }
public async exec(position: Position, vimState: VimState): Promise<VimState> {
let text = "";
if (vimState.currentMode === ModeName.Visual) {
text = vimState.allCursors.map(range => {
return vscode.window.activeTextEditor.document.getText(new vscode.Range(
range.start,
range.stop.getRight()
));
}).join("\n");
} else if (vimState.currentMode === ModeName.VisualLine) {
text = vimState.allCursors.map(range => {
return vscode.window.activeTextEditor.document.getText(new vscode.Range(
range.start.getLineBegin(),
range.stop.getLineEnd()
));
}).join("\n");
} else if (vimState.currentMode === ModeName.VisualBlock) {
for ( const { line } of Position.IterateLine(vimState)) {
text += line + '\n';
}
}
clipboard.copy(text, (err) => {
if (err) {
vscode.window.showErrorMessage(`Error copying to clipboard.
You use an OS that VSCodeVim's clipboard library doesn't support. :( Set vim.overrideCopy to be false to get
subpar behavior. And make some noise on VSCode's issue #217 if you want this fixed.`);
}
});
return vimState;
}
}
@RegisterAction
class CommandNextSearchMatch extends BaseMovement {
keys = ["n"];

View File

@ -70,6 +70,11 @@ class ConfigurationClass {
*/
useCtrlKeys = false;
/**
* Override default VSCode copy behavior.
*/
overrideCopy = true;
/**
* Width in characters to word-wrap to.
*/

View File

@ -1703,6 +1703,7 @@ export class ModeHandler implements vscode.Disposable {
}
vscode.commands.executeCommand('setContext', 'vim.useCtrlKeys', Configuration.useCtrlKeys);
vscode.commands.executeCommand('setContext', 'vim.overrideCopy', Configuration.overrideCopy);
vscode.commands.executeCommand('setContext', 'vim.platform', process.platform);
}