How can I change the keybinding used to autocomplete in vim? - vim

I'm not a big fan of Ctrl-n, I'd like to be able to use Ctrl-Space. Any ideas how I can do that?

if has("gui_running")
" C-Space seems to work under gVim on both Linux and win32
inoremap <C-Space> <C-n>
else " no gui
if has("unix")
inoremap <Nul> <C-n>
else
" I have no idea of the name of Ctrl-Space elsewhere
endif
endif

I like the Ctrl+Space mapping as well.
" Remap code completion to Ctrl+Space {{{2
inoremap <Nul> <C-x><C-o>
In your case you want:
inoremap <Nul> <C-n>

If you wanted to map it to, say, Ctrl-E, it'd be something like:
inoremap <C-E> <C-N>
The problem with Ctrl-Space is that most terminals will just see it as a space. I'll assume you're using some terminal program inside X; if you're using something different, you'll have to supply the relevant substitutions yourself.
Bash's readline normally has Ctrl-V mapped to "treat the next key as literal." So pressing Ctrl-V then Home at a bash command line will insert ^[[H or something like that on the command line, rather than going to the start of the line. Try pressing Ctrl-V then Ctrl-Space. Probably you'll just see a space.
In that case, you'll have to fool with xmodmap or write your own /usr/share/X11/xkb/* files to tell X to output something different when you press Ctrl-Space. Programs like Firefox don't care; they detect which base key is being pressed and figure out for themselves what modifiers are being pressed. But most terminal-based programs will just see Ctrl-Space as a Space unless you tell X to treat Space and Ctrl-Space differently.
I doubt you can make this change with xmodmap alone; you'll probably need to do the lower-level /usr/share/X11/xkb/* hacking. This is complicated, and I don't even know if you're using X in the first place, so I'll just leave it there.
Some terminals like urxvt let you specify your own keybindings. Like Firefox, they can tell when it's a Space and when it's a Control-Space even without you doing anything special to configure X. So you could tell urxvt to output "\033I_TYPED_CONTROL_SPACE_DAMMIT" when you press Ctrl-Space. And then you could tell vim to map that to <C-N>.
EDIT: I forgot that Ctrl-Space used to output \0 (I remapped that somewhere else on my keyboard). In that case, all the complexity I described above is unnecessary. What I said would apply to someone who wanted to use a more exotic mapping, like Ctrl-colon or Alt-Space.

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.

Can't map function keys with shift in Vim [duplicate]

