microsoft / vscode-docs

Public documentation for Visual Studio Code
http://code.visualstudio.com/docs
Other
5.72k stars 4.66k forks source link

Some control commands don't work for me in the terminal (Ctrl-R, Ctrl-A) #5221

Closed johnfn closed 2 years ago

johnfn commented 2 years ago

Does this issue occur when all extensions are disabled?: Yes/No

Yes

Steps to Reproduce:

  1. Open up Visual Studio Code
  2. Open an integrated terminal
  3. Type Ctrl-A

You expect this to bring the terminal cursor to the beginning of the line. However, this happens instead:

image

Here's another command that doesn't work:

  1. Open up Visual Studio Code
  2. Open an integrated terminal
  3. Type Ctrl-R

For this, literally nothing happens at all. Nothing gets inserted in the terminal. The desired result would be that bck-i-search starts, as it does in the normal terminal. However, that does not happen, either.

I've tried everything. I've disabled all my extensions. I've commented out all my user settings. I've disabled chording in the terminal. Nothing helps.

Additionally, these commands work perfectly fine on my other laptop, which is also a macbook pro with apple silicon, and I use vscode settings sync, so I'm not sure what the difference is!

meganrogge commented 2 years ago

the behavior you described can be resolved by enabling

terminal.integrated.sendKeybindingsToShell

meganrogge commented 2 years ago

I'm also on mac and Ctrl A even with that setting disabled works. What shell are you using? What are your settings?

meganrogge commented 2 years ago

Recording 2022-01-26 at 16 24 39

johnfn commented 2 years ago

No matter whether I set that to be true or false, I still have this issue unfortunately!

I'm using the standard macos terminal (with zsh) and even when I disable all settings in my user settings JSON file, I still have this issue.

meganrogge commented 2 years ago

please provide me with the logs when you run Keyboard Shortcuts Troubleshooting

Tyriar commented 2 years ago

Also, can you try enabling escape sequence logging and pasting in the output? This will tell us what the shell process is telling the terminal to do which will identify whether it's an issue with rendering or the process telling us the wrong thing.

johnfn commented 2 years ago

Here's troubleshooting when I type Ctrl-R

[2022-01-27 16:27:44.843] [renderer3] [info] [KeybindingService]: / Received  keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 17, key: Control
[2022-01-27 16:27:44.844] [renderer3] [info] [KeybindingService]: | Converted keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 5 ('Ctrl')
[2022-01-27 16:27:44.844] [renderer3] [info] [KeybindingService]: \ Keyboard event cannot be dispatched in keydown phase.
[2022-01-27 16:27:45.297] [renderer3] [info] [KeybindingService]: | Resolving ctrl+[KeyR]
[2022-01-27 16:27:45.298] [renderer3] [info] [KeybindingService]: \ No keybinding entries.
[2022-01-27 16:27:45.405] [renderer3] [info] [KeybindingService]: | Resolving ctrl+[KeyR]
[2022-01-27 16:27:45.405] [renderer3] [info] [KeybindingService]: \ No keybinding entries.
[2022-01-27 16:27:45.445] [renderer3] [info] [KeybindingService]: + Ignoring single modifier ctrl due to it being pressed together with other keys.

Ctrl-A

[2022-01-27 16:28:19.033] [renderer3] [info] [KeybindingService]: / Received  keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 17, key: Control
[2022-01-27 16:28:19.033] [renderer3] [info] [KeybindingService]: | Converted keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 5 ('Ctrl')
[2022-01-27 16:28:19.033] [renderer3] [info] [KeybindingService]: \ Keyboard event cannot be dispatched in keydown phase.
[2022-01-27 16:28:19.394] [renderer3] [info] [KeybindingService]: | Resolving ctrl+[KeyA]
[2022-01-27 16:28:19.395] [renderer3] [info] [KeybindingService]: \ From 2 keybinding entries, no when clauses matched the context.
[2022-01-27 16:28:19.519] [renderer3] [info] [KeybindingService]: | Resolving ctrl+[KeyA]
[2022-01-27 16:28:19.519] [renderer3] [info] [KeybindingService]: \ From 2 keybinding entries, no when clauses matched the context.
[2022-01-27 16:28:19.564] [renderer3] [info] [KeybindingService]: + Ignoring single modifier ctrl due to it being pressed together with other keys.

Here's escape sequence logging for Ctrl-A

 INFO [KeybindingService]: / Received  keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 17, key: Control
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: | Converted keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 5 ('Ctrl')
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: \ Keyboard event cannot be dispatched in keydown phase.
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: | Resolving ctrl+[KeyA]
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: \ From 2 keybinding entries, no when clauses matched the context.
LogService.ts:62 xterm.js: sending data "" [1]
LogService.ts:62 xterm.js: parsing data ^A
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: | Resolving ctrl+[KeyA]
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: \ From 2 keybinding entries, no when clauses matched the context.
workbench.desktop.main.js:sourcemap:577  INFO [KeybindingService]: + Ignoring single modifier ctrl due to it being pressed together with other keys.

And Ctrl-R

 INFO [KeybindingService]: / Received  keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 17, key: Control
workbench.desktop.main.js:577  INFO [KeybindingService]: | Converted keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 5 ('Ctrl')
workbench.desktop.main.js:577  INFO [KeybindingService]: \ Keyboard event cannot be dispatched in keydown phase.
workbench.desktop.main.js:577  INFO [KeybindingService]: | Resolving ctrl+[KeyR]
workbench.desktop.main.js:577  INFO [KeybindingService]: \ No keybinding entries.
LogService.ts:62 xterm.js: sending data "" [18]
LogService.ts:62 xterm.js: parsing data 

