How To Edit Files In Linux?

Learn via video courses
Topics Covered

Editing Files in Linux is a fundamental task that every user should be familiar with. Are there any specific software packages for Linux edit files? Yes, there are. In this article, we will explore three popular text editors—VI, GNU Nano, and Emacs—and discover their unique features and commands to enhance your file editing capabilities in Linux.

Edit Files with VI Editor

VI is a popular and powerful command-line text editor preinstalled on most Linux distributions. It provides a robust set of features and commands for editing files efficiently. While VI has a steep learning curve, mastering its usage can enhance your file editing capabilities in Linux.

VI editors have three main modes:

  1. Command Mode: This is the default mode when you open a file in VI. In this mode, you can navigate through the file and issue commands for editing and manipulation. Edit Files with VI Editor

, For example,, you can move the cursor to a specific line by typing :5 and pressing Enter, which takes you to line 5. Edit Files with VI Editor

  1. Insert Mode: In this mode, you can enter text and make modifications directly to the file. , For example,, after switching to insert mode by pressing i, you can type your desired text and then press Esc to return to command mode. Edit Files with VI Editor

Edit Files with VI Editor

  1. Visual Mode: This mode allows you to visually select and manipulate blocks of text. , For example,, you can enter visual mode by pressing v. Make sure that you are in command mode before pressing the keyv. Edit Files with VI Editor

Then use the arrow keys to select the desired text. Once selected, you can perform actions like copying or deleting the selected text. Edit Files with VI Editor

In Linux edit the file using VI by following these steps:

  1. Open a terminal window.
  2. Type vi scaler_topics.txt to open the scaler_topics.txt file with VI. Edit Files with VI Editor

Edit Files with VI Editor

Once you have opened the file in VI, you will be in command mode. You can navigate through the file and execute various editing commands. Here are some essential commands and their sub-commands to get you started:

CommandSub-CommandDescriptionExample
iEnter "insert mode" to start inserting or modifying text.Press i to enter insert mode and start typing.
EscReturn to the "command mode" from insert mode or other modes.Press Esc to return to command mode.
:wSave the changes made to the file.Type :w and press Enter to save the changes.
:qQuit the VI editor.Type :q and press Enter to exit the editor.
:wqSave the changes and exit the editor.Type :wq and press Enter to save and exit.
ddDelete the current line.Press dd to delete the current line.
yyCopy the current line.Press yy to copy the current line.
pPaste the copied or deleted line after the current line.Press p to paste the copied or deleted line.
/search_termSearch for a specific term within the file.Type /search_term and press Enter to search for the term.
nMove to the next occurrence of the search term.Press n to move to the next occurrence.
uUndo the last change.Press u to undo the last change.
Ctrl+rRedo the last undone change.Press Ctrl+r to redo the last undone change.

You can combine two or more commands to perform complex editing tasks. , For example,, let's say you want to delete lines 3 to 5 in the file.

To achieve this, you would follow these steps:

  1. While in the command mode, type 3,5d and press Enter. Edit Files with VI Editor

  2. This command specifies a range from line 3 to line 5 (3,5) and the d command for deletion.

  3. Pressing Enter executes the command, and lines 3 to 5 will be deleted from the file. Edit Files with VI Editor

With practice, VI becomes a powerful tool for manipulating files in Linux, making it a valuable addition to your Linux editing toolkit.

Edit Files with GNU Nano

GNU Nano is a user-friendly and lightweight command-line text editor in most Linux distributions. It provides a simple and intuitive interface for editing files, making it an excellent choice for both beginners and experienced users.

To edit a file using GNU Nano, follow these steps:

  1. Open a terminal window.
  2. Type nano scaler_topics.txt to open the scalertopics.txt file with GNU Nano. Edit Files with GNU Nano

Once you have opened the file in GNU Nano, you will see the contents of the file displayed in the terminal window.

Here are some essential commands and their subcommands to get you started:

CommandSubcommandDescriptionExample
Ctrl+OSave the changes made to the file.Press Ctrl+O and then Enter to save
Ctrl+XExit GNU Nano.Press Ctrl+X to exit the editor
Ctrl+KCut the current line.Press Ctrl+K to cut the line
Ctrl+UPaste the cut line.Press Ctrl+U to paste the line
Ctrl+WSearch for a specific term within the file.Press Ctrl+W to search
Ctrl+\Replace a specific term with another term.Press Ctrl+\ to replace
Ctrl+GDisplay the help menu.Press Ctrl+G for help

GNU Nano also displays a list of common commands at the bottom of the terminal window, making it easy to reference them while editing the file.

