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

11

4

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?

Owen

Posted 2011-05-02T19:48:49.857

Reputation: 701

Answers

7

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.

peth

Posted 2011-05-02T19:48:49.857

Reputation: 7 990

This is an interesting idea. I will give this a shot and see if I learn anything. – Owen – 2011-05-03T22:53:46.093

3

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.

Gaff

Posted 2011-05-02T19:48:49.857

Reputation: 16 863

2I don't think this applies to normal mode commands only to ex mode commands (that is commands that start with ':', example :e :w :q) – kmkkmk – 2011-05-03T13:52:35.547

1To see the ex commands history, you can also simply type q: in normal mode. same goes for search history with q/ – Yab – 2011-05-03T16:08:09.257

2

You could add this to your .vimrc :

set showcmd

It will show in the status bar the command you are making as you type them. While this won't give you an history, this is the closest solution I know of.

If however you are talking about ex commands, Gareth's awnser is the way to go

Yab

Posted 2011-05-02T19:48:49.857

Reputation: 2 363