In VIM, can I find out what keys I just typed?

Sometimes I'll be using VIM and something will happen, and I don't know what it was or what I typed that caused it. Is there a way to list some of the recent keys that were typed, and, even better, find out what actions they triggered?


Solution 1:

Vim has an option -W you could abuse, but it won't work while Vim is still running.

-w {scriptout}

All the characters that you type are recorded in the file {scriptout}, until you exit Vim. This is useful if you want to create a script file to be used with "vim -s" or ":source!". If the {scriptout} file exists, characters are appended.

-W {scriptout}

Like -w, but an existing file is overwritten.

Calling Vim with e.g. an alias

vim -W /tmp/vimlog-$(id -un)

will let you inspect with less /tmp/vimlog-$(id -un) or cat -v /tmp/vimlog-$(id -un) what you literally typed after you quit Vim.

Solution 2:

First make sure that vim is remembering any lines of history at all. This sets the history to 1000 commands and searches:

:set history=1000

If you type the start of what you are looking for you can use the keys to scroll through the history - this applies to commands and searches.

For example, if you had searched for china then cuba then Chad then cyprus:

You could type /c and press several times. You will find it displays /cyprus then /cuba then /china (/Chad is skipped because it doesn't start with a c).

Other commands:

  • :history lists the entire history.
  • :his lists the command history.
  • :his / lists the search history.