Vim UTF8 characters in terminal - vim

So I am editing a UTF8 file in terminal vim and all of a sudden these weird characters come up when I press then navigation keys. So the image attached shows one such character being printed onto the screen after moving up from the end of the file. These guys are merely printed but never saved into the buffer. When that line moves out of view and then back, the character is not printed again. This is a completely random event and never seems to happen at the same location. One exception is I am at the end of the file and happen to press the down key repeatedly.
EDIT: New image for :Set term=cons25

These escape characters appear when Vim is confused about what key presses it receives from the terminal emulator. The arrow keys are received as Escape followed by a character from A to D:
^]0A is <up>,
^]0B is <down>,
^]0C is <right>
^]0D is <left>.
Editing an UTF-8 file has nothing to do with your issue.
Here is what I have in my /.vimrc to work around that problem:
nnoremap <Esc>A <up>
nnoremap <Esc>B <down>
nnoremap <Esc>C <right>
nnoremap <Esc>D <left>
inoremap <Esc>A <up>
inoremap <Esc>B <down>
inoremap <Esc>C <right>
inoremap <Esc>D <left>
I'm not aware of a better solution.

Related

Vim: Map Esc Without Affecting Terminal Control Characters

I'm trying to map <Esc> to turn off search highlighting in Vim. The problem is that keys simulated by the terminal with +Esc are affected.
The terminal sends characters much fast than I type. Is there perhaps a way to map key + timeout in vim?
The same question was asked 4 years ago and the answer was that it can't be done. Is this (still) true?
Mapping :nohlsearch to escape key
Your troubles are being caused by some plugin or other, native vim handles this fine. Start vim with vim --noplugin, or if that's not enough then bypass your vimrc with vim -u NONE (or gvim -U NONE) and :source this:
set nocp " life's too short for pure vi-compatibility mode
set timeout ttimeout " enable separate mapping and keycode timeouts
set timeoutlen=250 " mapping timeout 250ms (adjust for preference)
set ttimeoutlen=20 " keycode timeout 20ms
nno <ESC> :nohls<CR>
I've never seen and can't reproduce the interference you're describing so I don't know what's causing it, all I can suggest is binary search with your plugin set.
Yes, it's still not possible for the reason given by ZyX in his answer.
<Esc> is "special" because its behavior sits between a "normal" key like a (you can map it to whatever you want) and a modifier key (it's used by the terminal to represent a lot of special keys like <Up>).
Safely mapping <Esc> to do anything else/more than <Esc> is possible but you'll have to noremap all the affected keys. Here is what I have in my vimrc to mitigate that side effect:
nnoremap <Esc>A <up>
nnoremap <Esc>B <down>
nnoremap <Esc>C <right>
nnoremap <Esc>D <left>
inoremap <Esc>A <up>
inoremap <Esc>B <down>
inoremap <Esc>C <right>
inoremap <Esc>D <left>

What mode is vim in when I press a single 'r' and is it possible to have a mapping here?

For a quick overview, I map <s-space> to <esc> so I can more easily cancel out of things without moving my hand to the escape key. For example, when I want out of insert mode, or to cancel something.
inoremap <esc> <nop> " to force me to stop using <esc>
cnoremap <esc> <nop>
nnoremap <s-space> <nop>
onoremap <s-space> <esc>
inoremap <s-space> <esc>
However, if I press 'r' vim is waiting for a character, and when I press <s-space>, I end up replacing with a space, instead of canceling the replace operation. Is it possible for mappings to work after pressing 'r' once, while waiting for a character?
Thanks!
If you find you've accidentally pressed r, how about just tapping uu?
This will make the replacement but immediately undo it again, without you having to move your hands from the letter keys.

CTRL-K in Vim produces unexpected results

Hi I'm trying to optimise my window management in vim by mapping ctrlk to ctrl+w, k so i can just press ctrl+k to switch to the split window above the one I'm working in (I'm doing this for h,j and l also but it's only k that's causing the problem).
I've added this into my .vimrc
noremap <silent> <c-k> <C-W>k
noremap <silent> <c-j> <C-W>j
noremap <silent> <c-h> <C-W>h
noremap <silent> <c-l> <C-W>l
However if I press ctrl+k, then something weird happens. It changes depending on where I am in the document.
If I'm at the top of a document with many lines beneath my curser, the cursor hops down a few lines and columns into a completely different place.
If I'm at the bottom of a document, it creates loads of spaces from the cursor onwards.
I've tested and removing the above lines causes the symptoms to stop happening. I'm just really confused as to what is going on!
Some info: I'm using the vim binary that comes with macvim via the command line.
Any help would be greatly appreciated!
Thanks!
I can’t explain the second problem, but if you pasted everything directly from the vimrc then you have lots of trailing spaces that must not be there. It can explain the first problem. Try running
:%sm/\s\+$
then save and see whether problem disappears. If it is so, use
:set list listchars=trail:-
to be able to see trailing spaces so that you won’t run into this problem again.
Maybe <C-k> is already mapped to something else. Try :verbose map <C-k>.
Maybe your mapping is triggered in visual or operator mapping, where <c-w>k has a different meaning.
You could try this:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
, which will trigger only in normal mode.

How to disable Esc and cursor keys in vim

There is an opinion that when working in vim you should not use Esc key (use ctrl+c instead) and don't use arrow keys (use h,j,k,l) on you keyboard. But it is difficult to not to use those keys. I thought that there is a way to disable those keys in .vimrc so there will be no other option but to use ctrl+c and hjkl.
I've searched a bit and found a solution on this link.
So I've inserted the following in my .vimrc file:
inoremap <Up> <NOP>
inoremap <Down> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Esc> <NOP>
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
noremap <Esc> <NOP>
But this does not work. Adding this to my .vimrc breaks my mapping to the
function keys. The another problem is that it does not block the function of arrow keys rather when I press Down in normal mode multiple actions are performed - the cursor goes up one line, the new line is created and the character 'B' is inserted.
How can I disable in my vim 7.2 the cursor keys and Esc key without breaking anything else?
If you're using vim in a terminal you should absolutely not remap Escape. Because of the way keys are handled in vim (and probably terminals in general), remapping it will break all kinds of keys you didn't intend on changing. To see what I mean, do the following.
Open up vim with no startup files: vim -u NONE --noplugin -N.
Enter insert mode.
Press Ctrl-v followed by any of the function keys, such as <F2>.
Notice the sequence that is entered. It very likely begins with ^[ which is a literal Escape.
Now open try the following:
:inoremap <esc> NO ESCAPE FOR YOU
Enter insert mode.
Press any of the function keys, like <F2>.
If the previous sequence showed the escape character as part of the <F2> key press, you'll now see our new string printed to the screen. In fact, now that you have the mapping, try to move around using the cursor keys. You'll probably notice the same bizarre behavior.
In conclusion, don't remap escape, I almost guarantee you will have unexpected consequences.
Here's a non-geeky way of achieving what you want: Crumple pieces of paper to the size of your thumb and tape them to the keys. The moment your finger tries to reach them you'll bump into the paper instead. They'll become a good reminder. Keep them taped there until you stopped bumping into them.
What you had was close:
inoremap <esc> <NOP>
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
nnoremap <Left> <NOP>
nnoremap <Right> <NOP>
nnoremap <Up> <NOP>
nnoremap <Down> <NOP>
This line was causing you trouble:
noremap <Esc> <NOP>

Navigating in Vim's Command Mode

I am a long time emacs user learning Vim. Emacs lets me navigate in the mini-buffer (where I issue commands like C-x C-s) using the same navigation keyboard shortcuts as in any other buffer. For example, I can navigate forward one character using C-f, even while in the mini-buffer. I could also use the arrow keys, but they are too far away.
Is there any keyboard shortcut to navigate in Vim's command mode (:), without using the arrow keys -- equivalent to emacs C-f, C-b? Thanks.
Adding to Greg Hewgill's answer, you can use q: to open the command-line window, where you have any Vim editing power at your hand.
Some from the Vim help:
CTRL-B or <Home>
cursor to beginning of command-line
CTRL-E or <End>
cursor to end of command-line
CTRL-H
<BS> Delete the character in front of the cursor (see |:fixdel| if
your <BS> key does not do what you want).
<Del> Delete the character under the cursor (at end of line:
character before the cursor).
CTRL-W Delete the |word| before the cursor. This depends on the
'iskeyword' option.
CTRL-U Remove all characters between the cursor position and
the beginning of the line.
I have these in my .vimrc
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
With the default key bindings, vim does not offer non-arrow-key navigation of the command line editing. However, see :help cmdline-editing for an example of how to use the :cnoremap command to set up alternate key bindings.
I achieved that with <C-p> and <C-n> to navigate previous and next commands respectively.
P.S I'm not making any custom binding like Tassos did.

Resources