Skip to content

Commit 3bfc1ae

Browse files
MrQubomarijnh
authored andcommitted
Allow mapping <Esc> in vim normal mode
1 parent 18c12c9 commit 3bfc1ae

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

keymap/vim.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,13 +865,17 @@
865865
}
866866
function handleEsc() {
867867
if (key == '<Esc>') {
868-
// Clear input state and get back to normal mode.
869-
clearInputState(cm);
870868
if (vim.visualMode) {
869+
// Get back to normal mode.
871870
exitVisualMode(cm);
872871
} else if (vim.insertMode) {
872+
// Get back to normal mode.
873873
exitInsertMode(cm);
874+
} else {
875+
// We're already in normal mode. Let '<Esc>' be handled normally.
876+
return;
874877
}
878+
clearInputState(cm);
875879
return true;
876880
}
877881
}

test/vim_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4667,6 +4667,15 @@ testVim('ex_map_key2key_from_colon', function(cm, vim, helpers) {
46674667
CodeMirror.Vim.mapclear();
46684668
}, { value: 'abc' });
46694669

4670+
testVim('map <Esc> in normal mode', function(cm, vim, helpers) {
4671+
CodeMirror.Vim.noremap('<Esc>', 'i', 'normal');
4672+
helpers.doKeys('<Esc>');
4673+
is(vim.insertMode, "Didn't switch to insert mode.");
4674+
helpers.doKeys('<Esc>');
4675+
is(!vim.insertMode, "Didn't switch to normal mode.");
4676+
CodeMirror.Vim.mapclear();
4677+
});
4678+
46704679
testVim('noremap', function(cm, vim, helpers) {
46714680
CodeMirror.Vim.noremap(';', 'l');
46724681
cm.setCursor(0, 0);

0 commit comments

Comments
 (0)