69

Is there a command which shows what was the last command in normal mode?

Suppose I accidently hit random key and got some unexpected result. Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?

dvs
  • 816
  • 1
  • 6
  • 5

5 Answers5

96

Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.

Roland Ewald
  • 4,630
  • 3
  • 35
  • 49
user3620332
  • 979
  • 6
  • 3
  • 5
    This didn't work for me for some reason. Turned out history was set to 0. The solution was to run `:set history=100` and then I was able to us the up arrow to view previous commands. – Jim Aug 11 '16 at 15:00
  • 1
    I've been trying for years to figure out how to get OUT of this mode. Esc doesn't work. It seems : sometimes works, but not always. I always get stuck in this mode. – devios1 Feb 28 '18 at 16:54
  • 5
    while (:) and up/down arrows show command history, (/) up/down arrows show search patterns history. I found that very useful. – Jack Chan Nov 11 '19 at 22:22
  • @devios1: Did you solve it now? It must work, what's the details of your problem? – NeoZoom.lua Apr 03 '21 at 23:16
79

q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.

mtraceur
  • 3,254
  • 24
  • 33
Sushil
  • 1,188
  • 12
  • 19
  • 31
    So _that's_ why this feature randomly pops up from time to time. (It's the reverse of `:q`). I wouldn't have known it existed to go looking for it otherwise. – felwithe Jun 08 '17 at 03:57
  • 5
    I *always* get stuck in this mode. How do you get out of it? Esc doesn't work and that really trips me up. – devios1 Feb 28 '18 at 16:56
  • @devios1: Vim is written by demon. – NeoZoom.lua Apr 03 '21 at 23:18
  • 1
    @devios1 `:q` will get you out of that mode. I know, very unintuitive and not something we're ever likely to try, because we're so used to `:q` qutting the editor itself, and it's not obvious that this mode is actually a separate window/split in vim which will intercept the next `:q`. – mtraceur Jul 03 '21 at 05:51
15

The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.

To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).

Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.

But none of these actually answer your question. I'm curious if it can be done.

RunHolt
  • 1,892
  • 2
  • 19
  • 26
14

Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.

This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.

jbeku
  • 141
  • 1
  • 4
8

It is difficult to know it. You can play with the variables:

v:operator
v:count (and v:prevcount)
v:register

But you cannot fully get the last normal mode command issued.

However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.

If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.

On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.

Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.

Community
  • 1
  • 1
Benoit
  • 76,634
  • 23
  • 210
  • 236