vi randomly echoing key presses - vim

When using vi it sometimes echoes keypresses to the screen. For example, if I press escape, it will show ^[ on the screen. The actual characters don't get written to the file, because if I go to another place in the file and come back, the characters aren't there. Still, it's confusing when this happens, especially if the characters appear where there's text.
I also have gvim installed. It never happens there, but it happens in all the various types of vi that run in a terminal.
Why does this happen, and how can it be fixed?

It's okay when you are working on slow connection.
It also happened to me, when I used vim/vi on remote machine over slow connection. It's because the slow connection extends the internal speed vim responds to your keystrokes. For me, nothing harmful but it's slow, and gives exception to eyes. At least VI is designed in this kind of condition, it can handle slow connection very well.

This doesn't really help you fix the problem, but if it's just noise in the terminal, you could use the :redr[aw]! command to clean up your screen.

Related

Vim motion keys sometimes appear on screen when using tmux

When I'm not in insert mode and I'm navigating around my file with the motion keys (hjkl etc), the keys often appear in the buffer (illustrated below). I can observe this effect pretty readily in tmux, but it doesn't seem to happen without. If I run :!reset in vim the buffer returns to normal, but it doesn't stay that way for long. I'm not sure why this is happening and google yielded nothing. Halp?
Edit: It seems just a simple :e will fix the problem, too.
I experience the same thing. A few extra observations:
it only seems to happen when I hold down a motion key like j or k, causing rapid repeats
it happens both in and outside of tmux, but much more often in tmux
it happens much more often since I enabled vim-go's go_auto_sameids, which I believe is making a call to an external program (guru) for every cursor movement
This leads me to assume that it's a timing issue in vim that is exacerbated by latency added by things like tmux and/or plugins.
(Using gnome terminal; $TERM is screen-256color in tmux and xterm-256color outside)
I think this was alluded to on the tmux mailing list recently. Try decreasing the value of the escape-time parameter in tmux, so that tmux more quickly sends the escape character to vim to return to command mode, rather than waiting for more characters in an escape sequence. The default value is 500ms, so try something smaller, like 50.
In .tmux.conf:
set-option -g escape-time 50

Can vim command history store commands that produce an error?

I'm new to vim (for the seventh time) so this may not be something that makes sense for vim style but:
The command history in the command window has up-arrow mapped to history and this has been very helpful in learning, except that it does not seem to store commands that fail to execute in the history. Obviously, this is a bad configuration choice for someone trying to learn from mistakes. Is there a way to force it to remember the failed commands, in the sense that they can be recalled with the up-arrow?
FYI, I am using MacVim with Janus extensions.
What kind of error are you talking about? :s/foo/bar is always recorded into history, even if there was no foo on the current line and you get E486.
As a side note, if you use Ex commands a lot you might be interested in the Command line window, invoked by q:, that you can navigate and edit just like any other window. It's really great.
Also, drop Janus as soon as possible! That pile of crap is the absolute worst thing to install when learning to use Vim. Seriously.

Moving the cursor around in vi produces unexpected characters. How come?

I took a couple screenshots of the problem I'm having. Basically what I'm used to with vi and what I'm expecting, is to be able to move around the document using the arrow keys, and still be able to read the actual content of the document.
Here is a shot of vi editor as I initially open a configuration file.
A shot of vi again, after I have pressed the down, left, and right arrow keys a few times.
If for some reason you cannot view the screenshots, what I see is: as I move the cursor around in vi, the place where my cursor just was gets replaced by numbers, commas, dashes, or curly braces. Sometimes it jumps one character over, but sometimes it jumps several.
Obviously I'm doing something wrong here. And here I thought I knew how to use vi...
I'm not exactly why the problem occured, but I do know what fixed it.
Solution:
As you can tell from the screenshot, I was in an SSH session. I disconnected and re-established a new SSH connection.
Please try runnning vi with different terminal emulation to see if it makes difference. I reckon its terminal related.
Something like:
TERM=linux vi
or
TERM=vt100 vi
Also running 'reset' command might also help without reconnecting. This resets terminal to its defaults.

vim scrolling slow on tmux over ssh

I have recently been trying out tmux on my server as a replacement for byobu (screen). When I started editing some code on vim, I found it very laggy when I held on to j and k to scroll up and down.
I thought it might be because there was something wrong with my connection but I tried editing the same file on vim without tmux and I found it much more responsive with almost no lag.
So, is there anything I can do to make vim on tmux not lag?
It sounds like you are using tmux over ssh. If so, you may want to give mosh a try. It is built on top of ssh but with many advantages over ssh. One of them is:
Get rid of network lag.
SSH waits for the server's reply before showing you your own typing. That can make for a lousy user interface. Mosh is different: it gives an instant response to typing, deleting, and line editing. It does this adaptively and works even in full-screen programs like emacs and vim. On a bad connection, outstanding predictions are underlined so you won't be misled.
It worked much better than over ssh in my experience.
I often work on a remote machine, and I have the same vim scrolling problem. When you use vim inside tmux and you have multiple panes open at the same time, tmux can't just redraw the part of the screen where vim is running, but it has to redraw a much bigger portion (potentially the entire screen). This increases the number of escape characters that tmux is sending through your connection.
In my case, enabling ssh compression reduced this kind of slowness quite a lot. You can give it a try with:
ssh -C user#host
or put Compression yes into your ~/.ssh/config to make it permanent.

How to see the last few keystrokes in vim?

Sometimes I hit the wrong keys on my laptop's small keyboard, and odd navigation or editing occurs (sometimes a feature that's new to me).
Although I can undo the editing (u) or navigation (control-o), I don't know what I did wrong, so it's difficult to avoid it in future. It's also frustrating to not know what just happened. So, I would like to be able to see my last few keystrokes.
A bit like :set showcmd, but to show the literal keystrokes, and (ideally) a short history of them.
I've only found commandline and navigation history in help/google.
To clarify: I'm not looking for commandline history, but keystroke history. e.g. did I press ) accidentally and go to the next sentence? Did I press dd and delete a line? I guess it's similar to a keystroke logger.
Commandline history (:history) only shows commands entered at the : prompt.
A bit old, but I've just found this on reddit:
You can start vim with the -w flag to write all keystrokes in a file. See :h -w
vim -w filename
To my knowledge, the closest you can get is the q command, which records your keystrokes into a register of your choice. Obviously, that has to be set up a priori as it's intended for complex repeats, although you could probably hack something to start recording on every file open. There's the matter of memory usage and that annoying "recording" prompt though.
Probably the most straightforward way would be to install keylogging software. I don't have any personal experience with these, but the security implications are probably mild if you get it from a trustworthy source or build it yourself, set it to only log to memory not a disk, only have a buffer the size of a sentence or so, or only log for vim windows.
More recently I came across a logging plugin for Vim and it reminded me of this question:
http://wolever.net/~wolever/wiki/vim-logging
It basically records everything you do for later analysis, it was intended for getting statistics about command usage but should work perfectly for finding what those magic commands you accidentally entered were.
:history will show your command history - that should help you uncover the new features that we all uncover in vim with misplaced keystrokes.
Gundo - Is probably worth a look, whilst perhaps not exactly what you are looking it will help slightly.
It lets you visualise the Vim undo tree, this means you will be able to see the last edits that happened. For example if you accidentally deleted a line or some such this will show up in the tree, however, it doesn't show you the actual keystrokes that were pushed and will not show things such as cursor movements.

Resources