Skip to content

Commit

Permalink
[vim bindings] Make Backspace delete characters
Browse files Browse the repository at this point in the history
In Replace mode (or Overwrite mode) it should still go backwards
without deleting characters.
  • Loading branch information
lukegb authored and marijnh committed Jul 7, 2016
1 parent a0547b7 commit 84dc2d0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
19 changes: 16 additions & 3 deletions keymap/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
{ keys: '<Down>', type: 'keyToKey', toKeys: 'j' },
{ keys: '<Space>', type: 'keyToKey', toKeys: 'l' },
{ keys: '<BS>', type: 'keyToKey', toKeys: 'h', context: 'normal'},
{ keys: '<BS>', type: 'motion', motion: 'moveByCharacters', motionArgs: { forward: false }, context: 'insert'},
{ keys: '<C-Space>', type: 'keyToKey', toKeys: 'W' },
{ keys: '<C-BS>', type: 'keyToKey', toKeys: 'B', context: 'normal' },
{ keys: '<S-Space>', type: 'keyToKey', toKeys: 'w' },
Expand All @@ -73,6 +72,7 @@
{ keys: '<PageUp>', type: 'keyToKey', toKeys: '<C-b>' },
{ keys: '<PageDown>', type: 'keyToKey', toKeys: '<C-f>' },
{ keys: '<CR>', type: 'keyToKey', toKeys: 'j^', context: 'normal' },
{ keys: '<Ins>', type: 'action', action: 'toggleOverwrite', context: 'insert' },
// Motions
{ keys: 'H', type: 'motion', motion: 'moveToTopLine', motionArgs: { linewise: true, toJumplist: true }},
{ keys: 'M', type: 'motion', motion: 'moveToMiddleLine', motionArgs: { linewise: true, toJumplist: true }},
Expand Down Expand Up @@ -277,6 +277,7 @@

function cmKey(key, cm) {
if (!cm) { return undefined; }
if (this[key]) { return this[key]; }
var vimKey = cmKeyToVimKey(key);
if (!vimKey) {
return false;
Expand All @@ -289,7 +290,7 @@
}

var modifiers = {'Shift': 'S', 'Ctrl': 'C', 'Alt': 'A', 'Cmd': 'D', 'Mod': 'A'};
var specialKeys = {Enter:'CR',Backspace:'BS',Delete:'Del'};
var specialKeys = {Enter:'CR',Backspace:'BS',Delete:'Del',Insert:'Ins'};
function cmKeyToVimKey(key) {
if (key.charAt(0) == '\'') {
// Keypress character binding of format "'a'"
Expand Down Expand Up @@ -2175,6 +2176,17 @@
var registerName = actionArgs.selectedCharacter;
macroModeState.enterMacroRecordMode(cm, registerName);
},
toggleOverwrite: function(cm) {
if (!cm.state.overwrite) {
cm.toggleOverwrite(true);
cm.setOption('keyMap', 'vim-replace');
CodeMirror.signal(cm, "vim-mode-change", {mode: "replace"});
} else {
cm.toggleOverwrite(false);
cm.setOption('keyMap', 'vim-insert');
CodeMirror.signal(cm, "vim-mode-change", {mode: "insert"});
}
},
enterInsertMode: function(cm, actionArgs, vim) {
if (cm.getOption('readOnly')) { return; }
vim.insertMode = true;
Expand Down Expand Up @@ -2220,14 +2232,14 @@
return;
}
}
cm.setOption('keyMap', 'vim-insert');
cm.setOption('disableInput', false);
if (actionArgs && actionArgs.replace) {
// Handle Replace-mode as a special case of insert mode.
cm.toggleOverwrite(true);
cm.setOption('keyMap', 'vim-replace');
CodeMirror.signal(cm, "vim-mode-change", {mode: "replace"});
} else {
cm.toggleOverwrite(false);
cm.setOption('keyMap', 'vim-insert');
CodeMirror.signal(cm, "vim-mode-change", {mode: "insert"});
}
Expand Down Expand Up @@ -4779,6 +4791,7 @@
};

CodeMirror.keyMap['vim-replace'] = {
'Backspace': 'goCharLeft',
fallthrough: ['vim-insert'],
attach: attachVimMap,
detach: detachVimMap,
Expand Down
28 changes: 27 additions & 1 deletion test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function testVim(name, run, opts, expectedFail) {
for (var i = 0; i < arguments.length; i++) {
var key = arguments[i];
// Find key in keymap and handle.
var handled = CodeMirror.lookupKey(key, 'vim-insert', executeHandler);
var handled = CodeMirror.lookupKey(key, cm.getOption('keyMap'), executeHandler, cm);
// Record for insert mode.
if (handled == "handled" && cm.state.vim.insertMode && arguments[i] != 'Esc') {
var lastChange = CodeMirror.Vim.getVimGlobalState_().macroModeState.lastInsertModeChanges;
Expand Down Expand Up @@ -1419,6 +1419,32 @@ testVim('i_repeat_delete', function(cm, vim, helpers) {
eq('abe', cm.getValue());
helpers.assertCursorAt(0, 1);
}, { value: 'abcde' });
testVim('insert', function(cm, vim, helpers) {
helpers.doKeys('i');
eq('vim-insert', cm.getOption('keyMap'));
eq(false, cm.state.overwrite);
helpers.doKeys('<Ins>');
eq('vim-replace', cm.getOption('keyMap'));
eq(true, cm.state.overwrite);
helpers.doKeys('<Ins>');
eq('vim-insert', cm.getOption('keyMap'));
eq(false, cm.state.overwrite);
});
testVim('i_backspace', function(cm, vim, helpers) {
cm.setCursor(0, 10);
helpers.doKeys('i');
helpers.doInsertModeKeys('Backspace');
helpers.assertCursorAt(0, 9);
eq('012345678', cm.getValue());
}, { value: '0123456789'});
testVim('i_overwrite_backspace', function(cm, vim, helpers) {
cm.setCursor(0, 10);
helpers.doKeys('i');
helpers.doKeys('<Ins>');
helpers.doInsertModeKeys('Backspace');
helpers.assertCursorAt(0, 9);
eq('0123456789', cm.getValue());
}, { value: '0123456789'});
testVim('A', function(cm, vim, helpers) {
helpers.doKeys('A');
helpers.assertCursorAt(0, lines[0].length);
Expand Down

0 comments on commit 84dc2d0

Please sign in to comment.