Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Return to normal mode if the focus is lost

Open aloispichler opened this issue 2 years ago • 4 comments

I'd like to return to normal mode, if the focus is lost. How can this be accomplished? The vim-command autocmd FocusLost * call feedkeys("\<esc>") (or autocmd FocusLost, TabLeave * call feedkeys("\<esc>")) automatically returns to normal mode, if the focus is lost.

We'd appreciate the same functionality in VSCodeVim. Thank you!

aloispichler avatar Oct 06 '22 08:10 aloispichler

+1

JonasKru avatar Oct 16 '22 08:10 JonasKru

This would be very useful. A couple of use cases from my own experience:

  1. Using Jupyter notebooks with VSCodeVim. Overall this works great, but one big oversight is that cells do not revert to normal mode when they lose focus. For example, you might type some code into a cell then hit Shift+Enter to evaluate it and focus the next cell (which will be created in normal mode if it doesn't exist yet). Whenever you return to the first cell (for example by hitting k enough times in the cell below), you will be surprised to find that it is still in insert mode, causing you to type nonsense into it. Basically, you have to mentally keep track of the last Insert/Normal-mode state of all of your notebook cells, which is impossible.
  2. Just editing regular files. Sometimes you might switch away to a different tab (for example with Ctrl+Tab), and after a long time come back to the file that you were working on, expecting to navigate quickly within it using normal mode commands, only to be surprised that it is still in insert mode as you left it.

This is a friction that only arises in VSCodeVim because there are so many ways to leave focus without leaving insert mode. In stock terminal-mode Vim, I don't believe it's possible to move the "focus" (i.e. move the cursor) from one text buffer to another without first leaving insert mode, so it never comes up. (Although @aloispichler's autocmds rightly note some examples where this can come up when using tabs or a GUI version of vim.)

maxbane avatar Feb 01 '23 15:02 maxbane

This would be very useful. A couple of use cases from my own experience:

  1. Using Jupyter notebooks with VSCodeVim. Overall this works great, but one big oversight is that cells do not revert to normal mode when they lose focus. For example, you might type some code into a cell then hit Shift+Enter to evaluate it and focus the next cell (which will be created in normal mode if it doesn't exist yet). Whenever you return to the first cell (for example by hitting k enough times in the cell below), you will be surprised to find that it is still in insert mode, causing you to type nonsense into it. Basically, you have to mentally keep track of the last Insert/Normal-mode state of all of your notebook cells, which is impossible.
  2. Just editing regular files. Sometimes you might switch away to a different tab (for example with Ctrl+Tab), and after a long time come back to the file that you were working on, expecting to navigate quickly within it using normal mode commands, only to be surprised that it is still in insert mode as you left it.

This is a friction that only arises in VSCodeVim because there are so many ways to leave focus without leaving insert mode. In stock terminal-mode Vim, I don't believe it's possible to move the "focus" (i.e. move the cursor) from one text buffer to another without first leaving insert mode, so it never comes up. (Although @aloispichler's autocmds rightly note some examples where this can come up when using tabs or a GUI version of vim.)

Thank you @maxbane for identifying where the issue happens with jupyter notebooks. I decided to change ctrl+enter and shift+enter in the keybindings json to enter ~nomal~ (Edit: normal) mode before running the cell. If you wish, you can add the following to your ~keybings~ (Edit: keybindings) json (I took the "when" string from the binding of escape for vim and from the original bindings of either shift+enter or ctrl+enter (I took the one under notebook with source ~jupter~ (Edit: jupyter) keymap since the one under just jupyter did not work for ctrl+enter for me))

    {
  "key": "ctrl+enter",
  "command": "runCommands",
  "when": "notebookCellListFocused && vim.active && !inDebugRepl || editorTextFocus && inputFocus && notebookEditorFocused && vim.active && !inDebugRepl",
  "args": {
    "commands": [
      "extension.vim_escape",
      "notebook.cell.executeAndFocusContainer",
    ]
  }
},
{
  "key": "shift+enter",
  "command": "runCommands",
  "when": "notebookCellListFocused && !interactiveEditorFocused && notebookCellType == 'code' && vim.active && !inDebugRepl || editorTextFocus && inputFocus && notebookEditorFocused && !interactiveEditorFocused && vim.active && !inDebugRepl",
  "args": {
    "commands": [
      "extension.vim_escape",
      "notebook.cell.executeAndSelectBelow",
    ]
  }
}

userrand avatar Dec 24 '23 09:12 userrand

I just found the small but excellent extension https://github.com/xaviergmail/vscodevim-leave-insertmode by @xaviergmail, and it does exactly this (leave normal mode when tab focus or notebook cell focus is lost).

SimonHeybrock avatar Jul 03 '24 09:07 SimonHeybrock