Skip to content

Commit

Permalink
Use angle bracket notation replacing ctrl+* => <C-*>, backspace => <B…
Browse files Browse the repository at this point in the history
…S>, etc
  • Loading branch information
jpoon committed Aug 31, 2016
1 parent e0449ae commit ec8c644
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 158 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ On OS X, open Terminal and run the following command:
defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false
```

#### How can I bind `jj` to `<escape>`?
#### How can I bind `jj` to `<Esc>`?

1. Add the following to `settings.json` (open the Command Pallete and search for "User Settings"):

```
"vim.insertModeKeyBindings": [
{
"before": ["j", "j"],
"after": ["<escape>"]
"after": ["<Esc>"]
}
]
```

2. If you want to press `jj` in modes which are not Insert Mode and still have it trigger `<escape>`, do the following as well:
2. If you want to press `jj` in modes which are not Insert Mode and still have it trigger `<Esc>`, do the following as well:

```
"vim.otherModesKeyBindings": [
{
"before": ["j", "j"],
"after": ["<escape>"]
"after": ["<Esc>"]
}
]
```
Expand All @@ -83,7 +83,7 @@ Notice the problem is that if you did this normally, the `j` in `gj` would be ex

Don't forget to restart!

#### How can I enable `ctrl-c` or `ctrl-[` as an alternative to `<escape>`?
#### How can I enable `ctrl-c` or `ctrl-[` as an alternative to `<Esc>`?

Put the following in your `settings.json`:

Expand Down
22 changes: 14 additions & 8 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,22 @@ export async function activate(context: vscode.ExtensionContext) {
});

for (let { key } of packagejson.contributes.keybindings) {
if (key.startsWith("ctrl+")) {
registerCommand(context, `extension.vim_${ key }`, () => handleKeyEvent(key));
} else {
let bracketedKey = `<${ key.toLowerCase() }>`;

registerCommand(context, `extension.vim_${ key.toLowerCase() }`, () => handleKeyEvent(`${ bracketedKey }`));
key = key.toLowerCase();

const keyNotationTranslations = {
'ctrl+' : 'C-',
'escape': 'Esc',
'backspace': 'BS',
'delete': 'Del',
};

let bracketedKey = `<${ key }>`;
for (let searchKey in keyNotationTranslations) {
bracketedKey = bracketedKey.replace(searchKey, keyNotationTranslations[searchKey]);
}
}

registerCommand(context, `extension.vim_esc`, () => handleKeyEvent(`<escape>`));
registerCommand(context, `extension.vim_${ key }`, () => handleKeyEvent(`${ bracketedKey }`));
}

// Initialize mode handler for current active Text Editor at startup.
if (vscode.window.activeTextEditor) {
Expand Down
52 changes: 26 additions & 26 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export abstract class BaseMovement extends BaseAction {
}

/**
* A command is something like <escape>, :, v, i, etc.
* A command is something like <Esc>, :, v, i, etc.
*/
export abstract class BaseCommand extends BaseAction {
/**
Expand Down Expand Up @@ -413,7 +413,7 @@ class CommandEsc extends BaseCommand {
ModeName.SearchInProgressMode,
ModeName.Replace
];
keys = ["<escape>"];
keys = ["<Esc>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
if (vimState.currentMode !== ModeName.Visual &&
Expand All @@ -435,13 +435,13 @@ class CommandEsc extends BaseCommand {

@RegisterAction
class CommandCtrlOpenBracket extends CommandEsc {
keys = ["ctrl+["];
keys = ["<C-[>"];
}

@RegisterAction
class CommandCtrlW extends BaseCommand {
modes = [ModeName.Insert];
keys = ["ctrl+w"];
keys = ["<C-w>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const wordBegin = position.getWordLeft();
Expand All @@ -456,7 +456,7 @@ class CommandCtrlW extends BaseCommand {
@RegisterAction
class CommandCtrlE extends BaseCommand {
modes = [ModeName.Normal];
keys = ["ctrl+e"];
keys = ["<C-e>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("scrollLineDown");
Expand All @@ -468,7 +468,7 @@ class CommandCtrlE extends BaseCommand {
@RegisterAction
class CommandCtrlY extends BaseCommand {
modes = [ModeName.Normal];
keys = ["ctrl+y"];
keys = ["<C-y>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("scrollLineUp");
Expand All @@ -479,7 +479,7 @@ class CommandCtrlY extends BaseCommand {

@RegisterAction
class CommandCtrlC extends CommandEsc {
keys = ["ctrl+c"];
keys = ["<C-c>"];
}

@RegisterAction
Expand Down Expand Up @@ -520,7 +520,7 @@ class CommandReplaceInReplaceMode extends BaseCommand {

const replaceState = vimState.replaceState!;

if (char === "<backspace>") {
if (char === "<BS>") {
if (position.isBeforeOrEqual(replaceState.replaceCursorStartPosition)) {
vimState.cursorPosition = position.getLeft();
vimState.cursorStartPosition = position.getLeft();
Expand Down Expand Up @@ -608,7 +608,7 @@ class CommandInsertInSearchMode extends BaseCommand {
const searchState = vimState.searchState!;

// handle special keys first
if (key === "<backspace>") {
if (key === "<BS>") {
searchState.searchString = searchState.searchString.slice(0, -1);
} else if (key === "\n") {
vimState.currentMode = ModeName.Normal;
Expand All @@ -625,12 +625,12 @@ class CommandInsertInSearchMode extends BaseCommand {
vimState.searchStatePrevious = searchState;

return vimState;
} else if (key === "<escape>") {
} else if (key === "<Esc>") {
vimState.currentMode = ModeName.Normal;
vimState.searchState = undefined;

return vimState;
} else if (key === "ctrl+v") {
} else if (key === "<C-v>") {
const text = await new Promise<string>((resolve, reject) =>
clipboard.paste((err, text) => err ? reject(err) : resolve(text))
);
Expand Down Expand Up @@ -754,7 +754,7 @@ class CommandInsertInInsertMode extends BaseCommand {
public async exec(position: Position, vimState: VimState): Promise<VimState> {
const char = this.keysPressed[this.keysPressed.length - 1];

if (char === "<backspace>") {
if (char === "<BS>") {
const newPosition = await TextEditor.backspace(position);

vimState.cursorPosition = newPosition;
Expand Down Expand Up @@ -1511,7 +1511,7 @@ class CommandUndo extends BaseCommand {
@RegisterAction
class CommandRedo extends BaseCommand {
modes = [ModeName.Normal];
keys = ["ctrl+r"];
keys = ["<C-r>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const newPosition = await vimState.historyTracker.goForwardHistoryStep();
Expand All @@ -1527,7 +1527,7 @@ class CommandRedo extends BaseCommand {
@RegisterAction
class CommandMoveFullPageDown extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["ctrl+f"];
keys = ["<C-f>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("cursorPageDown");
Expand All @@ -1538,7 +1538,7 @@ class CommandMoveFullPageDown extends BaseCommand {
@RegisterAction
class CommandMoveFullPageUp extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ["ctrl+b"];
keys = ["<C-b>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("cursorPageUp");
Expand All @@ -1548,7 +1548,7 @@ class CommandMoveFullPageUp extends BaseCommand {

@RegisterAction
class CommandMoveHalfPageDown extends BaseMovement {
keys = ["ctrl+d"];
keys = ["<C-d>"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(
Expand All @@ -1560,7 +1560,7 @@ class CommandMoveHalfPageDown extends BaseMovement {

@RegisterAction
class CommandMoveHalfPageUp extends BaseMovement {
keys = ["ctrl+u"];
keys = ["<C-u>"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return new Position(Math.max(0, position.line - Configuration.getInstance().scroll), position.character);
Expand Down Expand Up @@ -1641,7 +1641,7 @@ class CommandVisualMode extends BaseCommand {
@RegisterAction
class CommandVisualBlockMode extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualBlock];
keys = ["ctrl+v"];
keys = ["<C-v>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {

Expand Down Expand Up @@ -1820,7 +1820,7 @@ class MoveLeftArrow extends MoveLeft {
@RegisterAction
class BackSpaceInNormalMode extends BaseMovement {
modes = [ModeName.Normal];
keys = ["<backspace>"];
keys = ["<BS>"];

public async execAction(position: Position, vimState: VimState): Promise<Position> {
return position.getLeftThroughLineBreaks();
Expand Down Expand Up @@ -1896,7 +1896,7 @@ class MoveRightWithSpace extends BaseMovement {
@RegisterAction
class MoveToRightPane extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["ctrl+w", "l"];
keys = ["<C-w>", "l"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.focusChanged = true;
Expand All @@ -1908,7 +1908,7 @@ class MoveToRightPane extends BaseCommand {
@RegisterAction
class MoveToLeftPane extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
keys = ["ctrl+w", "h"];
keys = ["<C-w>", "h"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.focusChanged = true;
Expand Down Expand Up @@ -2505,7 +2505,7 @@ class ActionDeleteChar extends BaseCommand {
@RegisterAction
class ActionDeleteCharWithDeleteKey extends BaseCommand {
modes = [ModeName.Normal];
keys = ["<delete>"];
keys = ["<Del>"];
canBePrefixedWithCount = true;
canBeRepeatedWithDot = true;

Expand Down Expand Up @@ -2788,14 +2788,14 @@ class InsertInInsertVisualBlockMode extends BaseCommand {
return vimState;
}

if (char === '<backspace>' && vimState.topLeft.character === 0) {
if (char === '<BS>' && vimState.topLeft.character === 0) {
return vimState;
}

for (const { start, end } of Position.IterateLine(vimState)) {
const insertPos = insertAtStart ? start : end;

if (char === '<backspace>') {
if (char === '<BS>') {
await TextEditor.backspace(insertPos.getLeft());

posChange = -1;
Expand Down Expand Up @@ -3626,13 +3626,13 @@ abstract class IncrementDecrementNumberAction extends BaseCommand {

@RegisterAction
class IncrementNumberAction extends IncrementDecrementNumberAction {
keys = ["ctrl+a"];
keys = ["<C-a>"];
offset = +1;
}

@RegisterAction
class DecrementNumberAction extends IncrementDecrementNumberAction {
keys = ["ctrl+x"];
keys = ["<C-x>"];
offset = -1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/history/historyTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class HistoryStep {
// collapse add+del into add. this might make current.text.length === 0, see beginning of loop
current.text = current.text.slice(0, -next.text.length);
} else {
// del+add must be two separate DocumentChanges. e.g. start with "a|b", do `i<backspace>x<escape>` you end up with "|xb"
// del+add must be two separate DocumentChanges. e.g. start with "a|b", do `i<BS>x<Esc>` you end up with "|xb"
// also handles multiple changes in distant locations in the document
merged.push(current);
current = next;
Expand Down
12 changes: 1 addition & 11 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class RecordedState {
public get command(): BaseCommand {
const list = _.filter(this.actionsRun, a => a instanceof BaseCommand);

// TODO - disregard <escape>, then assert this is of length 1.
// TODO - disregard <Esc>, then assert this is of length 1.

return list[0] as any;
}
Expand Down Expand Up @@ -625,16 +625,6 @@ export class ModeHandler implements vscode.Disposable {
}

async handleKeyEvent(key: string): Promise<Boolean> {
if (key === "<c-r>") { key = "ctrl+r"; } // TODO - temporary hack for tests only!
if (key === "<c-a>") { key = "ctrl+a"; } // TODO - temporary hack for tests only!
if (key === "<c-x>") { key = "ctrl+x"; } // TODO - temporary hack for tests only!

if (key === "<esc>") { key = "<escape>"; }

// Due to a limitation in Electron, en-US QWERTY char codes are used in international keyboards.
// We'll try to mitigate this problem until it's fixed upstream.
// https://github.com/Microsoft/vscode/issues/713

this._vimState.cursorPositionJustBeforeAnythingHappened = this._vimState.cursorPosition;

try {
Expand Down
12 changes: 6 additions & 6 deletions test/cmd_line/substitute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ suite("Basic substitute", () => {
teardown(cleanUpWorkspace);

test("Replace single word once", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>']);
await runCmdLine("%s/a/d", modeHandler);

assertEqualLines([
Expand All @@ -24,7 +24,7 @@ suite("Basic substitute", () => {
});

test("Replace with `g` flag", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>']);
await runCmdLine("%s/a/d/g", modeHandler);

assertEqualLines([
Expand All @@ -33,7 +33,7 @@ suite("Basic substitute", () => {
});

test("Replace multiple lines", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>', 'o', 'a', 'b']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b']);
await runCmdLine("%s/a/d/g", modeHandler);

assertEqualLines([
Expand All @@ -43,7 +43,7 @@ suite("Basic substitute", () => {
});

test("Replace across specific lines", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>', 'o', 'a', 'b']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b']);
await runCmdLine("1,1s/a/d/g", modeHandler);

assertEqualLines([
Expand All @@ -53,7 +53,7 @@ suite("Basic substitute", () => {
});

test("Replace current line with no active selection", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>', 'o', 'a', 'b', '<escape>']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b', '<Esc>']);
await runCmdLine("s/a/d/g", modeHandler);

assertEqualLines([
Expand All @@ -63,7 +63,7 @@ suite("Basic substitute", () => {
});

test("Replace text in selection", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>', 'o', 'a', 'b', '<escape>', '$', 'v', 'k', '0']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>', 'o', 'a', 'b', '<Esc>', '$', 'v', 'k', '0']);
await runCmdLine("'<,'>s/a/d/g", modeHandler);

assertEqualLines([
Expand Down
2 changes: 1 addition & 1 deletion test/cmd_line/writequit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ suite("Basic write-quit", () => {
teardown(cleanUpWorkspace);

test("Run write and quit", async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<escape>']);
await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '<Esc>']);

await runCmdLine("wq", modeHandler);
await WaitForVsCodeClose();
Expand Down
Loading

0 comments on commit ec8c644

Please sign in to comment.