无法在vimrc中映射Home键

4
我想将Home键映射到vim中的第一个非空字符,但是映射Home键没有任何作用?如果我映射另一个键,则会正确工作。
请参见下面我的vimrc文件:
map <Home> 0w
imap <Home> <ESC>0wi

上述方法不起作用。而以下方法有效(例如使用Ctrl-F)。
map <C-f> 0w
imap <C-f> <ESC>0wi

有没有办法将 Home 键映射到这个功能?我真的很需要它,因为我在使用 Notepad++、Sublime text 2、Visual Studio 等时已经习惯了这个功能... 我还尝试了以下操作,却没有效果。如果使用其他键,它又正常了... http://vim.wikia.com/wiki/Smart_home

它对我有效...如果您不添加任何映射,Home键会有任何作用吗? - mihai
2
我尝试了你的映射,它在这里起作用。顺便说一下,你的 0w 可以改为 ^ - Kent
@mihai 是的...无论我是否使用映射,它总是回到行的开头。 - Ozkan
顺便提一下,使用Home键而不是0w真的是一个糟糕的主意,它会减慢你的速度。 - mihai
@Ozkan,请执行 :h ^ 命令, ^ 的功能和 0w 相同。 - Kent
显示剩余7条评论
1个回答

9

来自Vim FAQ(也可通过这个不错的插件获得):


这段话是关于从Vim FAQ获取信息的,可以通过一个插件来实现。
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?

1) First make sure, the key is passed correctly to Vim. To determine if
   this is the case, put Vim in Insert mode and then hit Ctrl-V (or
   Ctrl-Q if your Ctrl-V is remapped to the paste operation (e.g. on
   Windows if you are using the mswin.vim script file) followed by your
   key.

   If nothing appears in the buffer (and assuming that you have
   'showcmd' on, ^V remains displayed near the bottom right of the Vim
   screen), then Vim doesn't get your key correctly and there is nothing
   to be done, other than selecting a different key for your mapping or
   using GVim, which should recognise the key correctly.

这样您就可以检查您按下的home键是否与Vim理解为<Home>的相同。

另一种可能是其他映射正在干扰此映射。您可以尝试以下操作:

noremap <Home> 0w
inoremap <Home> <ESC>0wi

编辑:

问题似乎在于您的终端发送了一个 home 键码,但 Vim 没有将其识别为 <Home>

我认为最好的解决方案是让 Vim 正确地识别该键,这样您就可以在其他终端/系统上移动您的 .vimrc 而无需更改。

参考文献::h xterm-end-home-keys

  On some systems (at least on FreeBSD with XFree86 3.1.2) the codes that the
  <End> and <Home> keys send contain a <Nul> character.  To make these keys send
  the proper key code, add these lines to your ~/.Xdefaults file:

  *VT100.Translations:      #override \n\
        <Key>Home: string("0x1b") string("[7~") \n\
        <Key>End: string("0x1b") string("[8~")

如果那不起作用,您可以尝试:set t_kh=^V^[[1~。如果它有效,您可以将其包含在终端类型检查中。 更多信息可以在:h 终端选项中找到。
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?

:
:

3) If the key is seen, but not as itself and not as some recognizable
   key, then there is probably an error in the terminal library for the
   current terminal (termcap or terminfo database). In that case >

        :set term?

   will tell you which termcap or terminfo Vim is using. You can try to
   tell vim, what termcode to use in that terminal, by adding the
   following to your vimrc: >

        if &term == <termname>
            set <C-Right>=<keycode>
        endif

   where <termname> above should be replaced by the value of 'term'
   (with quotes around it) and <keycode> by what you get when hitting
   Ctrl-V followed by Ctrl-Right in Insert mode (with nothing around
   it). <C-Right> should be left as-is (9 characters). Don't forget that
   in a :set command, white space is not allowed between the equal sign
   and the value, and any space, double quote, vertical bar or backslash
   present as part of the value must be backslash-escaped.

   Now you should be able to see the keycode corresponding to the key
   and you can create a mapping for the key using the following command: >

        :map <C-Right>  <your_command_to_be_mapped>

For more information, read 

    :h map-keys-fails
    :h map-special-keys
    :h key-codes

1
谢谢你的回答。我按照所解释的方式操作,结果得到了以下内容:在插入模式下按Ctrl-Q => ^。按Home键后,我得到了=> ^[[1~。这意味着还有希望吗?我应该在vimrc中使用[[1~进行映射吗? - Ozkan
1
几乎 - ^[Esc。在输入地图命令时,您可以像上面那样插入序列,即在按下 Home 之前先按下 Ctrl-Q - Armali
@mMontu 我都试过了。我在我的主目录中创建了Xdefaults文件并添加了必要的行,但没有结果。我得到了之前相同的效果,没有改变。如果我按Ctrl-Q - Home,仍然得到^[[1~ - Ozkan
@Ozkan,尽管如此,您仍然收到了^[[1~,您是否检查过您的映射是否有效?我刚刚测试了一个终端,它不会显示Ctrl-q + Home<Home>,但是其与 <home> 的映射起作用。此外,您是否测试过我的答案中的noremap映射? - mMontu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接