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

7

6

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.

Amiss answered 22/12, 2018 at 16:53 Comment(0)
A
6

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.

P.S

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

enter image description here

Asaasabi answered 22/12, 2018 at 17:16 Comment(2)
I tried it using: "vim.insertModeKeyBindingsNonRecursive": [ { "before": ["<C-e>"], "commands": { "command": "cursorLineEnd" } } ] , because cursorLineEnd is more what I'm looking for. Unfortunately, it also doesn't work :(Amiss
It appears that binding to certain <C-...> combinations is currently not so stable, I updated my answer with my thoughts on the matter.Asaasabi
M
3

This is what worked for me:

VSCode 1.37.1 (July 2019)

VSCodeVim v1.9

First tell VSCodeVim extension to unhandle C-a and C-e. 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"
  },
]
Monoatomic answered 1/9, 2019 at 13:14 Comment(1)
Can't find extension.vim_home and extension.vim_end,must instead of cursorHome & cursorEnd.Polyhistor
V
3

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'"
}
Vivyan answered 27/11, 2019 at 8:56 Comment(1)
That helped me a great deal when I installed Rewrap and mapped it to "gq". Using vim mode I couldn't write the letter "g" anymore because vsCode was waiting for "g" to happen. I added the condition to my mapping and that fixed the problem I had.Ochlocracy
A
1

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

Antonetta answered 14/5, 2020 at 18:54 Comment(0)
E
0

I found recursive mappings work:

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

Though they are not ideal.

Ephialtes answered 15/7, 2019 at 5:12 Comment(1)
Unfortunately, it soon became not working, and I have no idea.Ephialtes
S
0

Adding the following to settings.json works for me:

"vim.inserModeKeyBindings": [
        {
            "before": ["<C-e>"],
            "after": ["<esc>", "$", "a"]
        }
]
Strict answered 11/5, 2020 at 7:29 Comment(0)
R
0

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

Robbinrobbins answered 26/9, 2020 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.