MUO logo

How to Access and Use Command History in Vim

Woman using laptop with multiple screens
No attribution needed -- Pexels
URL: https://www.pexels.com/photo/woman-using-a-computer-5473298/
Sign in to your MUO account

While you may use Linux shell's command history to recall previously used commands, did you know you can do the same thing in Vim?

Vim has a command history feature that lets you find and execute commands you've already entered. Here's how you can access it.

Recalling Vim Commands

The simplest way to recall commands you've entered in Vim is to enter command mode. To do this, press Escape and then the Colon (:) key. You can now scroll backward and forward through your command history by using the Up and Down arrow keys.

When you want to execute a command, press Enter. This is similar to using the shell history mechanism in modern shells like Bash or Zsh.

Opening the Command History Window

[画像:Vim command history window]
Screenshot by David Delony -- no attribution needed.

You can also open a history window that shows all your previously issued commands by entering command mode and pressing Ctrl + F.

When you've opened this window, you can navigate up and down by pressing the k and j keys, similar to moving around in the Vim buffer.

When you reach a line you want to run again, just press Enter and Vim will execute that command.

Closing the Command History Window

While the command history window is useful, you may wonder how to exit it without running a command. It's easy to open it accidentally since Ctrl + F also scrolls the screen forward if you haven't pressed the Colon (:) key. You might have meant to do this instead.

Fortunately, it's easy to close the command window. If you're in the command history, press Ctrl + C to jump down to the bottom, and then either press Ctrl + C or Esc to close the window completely.

Now you can page through your file if you meant to do this instead of opening the command history window.

Now You Can Access Command History in Vim

Command history is yet another one of Vim's powerful features. You can see commands you've issued, either in command mode or in the command history window, and execute them. If you opened the command history window by mistake, you can also close it quickly.

Another frequently overlooked feature of Vim is the ability to open and switch between multiple editing windows, and even run full terminal commands.

AltStyle によって変換されたページ (->オリジナル) /