Vscode Terminal
VS Code has a built-in integrated terminal. We can select the "Terminal" option from the menu bar and then choose "New Terminal":
!(#)
You can also select a file in the directory, right-click, and select "Open in Integrated Terminal" from the dropdown menu:
!(#)
**Terminal Shortcuts:**
| Function | Windows/Linux | macOS |
| --- | --- | --- |
| Show Integrated Terminal | Ctrl + ` | Ctrl + ` |
| New Terminal | `Ctrl+Shift+``` | `Cmd+Shift+``` |
| Switch Terminal | `Ctrl+PageUp/PageDown` | `Cmd+PageUp/PageDown` |
| Close Terminal | `Ctrl+Shift+W` | `Cmd+Shift+W` |
Depending on the operating system, we can choose different terminal environments, such as PowerShell, Command Prompt, or Bash.
!(#)
Enter the following command in the terminal to create a new file greetings.txt:
echo "Hello, VS Code" > greetings.txt
The default folder for the workspace is the root directory of the current project.
After creating the file, the "Explorer" will automatically display the new file.
!(#)
We can open multiple terminals at the same time. Click the "Launch Profile" dropdown menu in the top right corner of the terminal to view available terminal environments and select one.
!(#)
* * *
## Running Commands Using the Terminal
The terminal opens a default shell such as Bash, PowerShell, or Zsh. The working directory of the terminal starts from the root directory of the workspace folder.
!(#)
Enter basic commands like ls or dir to list the files in the current directory.
The terminal will directly display the command output:
!(#)
### Interacting with Command Output
The terminal in VS Code also provides the ability to interact with command output. Commands often output file paths or URLs, and you may want to open or jump to these links directly.
For example, a compiler or code checking tool might return an error message containing a file path and line number. You don't need to manually search for the file. Simply select the link in the terminal output to open the file directly in the editor.
Let's look at how to interact with command output in the terminal:
1. Open the terminal where you previously ran the `ls` or `dir` command.
2. In the terminal, hold the `Ctrl/Cmd` key, hover your mouse over the filename, and select the link.
!(#)
Note that when you hover your mouse over text in the output, it will turn into a link.
When you select a filename, VS Code will open the selected file in the editor.
All text in the terminal output is clickable. If you select a hyperlink in the terminal, it will open the link in the default browser.
### Create a Command.txt File Containing Available Shell Commands
In PowerShell:
Get-Command | Out-File -FilePath .Command.txt
In Bash / Zsh:
ls -l /usr/bin > Command.txt
Enter the following command to search for commands in the Command.txt file:
In PowerShell:
Get-ChildItem *.txt | Select-String "dir"
In Bash / Zsh:
grep -n "dir" *.txt
Note that the command output will include the filename and the line number where the search result was found. The terminal will recognize this text as links.
Selecting one of these links will open the file in the editor and jump to a specific line in the file.
!(#)
### Browsing Command History
When using the terminal, we may need to view previous commands and their output, or may want to rerun a certain command.
We can use keyboard shortcuts to quickly browse command history: press `Ctrl + β` or `β + β` to scroll up to the previous command in the terminal history.
!(#)
If you press `Ctrl + β` multiple times, the terminal will continue to scroll up through the history. You can also use `Ctrl + β` to browse history downwards.
Best of all, we will see a circular icon appear next to a previously run command. Select that circular icon and then select "Rerun Command" to re-execute the command.
!(#)
### Running Commands in Different Shells
The terminal supports opening multiple terminals at the same time.
For example, we can use one terminal for running Git commands
YouTip