vim

Vim Command History

Vim is a powerful code and text editor that is specifically designed for Unix, while Linux and macOS have it installed by default. After all, it is highly powerful and fast, in part because it is a small program that can run easily in a terminal despite having a built-in graphical interface. It can efficiently work without a menu or mouse with a keyboard. It is a simple text editor; however, it records the history of the commands we entered in command line mode. It keeps track of all the activities in command line mode, which allows us to recall previous commands easily. We do not have to type out all the long commands again, which we have typed before. We can simply go back through the command line history and use the “ex” commands at the prompt. This blog will learn about the vim command history, its usage, and how it can help us edit files while saving time and energy.

Command-line History

Vim text editor is capable of recording the history of all the commands that we enter on the command line prompt. There are two ways of recalling the command history: dialling up the command line window or scrolling through the past command lines using the cursor keys.

Scrolling through the past commands:

To switch to the command line prompt, press : key. After “:”, leave the prompt empty and then press the upper arrow key. The command history will be populated with the most recent executed command in the command line mode. Use the upper arrow key again to go further back through the commands you have entered so far, and use the down arrow key to come back to the most recent command you have entered.

Dialling up the command line window:

To search the specific previous command in the command line prompt, use : and your search key. For example; type :p and then press the upper arrow key. It will search the command that starts with p and display those commands for you. You can scroll through the history by using up and down arrow keys. This provides the history for all the commands which you have entered so far in the command line prompt.

Example

In this example, we have created a file with the name of “newfile”, and in it, we have entered some text, as you can see in the following screenshot.

After inserting the text, close this file by typing :wq. In the command window, type :p and then press the up key. It will provide the history of all commands in each press that you have used recently. In the below screenshot, we have attached two results out of many that we got.

Moreover, if you want to search the history, you can also do that. The entire history can be listed by using the: history command. For example, :his command lists the history of the commands entered so far, and :his / will list the search history. / is used to start a search, so when it is combined with : it lists the search history.

A command-line window is a better solution in many situations that can be easily opened in two different ways:

  • To search in the command window, type q/, and for commands, type “q:”
  • Type / to simply search or type : to enter a new command, then type the ‘cedit’ or press ctrl+f.

The command line window provides many advantages. The top benefit of the command line window is that it enables you to use all the editing power of vim, including simple search with / key in the normal mode or the insert mode’s whole line completion command. Furthermore, once you have edited the command, you can:

Press the enter key to execute the command on the current line and close the command line window.

Press the Ctrl+C key two times to cancel executing the current line and close the window.

Vim Editor Modes

The Vim text editor has three modes; normal mode or insert mode, command mode or line mode, and visual mode.

Normal mode or insert mode:

The normal mode is vim’s default mode which is used for editor commands. However, the default mode can be turned into the insert mode, where you can easily write, modify, and delete the text.

Command mode or command line mode:

The command line mode allows you to execute commands like find and replace, undo and redo, quit, etc. It supports the single line input at a time at the bottom of the vim command line window.

Visual mode:

The visual mode is very similar to the normal mode or the insert mode. However, it allows you to highlight or edit the text in bulk.

Example

Here are a few examples that will help you learn how you can easily use the command history to go through the commands you have entered so far, and if needed, you can reuse them by simply copying and pasting. This way, you can avoid the hassle of typing the long commands again and again. You just go through the history and use the previous commands as is where required. For example, let’s say you have typed ‘hellos’, then ‘humor’, then ‘purple’, then ‘simple’, then ‘humanity’, then ‘Hello’ again, then ‘market’, then ‘Helium’, ‘play, then ‘human’ and then ‘Habit’.

After typing all such text, you need to search for the words that start with ‘h’; for that, you need to type /h. When you enter /h and repeatedly press the up arrow key, it will take you through all the words that start with h, i.e., ‘human’, ‘humanity’, ‘humor’, ‘hello’. However, if you notice, you can observe that /h has skipped ‘Habit’, ‘Helium’, and ‘Hello’.

Why is that? Why does it not show these words when you search for the words that start with ‘h’. Well, it is because the vim history navigation is case sensitive; you have searched for the words starting with the lowercase letter ‘h’ that is ‘/h’. Therefore, the words that even start with the letter ‘h’ still not be in the navigation because they start with the uppercase letter H.

Vim Command History Store

You must be wondering, do the vim’s commands history store somewhere?

Well, yes, the command antiquity is kept in the viminfo file (:help viminfo). The viminfo file is usually read on startup and write up on exit; however, you can explicitly persist and synchronize between vim instances. To synchronize between vim instances, you can use :wviminfo and :rviminfo commands

Conclusion

In this blog, we have learned about the vim command history, two ways of recalling the command history, and the three modes of the command-line window. In addition, with the help of a simple example, we have learned the usage of “:” and “/” to enter and search the command in command line history.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.