Showing posts with label command history. Show all posts
Showing posts with label command history. Show all posts

14 December 2025

On Ubuntu, how to run one of the commands I ran before again?

To rerun a command from your history, use ! followed by its number (e.g., !123), or use !! for the very last command, !-2 for the second-to-last, or press Ctrl+r and type part of the command to search for it, then hit Enter. 
By History Number
  1. View History: Type history and press Enter to see a numbered list.
  2. Re-run: Type ! then the number of the command (e.g., !567 to run command #567). 
bash
# Example:
history
# ... (list of commands with numbers) ...
!123 # Runs the 123rd command
Shortcuts
  • !!: Runs the immediately preceding command.
  • !-2: Runs the second-to-last command.
  • !string: Runs the most recent command starting with string (e.g., !ls runs the last ls command). 
Reverse Search (Ctrl+r) 
  • Press Ctrl+r to enter interactive search mode.
  • Type part of the command you're looking for (e.g., ssh).
  • Press Ctrl+r again to cycle through older matches.
  • Press Enter to run the command, or Esc to exit search without running. 
  • If you have cycled through some older matches, now you want to cycle
    through some new 
    matches, you can add the following line to your  ~/.bashrc file:
stty -ixon
Starting now, you can press   Ctrl+s to cycle through new matches.


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).