简体   繁体   English

Vim for VSCode:重新映射 ctrl + e 以在插入模式下转到行尾

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

I'm using Vim with VSCode .我将Vim 与 VSCode 一起使用。

I'm trying to remap ctrl+e to got to the end of the line when in insert mode.在插入模式下,我试图重新映射ctrl+e以到达行尾。 Here is what I wrote in my settings.json :这是我在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:尝试使用commands选项:

"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.更新:我尝试了几种<C-...>组合,经过几个小时的摆弄,我得出的结论是某些Ctrl绑定根本不起作用。 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.我现在给你的建议是避免Ctrl重映射,改用<leader> You can also properly organize these findings and open a new issue on GitHub.您还可以适当地组织这些发现并在 GitHub 上打开一个新问题。

PS聚苯乙烯

You can check command names in File -> Preferences -> Keyboard Shortcuts:您可以在文件 -> 首选项 -> 键盘快捷键中检查命令名称:

在此处输入图像描述

This is what worked for me:这对我有用:

VSCode 1.37.1 (July 2019) VSCode 1.37.1(2019 年 7 月)

VSCodeVim v1.9 VSCodeVim v1.9

First tell VSCodeVim extension to unhandle Ca and Ce .首先告诉VSCodeVim扩展取消处理CaCe This will delegate those control keys to VSCode instead of the extension:这会将这些控制键委托给 VSCode 而不是扩展名:

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

Now simply remap those keys in VSCode:现在只需在 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.编辑:我发现简单地删除最后一个条件vim.mode != 'Insert'when工作并且更干净。 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 :在你的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:将以下内容添加到settings.json对我有用:

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

tl;dr tl;博士

in keybindings.json:在 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.在 2020-05-15 的这个线程中试过答案,没有一个仍然有效。 Found this issue https://github.com/VSCodeVim/Vim/issues/3126 containing a workaround, and it worked for me.发现这个问题https://github.com/VSCodeVim/Vim/issues/3126包含一个解决方法,它对我有用。
My vscode version: 1.45.0我的 vscode 版本:1.45.0
Credit goes to the issue author https://github.com/paupalou感谢问题作者https://github.com/paupalou

Firstly: set vim.useCtrlKeys": true, in your setting.json首先:在你的setting.json中设置vim.useCtrlKeys": true,

then:然后:

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

and your will done完成你的旨意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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