03-05-2023, 08:32 AM
5 Easy Ways to Edit a Text File From Command Line (Windows)
5/5 – (1 vote)
Problem Formulation
![[Image: image-33.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-33.png)
Given is a text file, say. How to modify its content in your Windows command line working directory?
I’ll start with the most direct method to solve this problem in 90% of cases and give a more “pure” in-terminal method afterward.
Method 1: Using Notepad
The easiest way to edit a text file in the command line (CMD) on your Windows machine is to run the command, or simply , in your cmd to open the text file with the visual editor Notepad.
You can also skip the prefix in most cases:
Now, you may ask:
Is Notepad preinstalled in any Windows installation? The answer is: yes! Notepad is a generic text editor to create, open, and read plaintext files and it’s included with all Windows versions.
Here’s how that looks on my Win 10 machine:
![[Image: image-28-1024x368.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-28-1024x368.png)
When I type in the command, CMD starts the Notepad visual editor in a new window.
I can then edit the file and hit to save the new contents.
But what if you cannot open a text editor—e.g. if you’re logged into a remote server via SSH?
Method 2: Pure CMD Approach
![[Image: image-32.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-32.png)
If you cannot open Notepad or other visual editors for some reason, a simple way to overwrite a text file with built-in Windows command line tools is the following:
Here’s what this looks like on my Windows machine, where I changed to contain the text :
![[Image: image-29.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-29.png)
This is a simple and straightforward approach to small changes. However, if you have a large file and you just want to edit some minor details, this is not the best way.
Method 3: Change File Purely In CMD (Copy Con)
If you need a full-fledged solution to edit potentially large files in your Windows CMD, use this method!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
To create a new file in Windows command prompt, enter followed by the target file name (). Then enter the text you want to put in the file. To end and save the file, press then or then .
How this looks on my Win machine:
![[Image: image-30.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-30.png)
A couple of notes:
Info: To edit an existing file, display the text by using the command followed by the file name. Then copy and paste the text into the command to make changes. Be careful not to make any typos, or you’ll have to start over again. Backspace works if you catch the mistake before pressing Enter. Note that this method may not work in PowerShell or other command line interfaces that don’t support this feature.
Method 4: If you SSH’d to a Unix Machine
Of course, if you have logged in a Unix-based machine, you don’t need to install any editor because it comes with powerful integrated editors such as vim or emacs.
One of the following three commands should open your file in a terminal-based editing mode:
You can learn more about Vim here.
Summary
![[Image: image-31.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-31.png)
To edit a in the command line, use the command to open a graphical editor on Windows.
If you need a simple file edit in your terminal without a graphical editor and without installation, you can use the command that overwrites the old content in with .
If you need a more direct in-CMD text editor run to open the file in editing mode.
If you’re SSH’d into a Unix machine, running the Vim console-based editor may be the best idea. Use or to open it.
Feel free to join our email coding academy (it’s free):
Recommended: How to Edit a Text File in PowerShell (Windows)
5/5 – (1 vote)
Problem Formulation
![[Image: image-33.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-33.png)
Given is a text file, say
Code:
my_file.txtI’ll start with the most direct method to solve this problem in 90% of cases and give a more “pure” in-terminal method afterward.
Method 1: Using Notepad
The easiest way to edit a text file in the command line (CMD) on your Windows machine is to run the command
Code:
notepad.exe my_text_file.txtCode:
notepad my_text_file.txtCode:
notepad.exe my_file.txtYou can also skip the
Code:
.exeCode:
notepad my_text_file.txtNow, you may ask:
Is Notepad preinstalled in any Windows installation? The answer is: yes! Notepad is a generic text editor to create, open, and read plaintext files and it’s included with all Windows versions.Here’s how that looks on my Win 10 machine:
![[Image: image-28-1024x368.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-28-1024x368.png)
When I type in the command
Code:
notepad.exe my_text_file.txtI can then edit the file and hit
Code:
CTRL + SBut what if you cannot open a text editor—e.g. if you’re logged into a remote server via SSH?
Method 2: Pure CMD Approach
![[Image: image-32.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-32.png)
If you cannot open Notepad or other visual editors for some reason, a simple way to overwrite a text file with built-in Windows command line tools is the following:
- Run the command to print the new content usingCode:
echo 'your new content' > my_file.txtand pipe the output into the text fileCode:echousingCode:my_text_file.txt.Code:>
- Check the new content using the command .Code:
type my_text_file.txt
Code:
C:\Users\xcent\Desktop>echo 'hello world' > my_file.txt
C:\Users\xcent\Desktop>type my_file.txt 'hello world'Here’s what this looks like on my Windows machine, where I changed
Code:
my_file.txtCode:
'hello world!'![[Image: image-29.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-29.png)
This is a simple and straightforward approach to small changes. However, if you have a large file and you just want to edit some minor details, this is not the best way.
Method 3: Change File Purely In CMD (Copy Con)
If you need a full-fledged solution to edit potentially large files in your Windows CMD, use this method!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
To create a new file in Windows command prompt, enter
Code:
copy conCode:
copy con my_file.txtCode:
Ctrl+ZCode:
EnterCode:
F6Code:
EnterCode:
copy con my_file.txtHow this looks on my Win machine:
![[Image: image-30.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-30.png)
A couple of notes:
Info: To edit an existing file, display the text by using the Code:
typeCode:
copy conMethod 4: If you SSH’d to a Unix Machine
Of course, if you have logged in a Unix-based machine, you don’t need to install any editor because it comes with powerful integrated editors such as vim or emacs.
One of the following three commands should open your file in a terminal-based editing mode:
Code:
vim my_text_file.txt
vi my_text_file.txt
emacs my_text_file.txtYou can learn more about Vim here.
Summary
![[Image: image-31.png]](https://blog.finxter.com/wp-content/uploads/2023/03/image-31.png)
To edit a
Code:
file.txtCode:
notepad file.txtIf you need a simple file edit in your terminal without a graphical editor and without installation, you can use the command
Code:
echo 'new content' > file.txtCode:
file.txtCode:
new contentIf you need a more direct in-CMD text editor run
Code:
copy con file.txtIf you’re SSH’d into a Unix machine, running the Vim console-based editor may be the best idea. Use
Code:
vim file.txtCode:
vi file.txtFeel free to join our email coding academy (it’s free):
Recommended: How to Edit a Text File in PowerShell (Windows)

