The short answer
In Linux, the most popular way to edit the content of a file is to open it with an in-terminal text editor such as Vim using the following command:
$ vim <file>
Run in Warp
Where:
- file is the relative or absolute path to the file you want to edit.
Then:
- 1. Press the i key to enter the Insert mode.
- 2. Make your edits.
- 3. Press the ESC key to exit the Insert mode.
- 4. Type :wq and press ENTER to save your changes and quit Vim.
You can learn more about Vim by reading our other article on how to switch between Vim's 7 modes.
Editing read-only files with Vim
To edit a read-only file, you can open it with Vim using the following command:
$ vim <file>
Run in Warp
Once you've made your changes, you can type the following command and press ENTER to save the file and quit Vim:
:wq !sudo tee %
Run in Warp
Where:
- :wq is used to save (i.e., write) and exit (i.e., quit) Vim.
- !sudo is used to run the following command with root privileges in Vim.
- tee reads from standard input and writes to standard output and files simultaneously.
- % represents the name of the current file.
You can learn more about read-only files by reading our other article on Linux file permissions and how to change file permissions in Linux.
Editing zipped files with Vim
Vim provides out of the box support for editing the files contained in an archive without having to extract them.
To edit a file within an archive, you can open it using the following command:
$ vim <file>
Run in Warp
Then:
- 1. Navigate to the file you want to edit using the UP and DOWN arrow keys and hit ENTER.
- 2. Press the i key to enter the Insert mode.
- 3. Make your edits.
- 4. Press the ESC key to exit the Insert mode.
- 5. Type the :wq command and press the ENTER key to save the changes and close the file.
- 6. Type the :q command and press the ENTER key to exit Vim.
Note that zip files aren't the only compressed files that you can open/edit with Vim. You can also edit gzipped files, tarballs, JPEGs, and .bzip2 amongst others.
Editing binary files with Vim
To open and edit a file in Binary mode with Vim, you can use the -b flag as follows:
$ vim -b <file>
Run in Warp
Type the :%!xxd command within the editor to replace the buffer with an editable hexadecimal dump where:
- The left column shows the offset of the first byte on each line.
- The middle column shows the bytes in hexadecimal.
- The right column shows the printable characters.
Then:
- 1. Press the i key to enter the Insert mode.
- 2. Make your edits to the hexadecimal part.
- 3. Press the ESC key to exit the Insert mode.
- 4. Type the :%!xdd -r command and press the ENTER key to reverse the buffer to its binary form.
- 5. Type the :wq command and press the ENTER key to save the changes and close the file.
Editing files with Nano
Nano is a built-in Linux editor that provides a straightforward and simpler editing experience.
To edit a file with Nano, you can use the nano command as follows:
$ nano <file>
Run in Warp
Where:
- file is the relative or absolute path to the file you want to edit.
Once you’ve made your modifications, you can press CTRL+O to save your file and CTRL+X to exit the editor.
Note that unlike Vim, Nano doesn’t have several edition modes and is automatically in the equivalent of Vim’s Insert mode.
Editing files with Emacs
Emacs is yet another file editor available on Linux with similar features to Vim.
To edit a file with Emacs, you can use the emacs command as follows:
$ emacs <file>
Run in Warp
Once you’ve made your modifications, you can press CTRL+X then CTRL+S to save your file and CTRL+X then CTRL+C to exit the editor.
Overwriting the content of a file
To overwrite the content of a file, you can use the single output redirection operator > as follows:
$ command > file
Run in Warp
Where:
- command is a shell command like echo or cat.
- file is the relative or absolute path to the file you want to write into.
For example, the following echo command will write the string ”Hello, World!” into the file named hello.txt:
$ echo “Hello, World!” > hello.txt
Run in Warp
And the following command will read from the standard input until it encounters an end-of-file (i.e., CTRL+D) and write the content typed in the terminal window in the file named hello.txt:
$ cat > lines.txt
Hello, World!
Bonjour, Monde!
^D
Run in Warp
Note that the output redirection operator will automatically create the file if it doesn’t exist.
Appending content to a file
To append content to an existing file without overwriting it, you can use the double output redirection operator >> as follows:
$ command >> file
Run in Warp
For example, the following command will append the string “Hallo, Welt!” at the end of the file named hello.txt:
$ echo “Hallo, Welt!” >> hello.txt
Run in Warp
Editing a file based on patterns with sed
The sed command is a powerful file editing command that allows you to edit files on the fly without opening them.
Find and replace text occurrences within a file
To replace all the occurrences of a pattern in a file, you can use the sed command with the -i flag as follows:
$ sed -i 's/<old_string>/<new_string>/g' <file>
Run in Warp
Where:
- old_string is the string you want to replace.
- new_string is the string that will replace the occurrences of old_string.
- file is the relative or absolute path to the file you want to make changes to.
- s is the substitute command of sed for “find and replace”.
- / serves as a delimiter.
- g is used to replace all occurrences of old_string.
For example, the following command will replace all the occurrences of the word "Adventure" by the word "Exploits" in the file named Sherlock.txt:
$ sed -i 's/Adventure/Exploits/g' Sherlock.txt
Run in Warp
Easily retrieve this command using Warp AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering Replace string in file in the AI Command Suggestions will prompt a list of sed commands that can then quickly be inserted into your shell by doing CMD+ENTER.
Deleting lines containing a specified string
To remove all the lines of a file containing a specific word, you can use the following sed command:
$ sed -i '/<string>/d' <file>
Run in Warp
For example, the following command will remove all the lines containing the word "The" in the file named Sherlock.txt:
$ sed -i '/The/d' Sherlock.txt
Run in Warp
Deleting specific lines
To remove a specific line number of a file, you can use the following sed command:
$ sed '<n>d' <file>
Run in Warp
Alternatively, to remove multiple lines at once, you can use the following syntax instead:
<;code class="language-shell">$ sed '<n>,<m>d' <file>
Run in Warp
For example, the following command will remove the first line of the file named sherlock.txt:
$ sed '1d' sherlock.txt
Run in Warp
And this command will remove the lines 2 through 6 of the same file:
$ sed '2,6d' Sherlock.txt
Run in Warp
Editing the crontab file
To edit the crontab file in Linux, you can use the crontab command with the -e flag as follows:
$ crontab -e
Run in Warp
Note that this command will open the crontab file using the text editor defined in the EDITOR environment variable or Vi/Vim by default.
If you want to use another text editor, you can use the following command:
$ EDITOR=<path> crontab -e
Run in Warp
Where:
- path is the path to the text editor's binary.
For example, the following command will open the crontab file using Nano:
$ EDITOR=/bin/nano crontab -e
Run in Warp
Written by
Jeremiah Muchiri
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
How to Check the Size of Folders in Linux
Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.