I am trying to customize the behaviour of Enter key in Normal mode in Vim.
Here is my .vimrc:
nmap <CR> o<Esc>
nmap <S-CR> i<CR><Esc>
I am trying to make Enter simply append a newline after the current line when pressed. However, when the Shift-Enter combination is pressed, I want to break the current line at the cursor.
The latter does not work. Whenever I press Shift-Enter it just appends a line without breaking it at the cursor.
I managed to correct my terminal key-code for Shift+Enter
by sending the key-code Vim apparently expects. Depending on your terminal,
(Adding Ctrl+Enter as a bonus!)
iTerm2
For a single Profile open Preferences → Profiles → Keys → [+] (Add)
For all profiles open Preferences →
Keys → [+] (Add)
Keyboard shortcut: (Hit Shift+Enter)
Action: Send Escape Sequence
Esc+ [13;2u
Repeat for Ctrl+Enter, with sequence: [13;5u
urxvt, append to your .Xresources file:
URxvt.keysym.S-Return: \033[13;2u
URxvt.keysym.C-Return: \033[13;5u
Alacritty, under key_bindings, add following to your ~/.config/alacritty/alacritty.yml:
- { key: Return, mods: Shift, chars: "\x1b[13;2u" }
- { key: Return, mods: Control, chars: "\x1b[13;5u" }
Kitty, in ~/.config/kitty/kitty.conf:
map shift+enter send_text all \x1b[13;2u
map ctrl+enter send_text all \x1b[13;5u
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today in the terminal version of Vim (<S-CR> should work in GVIM on all platforms, and in the Windows console Vim). This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
Note on mapping <CR>:
If you map <CR> in normal mode, it'll interfere with selection of history items in the command-line window and with jumping to error under cursor in quickfix/location list windows! (Unless you add the following:)
:autocmd CmdwinEnter * nnoremap <CR> <CR>
:autocmd BufReadPost quickfix nnoremap <CR> <CR>
Note on :nmap:
You should use :noremap; it makes the mapping immune to remapping and recursion.
I also wanted to map <S-CR> and found that I couldn't get it to work in CLI mode until I added a second mapping using Ctrl+V then <Shift+Enter> for the mapped keystroke. The <S-CR> mapping is still needed for GVIm to work as expected, however. This would render the your .vimrc snippet as follows:
nnoremap <CR> o<Esc>
nnoremap <S-CR> i<CR><Esc> " Needed for GVIm
nnoremap ^[0M i<CR><Esc> " Needed for CLI VIm (Note: ^[0M was created with Ctrl+V Shift+Enter, don't type it directly)
I tested this on Ubuntu 12.04. Happy Vimming!
You can't map <S-CR> in CLI Vim, no matter how hard you try, because Vim can't distinguish <S-CR> from <CR>.
You must find another mapping or stick with GVim/MacVim.
edit
Some terminal emulators, like iTerm.app or Terminal.app on Mac OS X, allow you to set up shortcuts to send specific characters sequences to the shell. If you have that possibility it may be worth a try but you'll quickly get used to a platform-specific gyzmo that can't be ported so, well… I don't really recommend it.
Ingo Karkat and romainl are 100% correct. However what you are asking is common so I want to give you some options.
I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.
If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:
nnoremap <silent> [<space> :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space> :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>

Include all default binds in vimrc?

Maybe this goes against the traditional zen of VI, but I get very overwhelmed when it comes to working with rebinding keys in Vi, in particular what can/cannot be bound. I've had a particular headache since moving to nvim with selecting/copying/pasting using visual mode, as well as no longer having access to Home/End to jump to beginning/end of a line while in insert mode.
I was thinking that a way around this, much like I do with my tmux config, is issue a command to unbind all keys in my vimrc, then rebind them all. That way, there's no confusion to what a binding is, and I can easy change and reference a change anytime. Am I going about this all wrong?
You can't really do this. Vim treats "built-in mappings" different from "user-defined mappings". You can't really "unmap" a built-in mapping.
Technically, it stores built-in mappings in a different C struct than user-defined mappings defined with :map. When you do :unmap, it just removes it from the user-defined struct (incidentally, I gave a brief − but incomplete − overview of how this works last week in How to find out what a key is mapped to? at vi.SE).
The only thing you can do is something like:
" Remap all ascii characters; everything below 33 is a control character
for i in range(33, 127)
" | needs some extra love
if i == 124 | continue | endif
execute 'nnoremap ' . nr2char(i) . ' <Nop>'
execute 'nnoremap <C-' . nr2char(i) . '> <Nop>'
endfor
" The above won't remap stuff like `<F1>`, `<Up>`
nnoremap <Bar> <Nop>
nnoremap <F1> <Nop>
nnoremap <Up> <Nop>
" ... etc ... You can use a loop for this as well...
" Now make our own mappings
nnoremap : :
" ...etc...
And the same for vnoremap, etc. but this won't remap <C-w><C-w>, gJ, etc. so you'll need to add even more exceptions for that (the "second key" for these mappings isn't even in a struct, but is a switch/case!)
However. don't do this. Because now rely on your vimrc that you and only you can understand. Just learn the default mappings. This will mean you can use any Vim installation out-of-the-box, and your mappings won't be somehow "better" than the default mappings.
You can put this line near the top of your vimrc to reset all options to their default value:
set all&
But you can't realistically hope to "unbind" the "default bindings" with a single command because:
they are not "bindings", they are "commands",
there's no such command anyway.
You could remap every default command to <nop> (:help <nop>) but that sounds like a lot of work for very little benefit.

How to map Shift-Enter

I am trying to customize the behaviour of Enter key in Normal mode in Vim.
Here is my .vimrc:
nmap <CR> o<Esc>
nmap <S-CR> i<CR><Esc>
I am trying to make Enter simply append a newline after the current line when pressed. However, when the Shift-Enter combination is pressed, I want to break the current line at the cursor.
The latter does not work. Whenever I press Shift-Enter it just appends a line without breaking it at the cursor.
I managed to correct my terminal key-code for Shift+Enter
by sending the key-code Vim apparently expects. Depending on your terminal,
(Adding Ctrl+Enter as a bonus!)
iTerm2
For a single Profile open Preferences → Profiles → Keys → [+] (Add)
For all profiles open Preferences →
Keys → [+] (Add)
Keyboard shortcut: (Hit Shift+Enter)
Action: Send Escape Sequence
Esc+ [13;2u
Repeat for Ctrl+Enter, with sequence: [13;5u
urxvt, append to your .Xresources file:
URxvt.keysym.S-Return: \033[13;2u
URxvt.keysym.C-Return: \033[13;5u
Alacritty, under key_bindings, add following to your ~/.config/alacritty/alacritty.yml:
- { key: Return, mods: Shift, chars: "\x1b[13;2u" }
- { key: Return, mods: Control, chars: "\x1b[13;5u" }
Kitty, in ~/.config/kitty/kitty.conf:
map shift+enter send_text all \x1b[13;2u
map ctrl+enter send_text all \x1b[13;5u
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today in the terminal version of Vim (<S-CR> should work in GVIM on all platforms, and in the Windows console Vim). This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
Note on mapping <CR>:
If you map <CR> in normal mode, it'll interfere with selection of history items in the command-line window and with jumping to error under cursor in quickfix/location list windows! (Unless you add the following:)
:autocmd CmdwinEnter * nnoremap <CR> <CR>
:autocmd BufReadPost quickfix nnoremap <CR> <CR>
Note on :nmap:
You should use :noremap; it makes the mapping immune to remapping and recursion.
I also wanted to map <S-CR> and found that I couldn't get it to work in CLI mode until I added a second mapping using Ctrl+V then <Shift+Enter> for the mapped keystroke. The <S-CR> mapping is still needed for GVIm to work as expected, however. This would render the your .vimrc snippet as follows:
nnoremap <CR> o<Esc>
nnoremap <S-CR> i<CR><Esc> " Needed for GVIm
nnoremap ^[0M i<CR><Esc> " Needed for CLI VIm (Note: ^[0M was created with Ctrl+V Shift+Enter, don't type it directly)
I tested this on Ubuntu 12.04. Happy Vimming!
You can't map <S-CR> in CLI Vim, no matter how hard you try, because Vim can't distinguish <S-CR> from <CR>.
You must find another mapping or stick with GVim/MacVim.
edit
Some terminal emulators, like iTerm.app or Terminal.app on Mac OS X, allow you to set up shortcuts to send specific characters sequences to the shell. If you have that possibility it may be worth a try but you'll quickly get used to a platform-specific gyzmo that can't be ported so, well… I don't really recommend it.
Ingo Karkat and romainl are 100% correct. However what you are asking is common so I want to give you some options.
I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.
If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:
nnoremap <silent> [<space> :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space> :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>

Mapping :nohlsearch to escape key

I like to use hlsearch but I hate to keep everything highlighted after searching, to solve this problem I could simply use :nohlsearch or an abbreviation of it but that is still to much effort so I decided to try to do that on pressing escape. What I came up with is:
nnoremap <ESC> :nohlsearch<CR>
This works exactly as I want it to in GVim which I usually use for development but it does not work in vim.
If I search something in vim, press escape to deactivate the highlighting and use one of the arrow keys to navigate vim goes directly into insert mode and inserts a character on a new line.
As I never really came around to using h, j, k and l for navigation this is really annoying and I would like to know how I can make vim behave like gvim.
If you need more information you can find my entire vim configuration here.
Your problem is that when you press <Up> terminal sends something like <Esc>OA (you will see it if you type <C-v><Up> in insert mode) which is remapped to :nohlsearch<CR>OA. I do not know any solution except not mapping a single <Esc>, try either mapping to double <Esc>.
I created this map to disable search when press double <Esc>
nnoremap <silent> <Esc><Esc> :let #/ = ""<CR>
is :noh still too much work?
EDIT: I don't know about you, but I personally think :noh is easier than pressing Esc key, since I can press all the buttons without stretching my pinky too far (which is why I think the default mapping of Esc for going back to Command Mode from Insert Mode is a bit unfortunate). If you really use the :nohlsearch that much, you probably should remap it to something you can reach from the Home Area (i.e. regular letters, or numbers, or perhaps Ctrl-letters).
Anyway, typing the exact command you give works in my vim (on gnome-terminal). Are you sure you put the rule in .vimrc file, instead of .gvimrc? No luck after restarting vim? Try :source /path/to/config/file and see if that makes it to work.

Resources