14 December 2025

In Vim, how to run one of the commands I ran before again?

    To re-run a command from your Vim history, you can use the following methods:
    1. Using the Command-line Window (q:)
    The most common and interactive way is to open the command-line window: 
    1. Press q:. This opens a special window in your Vim buffer which lists all previously executed commands, similar to a regular file buffer.
    2. Use the standard Vim movement keys (jkG, etc.) to navigate to the command you wish to re-run.
    3. Press Enter while your cursor is on the line of the desired command. Vim will execute that command and close the history window.
    4. To exit the window without running a command, use :q or Ctrl-C. 
    2. Using Command-line History Navigation
    You can cycle through your command history directly from the command prompt: 
    1. Type : to start a new command line.
    2. Use the Up Arrow (<Up>) or Ctrl-P to go back through the history of previous commands.
    3. Use the Down Arrow (<Down>) or Ctrl-N to go forward through the history.
    4. When the desired command appears on the command line, press Enter to execute it. 
    3. Using History Search (q/ or q?) 
    If you have a long history and want to find a specific command quickly, you can search for it:
    1. Press q/ (for forward search) or q? (for backward search) to open the search history window.
    2. Type your search pattern (e.g., s/) and press Enter. The window will list commands matching that pattern.
    3. Navigate to the command you want to run and press Enter to execute it. 
    4. Running a Specific History Index 
    You can also run a specific command by its index number if you know it from viewing :history. This is less common but useful if you need an exact, old command: 
    1. Type :@: followed by the number of the history entry you want to execute (e.g., :@:25 to run command number 25).

No comments:

Post a Comment