Vim Command Line Window

4.7
(60)

Have you ever tried to quit Vim with :q only to have a small window appear, showing you the last few commands you typed? You accidentally opened the Vim command line window. It happened because the command line window is opened with q:, and you made a common transposition mistake. Aside from giving you a quick look at your history, this window actually has some useful features.

Vim automatically stores your command history and by default it will store only the last 20 commands. I’d recommend increasing this limit to 100 or 1,000 or other similarly more useful number. It’s easy with a single line in your .vimrc:

set history=10000

This history is stored across all your Vim sessions so it works across all projects, directories, and repositories, making it easier to find your common commands.

You may notice that when you open the Vim command line window it shows a number next to each entry in your history, simply a running count of your historical commands. On the last line is the next number without a command next to it. For example, if I have 1,623 entries in my history, here is what I see:

1623 Git push
1624 

Herein lies the magic of this unique buffer. You can type a command on that last line and when you press <Enter>, the command will execute. Because this window is a regular Vim buffer, you have all the tools, movements, text objects, autocomplete, plugins, and more that you have available to you everywhere else in Vim. For typing complex commands, this area can be invaluable. Instead of tedious repetitive entry, run q: and use your full Vim prowess to craft that perfect command.

And there’s more: It’s not just this last empty line that you can edit. You can navigate back up to any prior line and modify it. When you press <Enter> the modified command will be executed, appended to your history, and the command line window will close.

With all that said, if you really did open the command line window by mistake with q: it’s pretty easy to close it::q.

Check out the demo screencast below where I show this in action. First, I open the Vim command line window with q:, then I search through the history for a Git rebase command with /rebase and press <Enter> to go to that line. Then I increment the number steps that I want to rebase back with 3<ctrl-a>, increasing the number by 3. I press <Enter> to execute the command.

(By the way, we wrote an entire eBook on using Git within Vim.)

How useful was this tip?

Average rating 4.7 / 5. Vote count: 60

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts