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

时间:2018-12-22 16:53:36

标签: vim visual-studio-code vscode-settings vscodevim

我正在使用Vim with VSCode

在插入模式下,我tryingctrl+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" }]

这两个都不起作用。

7 个答案:

答案 0 :(得分:2)

尝试使用commands选项:

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

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

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

我现在对您的建议是避免重新映射Ctrl,而应使用<leader>。您还可以适当地组织这些发现,并在GitHub上打开一个新期刊。

PS

您可以在文件->首选项->键盘快捷键中检查命令名称:

enter image description here

答案 1 :(得分:0)

我发现递归映射有效:

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

尽管它们并不理想。

答案 2 :(得分:0)

这对我有用:

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"
  },
]

答案 3 :(得分:0)

在您的keybindings.json中尝试一下:

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

答案 4 :(得分:0)

将以下内容添加到settings.json对我而言有效:

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

答案 5 :(得分:0)

tl; dr

在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

答案 6 :(得分:0)

首先:在您的vim.useCtrlKeys": true,中设置setting.json

然后:

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

您的意愿已经完成

相关问题