Tips and Tricks
The Command Prompt has many features and techniques that can noticeably speed up your work once you know them.
This article introduces a selection of tips you can use every day.
Using Command History
Browse History with the Up and Down Keys
Press ↑ and ↓ to recall previously entered commands one by one.
Show the History List with F7
Press F7 to display a popup list of command history. Select an entry with the arrow keys and press Enter to run it.
Show History as Text with doskey /history
doskey /history
Redirect the output, as in doskey /history > commands.txt, to save the history to a file.
By default, the most recent 50 commands are kept, and they are lost when you close the window. Save commands you want to keep as a batch file so you can reuse them.
Function Keys
| Key | Function |
|---|---|
| F1 | Re-enter the previous command one character at a time |
| F2 | Copy the previous command up to a specified character |
| F3 | Re-enter the previous command in full |
| F4 | Delete the typed text up to a specified character |
| F5 | Same as ↑ (previous history entry) |
| F7 | Show the command history list |
| F8 | Search history for entries matching the typed text |
| F9 | Recall a command by its number |
Type dir and press F8 to cycle through past commands that start with dir. This is a quick way to re-run frequently used commands.
Tab Completion
Press Tab to autocomplete file and folder names.
cd Doc[Tab]
Type part of a name and press Tab, and it completes to something like “Documents”.
- When there are several candidates, keep pressing Tab to cycle to the next one
- Press Shift+Tab to go back to the previous candidate
Keyboard Shortcuts
| Shortcut | Function |
|---|---|
| Ctrl+C | Interrupt the running command |
| Ctrl+V | Paste from the clipboard |
| Ctrl+A | Select all text on the screen |
| Ctrl+M | Toggle Mark mode (range selection) |
| Ctrl+Home | Scroll the view to the top |
| Ctrl+End | Scroll the view to the bottom |
| Alt+Enter | Toggle full-screen display |
| Esc | Clear the command being typed |
In Windows Terminal, the default terminal on Windows 11, you can also copy with Ctrl+Shift+C and paste with Ctrl+Shift+V (or Ctrl+V). Shortcuts may differ depending on the terminal and its settings.
Changing the Screen Colors (color Command)
The color command changes the background and text colors.
color [background][foreground]
Color Codes
| Code | Color | Code | Color |
|---|---|---|---|
| 0 | Black | 8 | Gray |
| 1 | Blue | 9 | Light blue |
| 2 | Green | A | Light green |
| 3 | Aqua | B | Light aqua |
| 4 | Red | C | Light red |
| 5 | Purple | D | Light purple |
| 6 | Yellow | E | Light yellow |
| 7 | White | F | Bright white |
Examples
color 0A
color 1F
color
If you specify the same code for both, the color command fails with ERRORLEVEL 1 and the colors do not change.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Changing the Title Bar (title Command)
title Backup - Running
Changing the title while a batch file runs lets you see at a glance which step is in progress.
Using It in a Batch File
@echo off
title Data Processing [1/3] - Copying files
copy /Y source\*.* dest\ >nul
title Data Processing [2/3] - Converting data
rem Conversion...
title Data Processing [3/3] - Cleaning up
del /Q temp\*.tmp >nul
title Data Processing - Done
echo All processing is complete.
pause
Changing the Prompt String (prompt Command)
The prompt command changes the text displayed at the prompt.
prompt $P$_$G
This shows the current path with > on the line below.
Special Codes for prompt
| Code | Displays |
|---|---|
$P | Current directory |
$G | > |
$D | Date |
$T | Time |
$N | Current drive |
$_ | Line break |
$$ | $ |
Restoring the Default
prompt
Useful Environment Variables
Here are some standard environment variables worth knowing.
| Variable | Contents |
|---|---|
%USERPROFILE% | The user’s home folder |
%APPDATA% | Application data folder |
%TEMP% | Temporary files folder |
%COMPUTERNAME% | Computer name |
%USERNAME% | Current user name |
%OS% | OS name (Windows_NT on Windows) |
%DATE% / %TIME% | Current date and time (format depends on regional settings) |
%RANDOM% | Random number from 0 to 32767 |
%CD% | Current directory (dynamic) |
%ERRORLEVEL% | Exit code of the previous command |
%~dp0 | Folder path of the batch file itself (batch files only) |
Example
echo User name: %USERNAME%
echo Home: %USERPROFILE%
echo PC name: %COMPUTERNAME%
echo Random: %RANDOM%
%~dp0 returns the path of the folder that contains the batch file itself. Use it to reliably refer to files in the same folder as the batch file. Example: set CONFIG=%~dp0config.ini
Storing Command Output in a Variable
Use for /f to store the output of a command in a variable.
@echo off
for /f "tokens=2 delims=:" %%A in ('ipconfig ^| findstr "IPv4"') do set IP=%%A
echo IP address:%IP%
The for variable is written %%A in a batch file but %A when typed directly at the Command Prompt. The value of %IP% also starts with a space. If there are several network adapters, or if autoconfiguration IPv4 lines also match, the value from the last matching line is kept.
Chaining Multiple Commands
| Symbol | Meaning |
|---|---|
& | Run the next command regardless of the previous result |
&& | Run the next command only if the previous one succeeds |
|| | Run the next command only if the previous one fails |
mkdir backup && echo Folder created || echo Failed to create folder
In the form A && B || C, C also runs if A succeeds but B fails. When you need strict branching, use the IF command.
Drag and Drop Files
Drag a file or folder onto the Command Prompt window to type its full path automatically.
This saves you from typing long paths by hand.
QuickEdit Mode
Enable “QuickEdit Mode” in the Command Prompt properties to select a range with the mouse, copy it with Enter, and paste with a right-click.
Summary
| Tip | Action |
|---|---|
| Show command history | F7 key |
| Tab completion | Tab key |
| Interrupt a command | Ctrl+C |
| Change screen colors | color background-foreground |
| Change the title | title text |
| Change the prompt | prompt format |
| Batch file’s folder path | %~dp0 |
Keeping these tips in mind makes working in the Command Prompt much faster.