5

我将Vim 与 VSCode 一起使用。

在插入模式下,我试图重新映射ctrl+e到行尾。这是我写在我的settings.json

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

不幸的是,这在某种程度上是行不通的。我怎样才能重新映射这个?

编辑: 根据答案,我也尝试过

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

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

这也都没有工作。

4

7 回答 7

6

尝试改用该commands选项:

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

更新:我尝试了几种<C-...>组合,经过几个小时的摆弄,我得出的结论是某些Ctrl绑定根本不起作用。我尝试了多种变体均无济于事,并且任何其他组合键似乎都可以正常工作,例如:

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

我现在对您的建议是避免Ctrl重新映射,<leader>而是使用。您还可以正确组织这些发现并在 GitHub 上打开一个新问题。

附言

您可以在 File -> Preferences -> Keyboard Shortcuts 中检查命令名称:

在此处输入图像描述

于 2018-12-22T17:16:53.340 回答
2

在你的试试这个keybindings.json

{
  "key": "ctrl+a",
  "command": "cursorLineStart",
  "when": "textInputFocus && vim.mode == 'Insert'"
},
{
  "key": "ctrl+e",
  "command": "cursorLineEnd",
  "when": "textInputFocus && vim.mode == 'Insert'"
}
于 2019-11-27T08:56:16.103 回答
1

这对我有用:

VSCode 1.37.1(2019 年 7 月)

VSCodeVim v1.9

首先告诉 VSCodeVim 扩展取消处理 C-aC-e. 这会将这些控制键委托给 VSCode 而不是扩展:

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

现在只需在 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'"
  },
]

我发现前两个绑定在正常和插入模式下工作,但不是在可视模式下(它有点移动光标但没有选择任何内容)。最后两个确保它也可以在可视模式下工作。

编辑:我发现简单地删除工作中的最后一个条件vim.mode != 'Insert'并且when更清洁。所以代替上面的键绑定,简单地:

// 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"
  },
]
于 2019-09-01T13:14:19.837 回答
0

首先:设置vim.useCtrlKeys": true,你的setting.json

然后:

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

你的意志完成了

于 2020-09-26T05:53:26.673 回答
0

添加以下内容settings.json对我有用:

"vim.inserModeKeyBindings": [
        {
            "before": ["<C-e>"],
            "after": ["<esc>", "$", "a"]
        }
]
于 2020-05-11T07:29:32.307 回答
0

我发现递归映射有效:

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

尽管它们并不理想。

于 2019-07-15T05:12:51.937 回答
0

tl;博士

在 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'"
  }
]

长版

在 2020-05-15 在这个线程中尝试了答案,但没有一个仍然有效。发现这个问题https://github.com/VSCodeVim/Vim/issues/3126包含一个解决方法,它对我有用。
我的 vscode 版本:1.45.0 归功于
问题作者https://github.com/paupalou

于 2020-05-14T18:54:55.737 回答