ChrisZZ
ChrisZZ

Reputation: 2181

In VSCode Neovim, press Ctrl+S to save file and switch to normal mode?

I use VSCode Neovim plugin, and I would like to use "Ctrl+S" key for "save this file, and then switch to normal mode", which visually change the cursor style from "thin" to "solid".

My vim/nvim config file is with:

"--- ctrl + s  saving
noremap  <silent> <C-S>  :update<CR>
vnoremap <silent> <C-S>  <C-C>:update<CR>
inoremap <silent> <C-S>  <C-O>:update<CR><ESC>

But in VSCode, edit some words, then press "Ctrl+S" just save the file, didn't switch to normal mode. i.e. the <C-O>:update<CR><ESC> is not executed. How can I let it be executed?

Tried solution: In VSCode, Exit Vim Insert Mode on Save

settings.json:

    "macros": {
        "saveAndExitVimInsertMode": [
            "workbench.action.files.save",
            "extension.neovim.escape"
        ]
    }

keybindings.json:

    {
        "key": "ctrl+s",
        //"command": "workbench.action.files.save"
        "command": "macros.saveAndExitVimInsertMode"
    },

But still not working.

Upvotes: 0

Views: 1249

Answers (3)

imfulee
imfulee

Reputation: 11

Just in case anyone in the future is unable to install this specific macros extension because VSCodium uses openvsx. Then you could install this other macros extension instead. And change the settings.json from the key macros to macros.list

{
  "macros.list": {
    "saveAndExitVimInsertMode": [
      "workbench.action.files.save",
      "vscode-neovim.escape"
    ]
  }
}

Upvotes: 1

Simon
Simon

Reputation: 1

Another nice solution is to use the multi-command Extension:

{
  "key": "ctrl+s",
  "command": "extension.multiCommand.execute",
  "when": "editorTextFocus && neovim.init && neovim.mode == 'insert'",
  "args": {
    "sequence": ["workbench.action.files.save", "vscode-neovim.escape"]
  }
}

That way, you only send the escape signal if you are acutally in insert mode.

Upvotes: 0

ChrisZZ
ChrisZZ

Reputation: 2181

Resolved by the following config in setttings.json:

    "macros": {
        "saveAndExitVimInsertMode": [
            "workbench.action.files.save",
            //"extension.nvim_escape" // not working
            "vscode-neovim.escape" // working
        ]
    },

Reference: https://github.com/vscode-neovim/vscode-neovim/issues/74#issuecomment-563214553

Upvotes: 0

Related Questions