Hendrik
Hendrik

Reputation: 4929

Re-execute command by :history option in VIM

How can you execute a command again listed in the

:history

option in vim. There are numbers shown. Is the only way to copy the command by hand, then re-enter it? Or is there something like in shell script.

Upvotes: 8

Views: 5443

Answers (4)

Jonathan
Jonathan

Reputation: 343

If Vim is compiled with +eval you can use histget( {history} [, {index}])

:exe histget('c', 15)

That isn't exactly convenient to type, so you can also create a user-defined command:

:com -nargs=1 HI exe histget('c', <args>)

Thereafter you can use HI {index} to execute the history entry:

:HI 15

Upvotes: 6

jurglic
jurglic

Reputation: 3737

I use q: shortcut in normal mode to open interactive command history window. You just move to the right command and execute it by pressing Enter. You can find more information and other ways of accessing history here.

Upvotes: 11

cforbish
cforbish

Reputation: 8819

What I like to do is type the first few characters in the command and press <UP>. For example if you want to repeat an edit command of the file file.txt you could:

:e fil<UP><ENTER>

If the first <UP> does not give you the command you want, keep pressing<UP> until you find the command you were looking for.

Upvotes: 5

romainl
romainl

Reputation: 196556

:history is only there for you to look at it.

To re-execute a previous command, you have two options:

  • Use <Up> and <Down> at the command prompt:

    :m10
    (do stuff)
    :<Up>
    
  • Use the "command-line window":

    You can call it with q: and navigate with search and use the beautiful normal mode commands we all love.

    Position the cursor on a line and hit <CR> to re-execute the command.

    Edit a command and hit <CR> to execute the new command.

    You can quit the command-line window with :q.

    See :help cmdline-window.

Upvotes: 14

Related Questions