41

I know that Vim keeps history for ex commands, searches, jump locations, changes, and possibly others.

With showcmd set, Vim also shows you what keys you have entered so far for a normal mode command, until that command is executed.

Sometimes, when in normal mode, I mistype and actions occur. I can undo these actions, but I sometimes want to know how the edits during my blunder occurred. Sometimes the edit looks cool and I want to know how to repeat it in the future.

So, is there any way of seeing the history of key presses in normal mode, preferably delimited when an actual edit or action (like movement or mode change) occurred?

1

2 Answers 2

23

Following the link from @WChargin a bit further, I found this:

You can start vim with the -w or -W option as follows:

vim -w keys.txt my_file

All the characters that you type are recorded in the file {scriptout}, until you exit Vim.

-w will append to the specified file if it exists; -W will overwrite it.

It doesn't quite get you what you want, but it's the right direction.

Update: It looks like vim actually buffers all the keystrokes until you quit vim, but there is a one-line patch here that will write the keystrokes immediately.

6
  • Thanks! I'm using this as a starting point for poking into the source code. It looks like -w saves every single key press, not just normal-mode commands, but you are correct: it is the right direction!
    – John O'M.
    Feb 8, 2015 at 3:02
  • @xthrd, how are these keys.txt file formatted/structured? When I use -w, appending is always happening in a long-long-long line; and, there are weird characters displayed: under UTF-8, whenever I open a file in a new buffer, I will get €齛. This is a good marker by itself, but it would be great if the logs could be parsed optionally.
    – llinfeng
    Nov 19, 2017 at 15:02
  • @llinfeng It's not formatted at all. It's literally just a file containing the keystrokes in the order they were made. (Although note that if some of the keystrokes are <cr>, then the file will contain linebreaks.) This is because the main purpose is for creating files that can be played back with the -s option. But why not try it out for yourself!
    – Rich
    Nov 28, 2017 at 12:24
  • @Rich Thank you for clarifying. I have used this key-tracking mechanism to debug the problem that started my search. [My lesson is that: never map W for the command line.]
    – llinfeng
    Nov 28, 2017 at 16:50
  • @llinfeng Why not? Mar 8, 2022 at 23:19
-4

There is q: which shows a command history.

I also find it useful that you can press up in a partially completed command to cycle through matches. E.g. /abc Then pressing up will cycle through your history of searches starting with 'abc'

2
  • 5
    q: shows ex command history, and is useful, but is not a log of normal commands, like "ad2tn (delete into register a until the second n)
    – John O'M.
    Feb 7, 2015 at 17:37
  • Don't forget that while in the middle of editing a command you can bring up current line in the history editor and use vi to edit the line by pressing Ctrl+f
    – Sukima
    Feb 19, 2015 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.