5

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.

7 Answers 7

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

2
  • 1
    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 :(
    – J. Hesters
    Dec 22, 2018 at 22:36
  • It appears that binding to certain <C-...> combinations is currently not so stable, I updated my answer with my thoughts on the matter.
    – guychouk
    Dec 23, 2018 at 0:32
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"
  },
]
1
  • Can't find extension.vim_home and extension.vim_end,must instead of cursorHome & cursorEnd.
    – bnPYSse
    Dec 31, 2022 at 16:17
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'"
}
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.
    – xh3b4sd
    May 26, 2020 at 16:43
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

0

I found recursive mappings work:

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

Though they are not ideal.

1
  • Unfortunately, it soon became not working, and I have no idea.
    – moba
    Jul 19, 2019 at 11:27
0

Adding the following to settings.json works for me:

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.