Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't map Home button in vimrc

Tags:

vim

I want to map the Home button so vim goes to the first non blank character in vim. But mapping the home button doesn't do anything? If I map another key, then it works correctly.

See below my vimrc file:

map <Home> 0w
imap <Home> <ESC>0wi

The above doesn't work. While the following works (Ctrl-F for example)

map <C-f> 0w
imap <C-f> <ESC>0wi

Isn't there a way to map Home key to this? I really need it, because I got used to this when working with Notepad++, Sublime text 2, Visual Studio,...

I also tried the following, with no results. When using another key, it works again... http://vim.wikia.com/wiki/Smart_home

like image 726
Ozkan Avatar asked Jan 13 '23 05:01

Ozkan


1 Answers

From Vim FAQ (also available through this nice plugin):

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.

This way you can check if the home key you are pressing is the same that Vim understand as <Home>.

Another possibility is that some other mapping is interfering with this one. You could try the following:

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

Edit:

It seems the problem is that your terminal is sending a home keycode that Vim isn't recognizing as <Home>.

I believe that the best solution is make Vim recognize that key correctly, so you can move your .vimrc to other terminals/systems without changes.

From :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~")

If that doesn't work, you could try :set t_kh=^V^[[1~. If it work you can enclose it on a check of your terminal type. Additional information can be found at :h terminal options


Edit 2:

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
like image 77
mMontu Avatar answered Jan 21 '23 12:01

mMontu