grant@Grants-MBP gem % ^A^A
workbench.desktop.main.js:577  INFO [KeybindingService]: | Resolving ctrl+[KeyR]
workbench.desktop.main.js:577  INFO [KeybindingService]: \ No keybinding entries.
workbench.desktop.main.js:577  INFO [KeybindingService]: + Ignoring single modifier ctrl due to it being pressed together with other keys.
miguelcoba commented 2 years ago

Hi @johnfn did you figure out what was happening. I am getting exactly the same behavior and I don't know what is happening. If I open a bash shell instead of a zsh the ctl+r and ctr+p et al all work fine

Screen Shot 2022-02-24 at 13 22 55
[2022-02-24 13:23:27.424] [renderer1] [info] [KeybindingService]: / Received  keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 17, key: Control
[2022-02-24 13:23:27.425] [renderer1] [info] [KeybindingService]: | Converted keydown event - modifiers: [ctrl], code: ControlLeft, keyCode: 5 ('Ctrl')
[2022-02-24 13:23:27.425] [renderer1] [info] [KeybindingService]: \ Keyboard event cannot be dispatched in keydown phase.
[2022-02-24 13:23:28.713] [renderer1] [info] [KeybindingService]: | Resolving ctrl+[KeyR]
[2022-02-24 13:23:28.714] [renderer1] [info] [KeybindingService]: \ From 2 keybinding entries, matched workbench.action.openRecent, when: no when condition, source: built-in.
[2022-02-24 13:23:28.808] [renderer1] [info] [KeybindingService]: | Resolving ctrl+[KeyR]
[2022-02-24 13:23:28.809] [renderer1] [info] [KeybindingService]: \ From 2 keybinding entries, matched workbench.action.openRecent, when: no when condition, source: built-in.
[2022-02-24 13:23:28.937] [renderer1] [info] [KeybindingService]: + Ignoring single modifier ctrl due to it being pressed together with other keys.

Only change was to remove oh-my-zsh and start using zplug. But even if I disable all the zplug initialization stuff in .zshrc, the terminal never works correctly for the ctrl-* chords

johnfn commented 2 years ago

Unfortunately, no. I still have this issue and I've been unable to solve it.

Tyriar commented 2 years ago

@johnfn the terminal looks to be working correctly so this is an issue with your shell, not VS Code's terminal.

LogService.ts:62 xterm.js: sending data "�" [1]

This is saying the shell is receiving the correct character \u0001

LogService.ts:62 xterm.js: sending data "�" [18]

This is saying it's getting the correct \u0012


@johnfn for you ctrl+r is matching workbench.action.openRecent, I don't have enough information about your setup and your issue appears to be unrelated to this. You can try adding this to your keybindings which will unset the openRecent keybinding and set it to only happen when the terminal is not focused.

{ "key": "ctrl+r", "command": "-workbench.action.openRecent" },
{ "key": "ctrl+r", "command": "workbench.action.openRecent", "when": "!terminalFocus" }
miguelcoba commented 2 years ago

Just for future reference, I found this comment https://github.com/microsoft/vscode/issues/109078#issuecomment-724594972

and it seems that setting set -o emacs solves the issue for an already open terminal. After that the Ctrl+* commands work correctly.

It seems that the terminal open inside vs code it has different options enabled than the terminal open with iterm or the Terminal app outside vs code.

johnfn commented 2 years ago

@Tyriar

this is an issue with your shell, not VS Code's terminal.

No, that doesn't seem to be the case. Everywhere else on my computer, ctrl-a and ctrl-r work fine.

This issue should not be closed. It's not a question. It's broken functionality on a vanilla install of vscode. Even if it's an issue with my shell, that issue doesn't manifest in any app other than vscode.

miguelcoba commented 2 years ago

Final comment. I found the cause and the fix.

Cause

Somehow the keybinding when starting zsh inside vscode is different than when starting it on Terminal. For the Ctrl+R et al to work we need emacs keybinding emulation.

According to the zsh docs here:

If one of the VISUAL or EDITOR environment variables contain the string ‘vi’ when the shell starts up then it will be ‘viins’, otherwise it will be ‘emacs’. bindkey’s -e and -v options provide a convenient way to override this default choice.

in my case, I had this in my $HOME/.zshrc

export EDITOR='vim'
export VISUAL='vim'

and that was setting keybinding to vi instead of emacs. In the external terminal somehow (maybe oh-my-zsh or zplug) it is correctly set to emacs, but in vscode this is never overridden so it remains in vi mode and that's why the ctrl+* commands didn't work.

Why this happens in vscode? I have no idea.

Fix

You have two options:

I did the second one in my $HOME/.zshrc and now the external terminal and the integrated vscode terminal both use emacs keybinding emulation and the ctrl-* commands work correctly.

Hope this helps

Tyriar commented 2 years ago

@miguelcoba thanks for the investigation, reopening to add this to the faq. My guess about why VS Code is different is either because we inherit the base environment from an $SHELL -ilc shell that sources the "development environment" or because it's run as a login/non-login shell.

miguelcoba commented 2 years ago

Thanks, @Tyriar

johnfn commented 2 years ago

Wow @miguelcoba - you're dead on - that was the solution! Amazing - I've been struggling with this for ages!

wsshin commented 1 year ago

I thank @miguelcoba. I have been struggling with this issue, and you solution works.

QuarticCat commented 7 months ago

Why this happens in vscode? I have no idea.

@miguelcoba I guess this is the case: you opened VSCode from an external terminal, thus the environment variables of this VSCode instance contain EDITOR & VISUAL, hence it's internal terminal contains them as well, and therefore the default viins keymap.