- How to Edit Files on Ubuntu Command Line?
- How to Edit Files Using “nano” Text Editor?
- 1. Selection of Particular Text
- 2. Copy/Cut and Paste the Text
- 3. Search and Replace Word
- 4. Save and Close the File
- How to Edit Files Using “vim” Text Editor?
- How to Install Vim on Ubuntu?
- Vim Editor Mode
- 1. Selection of Particular Text
- 2. Copy/Cut and Paste Content
- Paste
- 3. Search and Replace Word
- 4. Save and Close File
- Conclusion
- My Favorite Command Line Editors for Linux – What’s Your Editor?
- 1. Vi/Vim Editor
- Install Vi/Vim Editor in Linux
- 2. Nano Editor
- Install Nano Editor in Linux
- 3. Emacs Editor
- Install Emacs Editor in Linux
- Conclusion
How to Edit Files on Ubuntu Command Line?
The editing of files is an important process of writing that involves a detailed review of the document. It helps make the content more accurate, eliminating spelling, grammatical, and punctuation mistakes, and increasing its readability. Like other Linux distro, the Debian-based operating system Ubuntu assists the user in editing files through the command line or graphical user interface.
This post tells the possible methods that are helpful to edit files in Ubuntu using the command line
How to Edit Files Using “nano” Text Editor?
The “nano” is a commonly used powerful default text editor of Ubuntu that helps to create and edit the text/code and configuration files. It offers a large list of pre-defined shortcut keys for file handling.
Let’s take a sample file in the “nano” editor and edit it in different ways:
Sample File
A “File1.txt” is taken as a sample file opened in the “nano” text editor:
The “nano” editor shows the shortcut keys that can be easily invoked using the “Ctrl” key:
1. Selection of Particular Text
Place the mouse pointer at the particular line/paragraph that needs to be selected. Use the “Alt+A” shortcut key that indicates the mark of selection:
Start selecting the text with the help of the keyboard “right/left-arrow” key:
The desired line has been selected.
2. Copy/Cut and Paste the Text
Copy and cut is the most commonly used file operation. The “nano” editor offers the “Alt+6” to copy the text and “Ctrl+U” to paste:
The selected text has been copied and pasted at the end of the file. To cut the selected part, utilize the “Ctrl+K” shortcut key.
3. Search and Replace Word
To search a specific word/character, press the “Ctrl+\” shortcut key. It prompts the pattern or word that you want to search for. Type the word in the search bar:
For instance, the searched word is “This”. Hit the “Enter” key and enter the word of user choice in the “Replace with” bar, i.e., “it”:
The searched word “This” will be highlighted in the file. Type the “Y” letter for “yes” and “A” for all the instances:
The notification bar shows that the word “This,” having “3” total occurrences, has been replaced with “It.”
4. Save and Close the File
Press “Ctrl+S” to save the code and “Ctrl+X” to exit the editor from the terminal. For a detailed overview of “nano” editor working and usage, read our guide “How to Use nano Editor in Linux?”
How to Edit Files Using “vim” Text Editor?
“Vim” is another open-source, well-reputed text editor for editing text files, scripts, and configuration files. in Linux. It refers to the enhanced version of the original “vi” text editor and is also called “Vi Improved.”
How to Install Vim on Ubuntu?
Vim is an external application that can be easily installed from the default repository of Ubuntu using the “apt” command:
The installation of “vim” has been completed. Let’s see if it can edit the files in Ubuntu
Sample File
The existing “File2.txt” is opened in the “vim” editor in this way:
Vim Editor Mode
The “vim” editor provides three modes to handle the files that are stated below:
- Command Mode: It supports the execution of commands like find, undo, replace, redo, and many others. By default, the command mode is enabled; press “Esc” to move back from it.
- Insert Mode: It allows text insertion into a file at the cursor point. Type “i” to enter in the insert mode and the “Esc” key to turn it off.
- Visual Mode: It selects text to cut, delete, copy, and many other operations. Press “Ctrl+V” to enable the visual mode.
1. Selection of Particular Text
Enter into the “VISUAL” mode by pressing the “Shift+V” key. Type the lowercase letter “v” to select the specific text and uppercase “V” for the whole line:
The whole line has been selected.
2. Copy/Cut and Paste Content
To copy the text, press the small letter “y,” and for the whole line of text, use the capital “Y” letter. Use the small “d” to cut the specific text and the capital “D” for the whole line/paragraph:
The highlighted line has been copied.
Paste
Same as the copy and cut, the paste operation requires lowercase “p” to paste the text at a particular position and uppercase “P” to paste at the cursor point.
The copy-and-paste operation has been done successfully.
3. Search and Replace Word
Vim editor supports the “%s” parameter for searching the desired word/character and uses “g” to replace the entered word globally:
In this scenario, the word “freedom” is replaced with “privilege” in the whole file:
The “4” substitution shows the replacement of “privilege” in place of “freedom” in the whole file.
Note: The “vim” editor supports a large variety of commands for editing operations. For more editing commands, read this guide, “Top 25 Vim commands for Linux”
4. Save and Close File
When the editing of the file is completed, enter into the “COMMAND” mode and press the “:wq” to exit and save the file with changes:
Note: For more ways of closing and exiting the vim editor follow this detailed guide on “10 Quick Ways to Exit Vim Editor”
Conclusion
The Ubuntu operating system supports a large list of command line text editors from which most commonly “nano” and “vim” are used to edit files. The “nano” editor comes pre-installed in Ubuntu, whereas the “vim” corresponds to an external tool that can be easily installed from the default repository. The “nano” editor is the most recommended editor for beginners because of its shortcut keys as compared to vim commands.
This guide has provided the command line utilities to edit files in ubuntu.
My Favorite Command Line Editors for Linux – What’s Your Editor?
Knowing how to fast and effectively edit files via command line is vital for every Linux system administrator. File edits are performed on a daily basis, whether it’s a configuration file, user file, text document or whatever file you need to edit.
This is why it is good idea to pick a favourite command line text editor and master it. It’s good to know how to work with other text editors, but you should master at least one so you can perform more complex tasks when needed.
In this tutorial, we are going to show you the most common command line text editors in Linux and show you their pros and cons.
Note however that we will not cover a complete guide how to work with each one of them as this can be a complete other article with explanation.
1. Vi/Vim Editor
First in our list is the infamous Vi/Vim (Vim comes from Vi improved). This is a very flexible text editor that can perform many different operations on text.
For example you can use regular expressions to replace text snippets in a file using vim. This of course is not the only benefit. Vi(m) provides an easy way to navigate between lines, words paragraphs. It also includes text highlighting.
Vim may not be the most user friendly text editor, but it is often preferred by developers and Linux power users. If you want to install this command line text editor on your system, you can use the command associated with your OS:
Install Vi/Vim Editor in Linux
$ sudo apt-get install vim [On Debian and its derivatives] # yum install vim [On RedHat based systems] OR # dnf install vim [On newer Fedora 22+ versions]
If you want to see our complete coverage of vi(m), please follow the links below:
2. Nano Editor
Nano is probably one of the most used command line text editors. The reason for this is it’s simplicity and the fact that it’s preinstalled in most of the Linux distributions.
Nano doesn’t have vim’s flexibility, but it will definitely do the work if you need to edit a large file. Actually pico and nano are quite similar. Both have their command options displayed at the bottom so you can choose which one to run. Commands are completed with key combinations of Ctrl and a letter displayed at the bottom.
Nano has the following features that you can use out of the box:
- Get Help
- Write out
- Justify
- Read File
- Where is (search)
- Previous page
- Next page
- Cut Text
- Uncut Text
- Cur Pos (Current position)
- Spell check
Install Nano Editor in Linux
$ sudo apt-get install nano [On Debian and its derivatives] # yum install nano [On RedHat based systems] OR # dnf install nano [On newer Fedora 22+ versions]
You can check our complete guide for editing files with Nano editor on this link:
3. Emacs Editor
This is probably the most complex text editor in our list. It’s the oldest command line editor available for both Linux and UNIX based systems. Emacs can help you be more productive by providing an integrated environment for different kinds of tasks.
At first the user interface may look somehow confusing. The good thing is that emacs has a very detailed manual that will help you with file navigation, edits, customization, setting up commands. Emacs is the ultimate tool used by advanced *Nix users.
Here are some of the features that make it the preferred choice over the previous editors we mentioned:
- Emacs server platform enables multiple hosts to connect to the same Emacs server and share the buffer list.
- Powerful and extensible file manager.
- Customization beyond a regular editor – as some say it’s an OS within the OS.
- Commands customization.
- Can change to Vi(m) like mode.
Emacs is a multi-platform editor and can be easily installed with the commands shown below:
Install Emacs Editor in Linux
$ sudo apt-get install emacs [On Debian and its derivatives] # yum install emacs [On RedHat based systems] OR # dnf install emacs [On newer Fedora 22+ versions]
Note: In Linux Mint 17 I had to run the following command to complete the installation:
$ sudo apt-get install emacs23-nox
Conclusion
There are other command line editors, but they barely even reach the functionality that the above 3 provide. Whether you are a Linux newbie or a Linux guru, you will most definitely need to learn at least one of the above mentioned editors. If we’ve missed any command-line editor in this article, please don’t forget to inform us via comments.