Terminus
Edit Files in Linux

Edit Files in Linux

[#the-short-answer] The short answer [#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>

Where:

  • [.inline-code]file[.inline-code] is the relative or absolute path to the file you want to edit.

Then:

  1. Press the [.inline-code]i[.inline-code] key to enter the Insert mode.
  2. Make your edits.
  3. Press the [.inline-code]ESC[.inline-code] key to exit the Insert mode.
  4. Type [.inline-code]:wq[.inline-code] and press [.inline-code]ENTER[.inline-code] 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.

[#edit-read-only-files-with-vim] Editing read-only files with Vim [#edit-read-only-files-with-vim]

To edit a read-only file, you can open it with Vim using the following command:

$ vim <file>

Once you've made your changes, you can type the following command and press [.inline-code]ENTER[.inline-code] to save the file and quit Vim:

:wq !sudo tee %

Where:

  • [.inline-code]:wq[.inline-code] is used to save (i.e., write) and exit (i.e., quit) Vim.
  • [.inline-code]!sudo[.inline-code] is used to run the following command with root privileges in Vim.
  • [.inline-code]tee[.inline-code] reads from standard input and writes to standard output and files simultaneously.
  • [.inline-code]%[.inline-code] 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.

[#edit-zip-archives-with-vim] Editing zipped files with Vim [#edit-zip-archives-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>

Then:

  1. Navigate to the file you want to edit using the [.inline-code]UP[.inline-code] and [.inline-code]DOWN[.inline-code] arrow keys and hit [.inline-code]ENTER[.inline-code].
  2. Press the [.inline-code]i[.inline-code] key to enter the Insert mode.
  3. Make your edits.
  4. Press the [.inline-code]ESC[.inline-code] key to exit the Insert mode.
  5. Type the [.inline-code]:wq[.inline-code] command and press the [.inline-code]ENTER[.inline-code] key to save the changes and close the file.
  6. Type the [.inline-code]:q[.inline-code] command and press the [.inline-code]ENTER[.inline-code] 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.

[#edit-binary-files-with-vim] Editing binary files with Vim [#edit-binary-files-with-vim]

To open and edit a file in Binary mode with Vim, you can use the [.inline-code]-b[.inline-code] flag as follows:

$ vim -b <file>

Type the [.inline-code]:%!xxd[.inline-code] 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 [.inline-code]i[.inline-code] key to enter the Insert mode.
  2. Make your edits to the hexadecimal part.
  3. Press the [.inline-code]ESC[.inline-code] key to exit the Insert mode.
  4. Type the [.inline-code]:%!xdd -r[.inline-code] command and press the [.inline-code]ENTER[.inline-code] key to reverse the buffer to its binary form.
  5. Type the [.inline-code]:wq[.inline-code] command and press the [.inline-code]ENTER[.inline-code] key to save the changes and close the file.

[#edit-files-with-nano] Editing files with Nano [#edit-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 [.inline-code]nano[.inline-code] command as follows:

$ nano <file>

Where:

  • [.inline-code]file[.inline-code] is the relative or absolute path to the file you want to edit.

Once you’ve made your modifications, you can press [.inline-code]CTRL+O[.inline-code] to save your file and [.inline-code]CTRL+X[.inline-code] 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.

[#edit-files-with-emacs] Editing files with Emacs [#edit-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 [.inline-code]emacs[.inline-code] command as follows:

$ emacs <file>

Once you’ve made your modifications, you can press [.inline-code]CTRL+X[.inline-code] then [.inline-code]CTRL+S[.inline-code] to save your file and [.inline-code]CTRL+X[.inline-code] then [.inline-code]CTRL+C[.inline-code] to exit the editor.

[#overwrite-files] Overwriting the content of a file [#overwrite-files]

To overwrite the content of a file, you can use the single output redirection operator [.inline-code]>[.inline-code] as follows:

$ command > file

Where:

  • [.inline-code]command[.inline-code] is a shell command like [.inline-code]echo[.inline-code] or [.inline-code]cat[.inline-code].
  • [.inline-code]file[.inline-code] is the relative or absolute path to the file you want to write into.

For example, the following [.inline-code]echo[.inline-code] command will write the string ”Hello, World!” into the file named [.inline-code]hello.txt[.inline-code]:

$ echo “Hello, World!” > hello.txt

And the following command will read from the standard input until it encounters an end-of-file (i.e., [.inline-code]CTRL+D[.inline-code]) and write the content typed in the terminal window in the file named [.inline-code]hello.txt[.inline-code]:

$ cat > lines.txt
Hello, World!
Bonjour, Monde!
^D

Note that the output redirection operator will automatically create the file if it doesn’t exist.

[#append-to-files] Appending content to a file [#append-to-files]

To append content to an existing file without overwriting it, you can use the double output redirection operator [.inline-code]>>[.inline-code] as follows:

$ command >> file

For example, the following command will append the string “Hallo, Welt!” at the end of the file named [.inline-code]hello.txt[.inline-code]:

$ echo “Hallo, Welt!” >> hello.txt

[#edit-files-with-sed] Editing a file based on patterns with [.inline-code]sed[.inline-code] [#edit-files-with-sed]

The [.inline-code]sed[.inline-code] command is a powerful file editing command that allows you to edit files on the fly without opening them.

[#find-and-replace-strings] Find and replace text occurrences within a file [#find-and-replace-strings]

To replace all the occurrences of a pattern in a file, you can use the [.inline-code]sed[.inline-code] command with the [.inline-code]-i[.inline-code] flag as follows:

$ sed -i 's/<old_string>/<new_string>/g' <file>

Where:

  • [.inline-code]old_string[.inline-code] is the string you want to replace.
  • [.inline-code]new_string[.inline-code] is the string that will replace the occurrences of [.inline-code]old_string[.inline-code].
  • [.inline-code]file[.inline-code] is the relative or absolute path to the file you want to make changes to.
  • [.inline-code]s[.inline-code] is the substitute command of [.inline-code]sed[.inline-code] for “find and replace”.
  • [.inline-code]/[.inline-code] serves as a delimiter.
  • [.inline-code]g[.inline-code] is used to replace all occurrences of [.inline-code]old_string[.inline-code].

For example, the following command will replace all the occurrences of the word "Adventure" by the word "Exploits" in the file named [.inline-code]Sherlock.txt[.inline-code]:

$ sed -i 's/Adventure/Exploits/g' Sherlock.txt

[#easily-recall-syntax-using-ai] Easily retrieve this command using Warp AI Command Suggestions [#easily-recall-syntax-using-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]Replace string in file[.inline-code] in the AI Command Suggestions will prompt a list of [.inline-code]sed[.inline-code] commands that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#delete-lines-containing-a-string] Deleting lines containing a specified string [#delete-lines-containing-a-string]

To remove all the lines of a file containing a specific word, you can use the following [.inline-code]sed[.inline-code] command:

$ sed -i '/<string>/d' <file>

For example, the following command will remove all the lines containing the word "The" in the file named [.inline-code]Sherlock.txt[.inline-code]:

$ sed -i '/The/d' Sherlock.txt

[#delete-specific-lines] Deleting specific lines [#delete-specific-lines]

To remove a specific line number of a file, you can use the following [.inline-code]sed[.inline-code] command:

$ sed '<n>d' <file>

Alternatively, to remove multiple lines at once, you can use the following syntax instead:

<;code class="language-shell">$ sed '<n>,<m>d' <file>

For example, the following command will remove the first line of the file named [.inline-code]sherlock.txt[.inline-code]:

$ sed '1d' sherlock.txt

And this command will remove the lines 2 through 6 of the same file:

$ sed '2,6d' Sherlock.txt

[#edit-the-crontab-file] Editing the crontab file [#edit-the-crontab-file]

To edit the crontab file in Linux, you can use the [.inline-code]crontab[.inline-code] command with the [.inline-code]-e[.inline-code] flag as follows:

$ crontab -e

Note that this command will open the crontab file using the text editor defined in the [.inline-code]EDITOR[.inline-code] 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

Where:

  • [.inline-code]path[.inline-code] 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