简体   繁体   中英

Vim for VSCode: Remap ctrl + e to go to end of line in insert mode

I'm using Vim with VSCode .

I'm trying to remap ctrl+e to got to the end of the line when in insert mode. Here is what I wrote in my settings.json :

"vim.insertModeKeyBindingsNonRecursive": [{ "before": ["<C-o>", "$"], "after": ["<C-e>"] }]

Unfortunately, this somehow doesn't work. How can I remap this?

Edit: Based on the answers, I also tried

"vim.insertModeKeyBindingsNonRecursive": [ { "before": ["<C-e>"], "commands": { "command": "cursorLineEnd" } } ]

and

"vim.insertModeKeyBindingsNonRecursive": [{ "before": ["<C-e>"], "commands": "cursorLineEnd" }]

which also both didn't work.

Try using the commands option instead:

"vim.insertModeKeyBindingsNonRecursive": [{
       "before":[
          "<C-e>"
       ],
       "after":[],
       "commands":[
          {
             "command":"cursorEnd",
             "args":[]
          }
       ]
    }]

Update : I have attempted several <C-...> combinations and after a couple of hours of fiddling I've come to the conclusion that some Ctrl bindings simply do not work. I've tried multiple variations to no avail, and any other key combination seems to work flawlessly, see this for example:

"vim.insertModeKeyBindingsNonRecursive": [
      {
         "before": [
            "j",
            "k"
         ],
         "commands": [
            "cursorLineEnd",
         ]
      }
   ]

My suggestion for you now is to avoid Ctrl remappings, use <leader> instead. You can also properly organize these findings and open a new issue on GitHub.

PS

You can check command names in File -> Preferences -> Keyboard Shortcuts:

在此处输入图像描述

This is what worked for me:

VSCode 1.37.1 (July 2019)

VSCodeVim v1.9

First tell VSCodeVim extension to unhandle Ca and Ce . This will delegate those control keys to VSCode instead of the extension:

// In your settings.json
"vim.handleKeys": {
        "<C-a>": false,
        "<C-e>": false
    },

Now simply remap those keys in VSCode:

// In your keybindings.json
[
  {
      "key": "ctrl+a", // default is Home
      "command": "cursorHome",
      "when": "textInputFocus"
  },
  {
      "key": "ctrl+e", // default is End
      "command": "cursorEnd",
      "when": "textInputFocus"
  },
  {
      "key": "ctrl+a", // default is Home
      "command": "extension.vim_home",
      "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
  },
  {
      "key": "ctrl+e", // default is End
      "command": "extension.vim_end",
      "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'"
  },
]

I found that the first two bindings work in normal and insert mode, but not in visual mode (it sort of moves the cursor but nothing gets selected). The last two ensures it also works in visual mode.

Edit: I found that simply removing the last condition vim.mode != 'Insert' in when works and is much cleaner. So instead of the keybindings above, simply:

// In your keybindings.json
[
  {
      "key": "ctrl+a",
      "command": "extension.vim_home",
      "when": "editorTextFocus && vim.active && !inDebugRepl"
  },
  {
      "key": "ctrl+e",
      "command": "extension.vim_end",
      "when": "editorTextFocus && vim.active && !inDebugRepl"
  },
]

Try this in your keybindings.json :

{
  "key": "ctrl+a",
  "command": "cursorLineStart",
  "when": "textInputFocus && vim.mode == 'Insert'"
},
{
  "key": "ctrl+e",
  "command": "cursorLineEnd",
  "when": "textInputFocus && vim.mode == 'Insert'"
}

I found recursive mappings work:

    "vim.insertModeKeyBindings": [
        {
            "before": [
                "<C-e>"
            ],
            "commands": [
                "cursorEnd"
            ],
        },
        {
            "before": [
                "<C-a>"
            ],
            "commands": [
                "cursorHome"
            ],
        }
    ],

Though they are not ideal.

Adding the following to settings.json works for me:

"vim.inserModeKeyBindings": [
        {
            "before": ["<C-e>"],
            "after": ["<esc>", "$", "a"]
        }
]

tl;dr

in keybindings.json:

[
  {
    "key": "ctrl+a",
    "command": "cursorHome",
    "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Insert'"
  },
  {
    "key": "ctrl+e",
    "command": "cursorEnd",
    "when": "editorTextFocus && vim.active && !inDebugRepl && vim.mode == 'Insert'"
  }
]

long version

Tried answers in this thread at 2020-05-15, none still working. Found this issue https://github.com/VSCodeVim/Vim/issues/3126 containing a workaround, and it worked for me.
My vscode version: 1.45.0
Credit goes to the issue author https://github.com/paupalou

Firstly: set vim.useCtrlKeys": true, in your setting.json

then:

    "vim.insertModeKeyBindingsNonRecursive": [
        {
            "before": ["<C-e>"],
            "after": [],
            "commands":[
                {
                   "command":"cursorLineEnd",
                   "args":[]
                }
             ]
        },
        {
            "before": ["<C-a>"],
            "after": [],
            "commands":[
                {
                   "command":"cursorLineStart",
                   "args":[]
                }
             ]
        }
    ],

and your will done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM