Can't map Home button in vimrc - 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

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

Related

VIM: set term=xterm changes <BS> to <Del>, is it reversible?

I have added set term=xterm to my vimrc to be able to use 256-color vim schemes in terminal, but it comes at a price (at least for me). It also replaces (sort of) the BackSpace with Delete (i.e. BackSpace starts to delete forward just like Delete does) in insert mode and makes it "BackSpace" instead of moving left (or h) in normal mode. I have nothing against Ctrl-H as a way "to Backspace", but I simply don't want to have two delete buttons and ability "to BackSpace" (delete backward) in normal mode.
How can I reverse that change while retaining the setting I need?
PS I've read :h CTRL-h and a bit of :h xterm, but I couldn't find the solution.
Vim's inoremap and nnoremap commands can be used to adjust how keys are interpreted in Vim.
A solution is documented here for your specific context: https://conemu.github.io/en/VimXterm.html
The relevant quote:
"If you have problems with BS in Vim (BS acts like Delete key) under ConEmu when term=xterm, you may try to remap BS key:
inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>
"
In general, when a key does not do what you want, the trick is to find out what it actually sends to Vim. Sometimes hitting Ctrl-V followed by that key in insert mode might help figure it out. Then inoremap and nnoremap as shown above can be used to reassign it to the behaviour you want in insert and normal modes, respectively.

How do I change cursor key behaviour in the command line in neovim?

Currently, when I press cursor-left in the command line in neovim (:whatever foo bar), the cursor will move over a whole word. Most of the time, I just want it to move one character.
This shouldn't be the default behaviour. It's probably a plugin or something you once added to your vimrc at some point. Try using :verbose cmap to see what it's set to (also see How do I debug my vimrc file?).
You should also be able to use :cnoremap <Left> <Left> to restore the default behaviour.
This has to do with putty and how it sends cursor keys. The trick is to find out what nvim actually sees, and then :cnoremap... accordingly.
The way to figure out what your nvim receives, is this:
enter input mode
hit ctrl-v
hit the key you want to see
Be aware that nvim behaves slightly differently than vim here: vim shows you the actual escape sequence, while nvim shows you the translated keys. The latter is a tad unfortunate.
In my case, for reasons I haven't figured out, nvim saw Ctrl-Left as , but Left as . Since I never need S-Left in the command line, :cnoremap <S-Left> <Left> did the trick.

Remap CTRL-A in command line mode

How can I remap my CTRL-A to CTRL-Q in the command line mode?
I use VIM within Tmux session (which has CTRL-A set as a prefix). When I try to insert all files which names match the pattern (i.e. for :bd command) I have to press CTRL-A twice (as it's also a prefix for tmux). I would like to use one key stroke <C-a> for that.
I've tried to put cnoremap <C-q> <C-a> to my .vimrc but it doesn't work. Any suggestions?
This works for me; note that in the terminal, Ctrl-S and Ctrl-Q are special sequences for flow control. See here for how to unconfigure that, or use another left-hand side for your mapping, e.g. <C-g>.
Thank you guys for letting me know that is special key, I wasn't aware of that.
In that case I decided to use different key (<C-b>). Key map for that:
cnoremap <C-b> <C-a>
I know that <C-b> is for scrolling but I guess in command line mode it should be ok to use that key.

Stop inserting hotkey combinations after meta/leader key

On MacVim I have a rather annoying occurrence that I don't have any solutions for yet.
While in insert mode, if I type , (my leader key), then hit CMD-S in an attempt to save, it ends up inserting <t_<fa>X> after the comma.
I don't know why or what that text is, but is there any way to stop it from being inserted?
I found your gvimrc here on github
In the first section you have a bunch of mapping. The last couple of lines are shown below.
if has("gui_macvim")
...
" Adjust viewports to the same size
map <Leader>= <C-w>=
imap <Leader>= <Esc> <C-w>=
endif
The problem is this mapping
imap <Leader>= <Esc> <C-w>=
Macvim takes your <leader> key and waits to see if the next one if =. It doesn't preform the mapping and since the mapping was incorrect it insert the characters into the buffer. It seems MacVim prints <d-s> as <t_<fa>X>. So <t_<fa>X> gets put into the buffer what <CMD-S> is pressed.
To stop this you should not have any mappings in insert mode that use your leader key because there will be a short time after it where MacVim is waiting for another character to determine if your are typing a mapping or trying to insert something.
Edit: Kent also found a link to a MacVim issue tracker issue 388 that describes this issue.

How do I get Ctrl-Backspace to delete a word in vim within gnome-terminal?

I'd like Ctrl-Backspace to delete the current word in vim insert mode. From within xterm I can pull this off via
:inoremap <C-H> <C-W>
but in gnome-terminal I cannot figure out a way to make it happen.
When in vim insert mode, if I type control-v and then press backspace, I get ^H in xterm, and ^? in gnome-terminal. Unfortunately,
:inoremap <C-?> <C-W>
doesn't do the trick in gnome-terminal; control-backspace just erases a single character no matter what.
Regarding ASCII codes:
Gnome-terminal lets you change the backspace character under Edit -> Profile Preferences -> Compatibility. Unfortunately, no option works, as far as I can tell: whatever character I apply to Backspace via the settings, if I try mapping the character itself, like
:inoremap <C-H> <C-W>
then regular backspace and control-backspace both erase an entire word; and if I try mapping control plus that character, like
:inoremap <C-^H> <C-W>
then regular backspace and control-backspace just erase a single character.
gnome-terminal's libvte would need to be patched.
libvte already has several options to map backspace, none of which distinguish Ctrl-backspace. It needs an option that does, maybe one that follows the behaviour of the linux console (^? for backspace, ^H for Ctrl-backspace). See this gnome bug.
2015 update: this was fixed in 23c7cd0f99d504cbab06d4c27254d4f3e2807ba8.
libvte 0.41.90, 0.40.3 and newer have the fix.

Resources