Here is an example:

  1. Let us say you have opened the scalertopics.txt file in GNU Nano.
    Edit Files with GNU Nano

  2. To make changes to the file, you can navigate using the arrow keys and modify the text directly in the editor.
    Edit Files with GNU Nano

  3. Once you have made the desired changes, press Ctrl+O to save the file. Edit Files with GNU Nano

  4. GNU Nano will prompt you to confirm the file name; press Enter to save the changes. Edit Files with GNU Nano

  5. To exit the editor, press Ctrl+X. Edit Files with GNU Nano

  6. GNU Nano may ask you to confirm if you want to save any unsaved changes before exiting.

Overall, GNU Nano is a straightforward and efficient text editor for editing files in Linux. Beginners often find it challenging to Linux edit files efficiently. Nano offers basic editing capabilities without overwhelming users with complex commands and options.

Edit Files Using Emacs Editor

Emacs is a powerful and extensible text editor used in the Linux community. It offers a wide range of functionality beyond basic text editing, including advanced features for programming, customization, and extensibility.

Linux edit file is an important task for system administrators and developers.

To edit a file using Emacs, follow these steps:

  1. Open a terminal window.
  2. Type emacs scaler_topics.txt to open the scaler_topics.txt file with Emacs. Edit Files Using Emacs Editor
  3. Once you have opened the file in Emacs, you will see the contents displayed in the Emacs editor window. Edit Files Using Emacs Editor

Here are some essential commands and their descriptions to help you get started:

CommandDescriptionExample
Ctrl + X, Ctrl + SSave the changes made to the file.Press Ctrl + X, then Ctrl + S to save changes.
Ctrl + X, Ctrl + CQuit Emacs and close the editor.Press Ctrl + X, then Ctrl + C to exit Emacs.
Ctrl + KCut the current line or selected region.Press Ctrl + K to cut the line or region.
Ctrl + YPaste the previously cut or copied text.Press Ctrl + Y to paste the text.
Ctrl + SSearch for a specific term within the file.Press Ctrl + S, then enter the search term.
Ctrl + RSearch backwards for a specific term within the file.Press Ctrl + R, then enter the search term.
Ctrl + SpaceSet a mark at the current position to make selections or edits.Press Ctrl + Space to set the mark.
Ctrl + WCut the selected region or marked text.Press Ctrl + W to cut the region or text.
Ctrl + YYank (paste) the previously cut or copied text.Press Ctrl + Y to paste the text.
Ctrl + GCancel the current command or operation and return to normal.Press Ctrl + G to cancel the current operation.

Here is an example of using Emacs:

Let's assume you have opened the scaler_topics.txt file in Emacs. The file contains the following lines:

Edit Files Using Emacs Editor

To make changes to the file using Emacs:

  1. Use the arrow keys to navigate through the file and position the cursor at the line
  1. Press Ctrl + Space to set the mark at the current position.
  2. Use the arrow keys to select the entire line or the desired portion of the text. Edit Files Using Emacs Editor
  3. Press Ctrl + W to cut the selected region. Edit Files Using Emacs Editor
  4. Move the cursor to the line below the selected line. Edit Files Using Emacs Editor
  5. Press Ctrl + Y to paste the previously cut text. Edit Files Using Emacs Editor
  6. Make the necessary modifications to the pasted text. Edit Files Using Emacs Editor
  7. To save the changes, press Ctrl + X, then Ctrl + S. Emacs will save the file and display it. Edit Files Using Emacs Editor Edit Files Using Emacs Editor

Emacs supports various programming modes, syntax highlighting, and integration with other development tools.

Conclusion

Linux edit file options vary depending on the chosen text editor. This article explores file editing in Linux using popular text editors: VI, GNU Nano, and Emacs.

Here are the key takeaways:

  • VI is a powerful command-line text editor found on most Linux distributions. It has three modes: Command, Insert, and Visual, and offers commands like i, Esc, :w, :q, dd, yy, and more.
  • GNU Nano is beginner-friendly and lightweight, featuring commands such as Ctrl+O to save, Ctrl+X to exit, Ctrl+K to cut, Ctrl+U to paste, and more.
  • Emacs is a highly customizable editor with advanced capabilities. It includes commands like Ctrl+X Ctrl+S to save, Ctrl+X Ctrl+C to exit, Ctrl+K to cut, Ctrl+Y to paste, and more.
  • VI excels in advanced editing, GNU Nano emphasizes simplicity, while Emacs offers extensive customization.
  • To edit files, open a terminal and use the appropriate command: vi for VI, nano for GNU Nano, or emacs for Emacs.

By mastering the usage of these text editors, you can efficiently edit files, perform various editing tasks, and enhance your productivity in Linux.