Editing in the Terminal: edit
edit opens a text file in Microsoft Edit, an editor inside the terminal where the arrow keys and the mouse both work, Ctrl+S saves and Ctrl+Q quits, and notepad and code open the same file in a window.
Novice · PowerShell · Operating System · Updated
After this lesson you can
- Open a file with edit, change a line, save it with Ctrl+S and quit with Ctrl+Q
- Choose the right tool for a file job, such as edit to change a line, Set-Content or Add-Content to write, or Remove-Item to delete
Key takeaways
editand a file's name open the file in Microsoft Edit, a small text editor that runs inside the terminal and comes with Windows 11.- Move with the arrow keys or the mouse, type to change the text, press Ctrl+S to save and Ctrl+Q to quit.
notepadopens the same file in Notepad's window, andcodein VS Code, if you have it.- Use an editor to change part of a line. Set-Content and Add-Content write whole lines.
You've moved, and your smart speaker still thinks you live at the old address, so its weather and its "how long to get to work" are for the wrong street. The fix is one line in its settings file. This happens with computers all the time: a tutorial says "change the port in settings.json", an app's settings live in a .ini file, a project's README.md has a typo. Set-Content would rewrite the whole file, and Add-Content only adds to the end. To change part of one line and keep the rest, you open the file in a text editor, a program for changing text files.
This lesson works in the moving-day folder from Deleting Safely: Remove-Item, -WhatIf and -Confirm. Make the speaker's settings file there with the command from Writing to Files: Set-Content, Add-Content and >:
Set-Location ~\terminal-practice\moving-day
Set-Content speaker-settings.ini '[speaker]', 'name=Living Room', 'volume=40', 'address=12 Elm Street, Apt 3'
Get-Content speaker-settings.ini
[speaker]
name=Living Room
volume=40
address=12 Elm Street, Apt 3
Open the file: edit
edit opens Microsoft Edit, a small text editor that runs right inside the terminal and comes with current versions of Windows 11:
edit speaker-settings.ini
The file fills the window. A menu bar runs along the top (File, Edit, View, Help), the file's lines sit in the middle, and a status bar along the bottom shows where the cursor is.
If PowerShell says the term edit isn't recognized, your Windows doesn't have it yet. Install it once with winget install Microsoft.Edit, then open a new terminal tab.
Change, save, quit
Microsoft Edit works the way most Windows apps do, so many of the keys are ones you already know:
- Move: the arrow keys, or click where you want the cursor. The mouse works here, even inside a terminal.
- Change: go to the end of the
address=line, press Backspace until the old address is gone, and type48 Maple Avenue, Apt 4B. - Save: Ctrl+S.
- Quit: Ctrl+Q, which takes you back to the prompt.
If you quit before saving, Edit asks whether you want to save the changes you made, so a change is never lost or saved by surprise. Ctrl+Z undoes your last change, Ctrl+F finds a word in a long file, and every command is also in the menus, with its keys beside it.
Back at the prompt, check the file:
Get-Content speaker-settings.ini
[speaker]
name=Living Room
volume=40
address=48 Maple Avenue, Apt 4B
One line changed, and the other three are exactly as they were. One more trick: edit speaker-settings.ini:4 opens the file with the cursor already on line 4, handy when an error message names a line.
Other doors: notepad and code
edit is one way in. Two others open the same file in a window of its own:
notepad speaker-settings.iniopens it in Notepad, the editor every Windows PC has. The prompt comes back at once, and the file stays open in its window.code speaker-settings.iniopens it in VS Code, a free editor made for code, if you've installed it. Writing Scripts in VS Code, in Module 6, sets it up.
Which door you use is up to you. edit is quickest when you're already in the terminal, and it's the one that works on a computer you reach with no screen of its own. Notepad suits a quick change with the mouse, and VS Code suits a project with many files.
As a rule of thumb for any file job: to change part of a line, open an editor; to write or add whole lines, use Set-Content or Add-Content; to read, use Get-Content; to throw a file out, use Remove-Item, with -WhatIf first.
On a Mac or Linux, Microsoft Edit needs installing before edit works, and notepad is Windows only. Windows PowerShell 5.1 opens all three editors the same way.
Check yourself
2 questions to try on the lesson page.
So what
Settings files, notes and scripts are all text, and sooner or later one of them needs a one-line fix. Now you can make it without leaving the terminal, or open a window when that suits you better.