You have got discovered a bunch of file operations to this point on this Terminal Fundamentals collection. You discovered to create new recordsdata, delete present ones, and replica and transfer them.
It’s time to take it to the subsequent stage. Let’s have a look at the right way to edit recordsdata within the Linux terminal.
If you’re writing bash shell scripts, you need to use the GUI textual content editors like Gedit and run them within the terminal.
However at occasions, you will end up in a state of affairs the place it’s a must to edit present recordsdata within the terminal itself. For instance, modifying config recordsdata situated within the /and many others listing.
As a desktop Linux person, you possibly can nonetheless use GUI editors for modifying config recordsdata whilst root. I am going to present it to you later.
Nonetheless, understanding the right way to edit recordsdata within the command line is best.
Modifying recordsdata in Linux terminal
Chances are you’ll use the cat command for those who simply have so as to add a couple of traces on the backside of an present file. However in an effort to correctly edit a file, you will want a correct textual content editor.
There may be merely no scarcity of terminal-based textual content editors in Linux. Vi, Vim, Nano, Emacs are just some of the most well-liked ones on the market.
However right here is the factor. All of them have a studying curve concerned. You do not have the consolation of the GUI. You do not have menus to work together with the editor along with your mouse.
As an alternative, it’s a must to use (and bear in mind) keyboard shortcuts.
I discover Nano to be a superb start line for brand new customers. It’s the default textual content editor in Ubuntu and plenty of different Linux distributions.
After all, there’s a studying curve, however it isn’t as steep as that of Vim or Emacs. It retains on displaying essentially the most related keyboard shortcuts on the backside. This helps you navigate even for those who do not bear in mind the precise shortcut.
Because of this, I will be masking absolutely the fundamentals of the Nano editor right here. You’ll study all of the necessities that you must know to start out utilizing Nano for modifying recordsdata within the Linux terminal.
Utilizing Nano editor
Nano can be utilized to edit textual content recordsdata, script recordsdata, program recordsdata and many others. Please keep in mind that it isn’t a phrase processor and can’t be used to edit docs or PDF recordsdata. For easy textual content modifying of conf recordsdata, scripts, or textual content recordsdata, Nano is a superb alternative.
🚧
It is best to have Nano put in in your system to comply with this tutorial.
I will be utilizing a textual content file named agatha_complete.txt. It consists of the names of all Agatha Christie’s books beneath her identify. You may obtain it from this hyperlink for those who plan to comply with the steps in your system.
Discover the Nano editor interface
Open the Nano editor with the next command:
nano
You’ll discover a brand new interface in your terminal that reads like GNU nano and shows New Buffer. New Buffer means Nano is engaged on a brand new file.
That is equal to opening a brand new unsaved file in a textual content editor like Gedit or Notepad.

Nano editor exhibits important keyboard shortcuts that you must use for modifying on the backside of the editor. This manner, you received’t get caught at exiting the editor like Vim.
The broader your terminal window, the extra shortcuts it exhibits.
It is best to get accustomed to the symbols in Nano.
- The caret image (^) means Ctrl key
- The M character imply the Alt key
📋
When it says ^X Exit
, it means to make use of Ctrl+X
keys to exit the editor. When it says M-U Undo
, it means use Alt+U
key to undo your final motion.
Yet another factor. It exhibits the characters in caps within the keyboard. However it doesn’t imply uppercase character. ^X means Ctrl + x key on the keyboard, not Ctrl+Shift+x key (to get the uppercase X).
You might also get an in depth assist doc contained in the editor by urgent Ctrl+G.

Now that you’re a bit accustomed to the interface, exit the Nano editor with Ctrl+X keys. Since you haven’t made any adjustments to this opened unsaved file, you received’t be requested to reserve it.
Superior! You now have some concepts concerning the editor. Within the subsequent part, you’ll study to create and edit recordsdata with Nano.
Create or open recordsdata in Nano
You may open a file for modifying in Nano like this:
nano filename
If the file doesn’t exist, it is going to nonetheless open the editor and if you exit, you’ll have the choice for saving the textual content to my_file.
You might also open a brand new file with none identify as nicely (like new doc) with Nano like this:
nano
Strive it. In a terminal, simply write nano
and enter.

Did you discover “New Buffer”? Because you didn’t give the file any identify, it signifies that may be a new, unsaved file within the reminiscence buffer.
You can begin writing or modifying the textual content straightaway in Nano. There aren’t any particular insert modes or something of that kind. It’s nearly like utilizing an everyday textual content editor, no less than for writing and modifying.
Should you make any adjustments to the file (new or present), you’ll discover that an asterisk (*) seems beside the file identify or New Buffer (which means a brand new, unsaved file).

That appears good. Within the subsequent part, you’ll see the right way to save recordsdata and exit the Nano editor interface.
Saving and exiting in Nano
Nothing is saved instantly to the file routinely until you explicitly achieve this. Whenever you exit the editor utilizing Ctrl+X keyboard shortcut, you’ll be requested whether or not you need to save the file.

- Y to avoid wasting the file and exit the editor
- N to discard adjustments
- C to cancel saving however proceed to edit
Should you select to avoid wasting the file by urgent the Y key, you’ll be requested to provide the file a reputation. Identify it my_file.txt.

📋
The .txt extension will not be needed as a result of the file is already a textual content file even when you don’t use the extension. Nonetheless, it’s a good observe to maintain the file extension for comprehension.
Enter the identify and press the enter key. Your file will probably be saved and also you’ll be out of the Nano editor interface. You may see that the textual content file has been created in your present listing.

📋
If you’re recurring of utilizing Ctrl+S for saving the file in a textual content editor and also you subconsciously press that in Nano, nothing occurs. Why “nothing occurs” is vital? As a result of for those who press Ctrl+S in a Linux terminal, it freezes the output display screen and you can not sort or do something. You will get again from this “frozen terminal” by urgent Ctrl+Q.
Carry out a “save as” operation in Nano
In Gedit or Notepad, you get the “save as” possibility to avoid wasting the adjustments made to an present file as a brand new file. This manner, the unique recordsdata stay unchanged and also you create a brand new file with the modified textual content.
You are able to do it in Nano editor too and the nice factor is that you simply don’t want to recollect one other keyboard shortcut. You should use the identical Ctrl+X keys that you simply used for saving and exiting.
Let’s see it in motion. Open the pattern file you had downloaded earlier.
nano agatha_complete.txt
Should you don’t make any adjustments, Ctrl+X will merely shut the editor. You don’t need that, do you?
So simply press enter after which backspace key. This can insert a brand new line after which delete it as nicely. This manner, nothing within the textual content file is adjustments and but Nano will see it as a modified file.
Should you press Ctrl+X and press Y to substantiate the save, you’ll come to the display screen the place it exhibits the file identify. What you are able to do is to vary the file identify right here by urgent the backspace key and typing a brand new identify.

It’s going to ask you to substantiate saving it beneath a unique identify. Press Y to substantiate this determination.

I named it agatha_complete.again as a sign that it’s a “backup” of a file of the identical identify. It’s only for comfort. There isn’t a actual significance behind the .again extension.
So, you will have discovered to avoid wasting recordsdata with Nano on this lesson. Within the subsequent part, you’ll study to maneuver round a textual content file.
Transferring round in a file
Open the agatha_complete.txt file with Nano. You understand how to open recordsdata with Nano editor, proper?
nano agatha_complete.txt
Now you will have a textual content file with a number of traces. How do you turn to different traces or to the subsequent web page or to the tip of the road?
Mouse clicks don’t work right here. Use the arrow keys to maneuver up and down, left and proper.
You should use the House key or Ctrl+A to maneuver to the start of a line and Finish key or Ctrl+E to maneuver to the tip of a line. Ctrl+Y/Web page Up and Ctrl+V/Web page Down keys can be utilized to scroll by pages.
- Use arrow keys for shifting round
- Use Ctrl+A or House key to go to the start of a line
- Use Ctrl+E or Finish key to go to the tip of a line
- Use Ctrl+Y or Web page Up keys to go up by one web page
- Use Ctrl+V or Web page Down keys to go down by one web page
You haven’t made any adjustments to the file. Exit it.
Now, open the identical file once more however utilizing this command:
nano -l agatha_complete.txt
Did you discover one thing completely different? The -l
possibility shows the road numbers within the left-hand facet.
Why did I present that to you? As a result of I would like you to study to go to a particular line now. To do this, use Ctrl+_ (underscore) key mixture.

📋
The Assist choices get modified on the backside. That’s the great thing about Nano. Should you select a particular keyboard shortcut, it begins exhibiting the choices that can be utilized with that key mixture.
Within the above image, you possibly can enter a line or column quantity. On the identical time, it exhibits which you could enter Ctrl+Y to go to the primary line of the file (it’s completely different from the common Ctrl+Y for shifting one web page up).
Utilizing Ctrl+T on the identical display screen, you possibly can go to a sure textual content. That’s nearly like trying to find a particular textual content.
And that brings us to the subject of the subsequent part, which is search and substitute.
Search and substitute
You continue to have the pattern textual content file opened, proper? If not, open it once more. Let’s the right way to to seek for textual content and substitute it with one thing else.
If you wish to seek for a sure textual content, use Ctrl+W after which enter the time period you need to search and press enter. The cursor will transfer to the primary match. To go to the subsequent match, use Alt+W keys.

By default, the search is case-insensitive. You may carry out a case-sensitive search by urgent Alt+C when you find yourself about to carry out a search.

As soon as once more, have a look at the underside for choices that can be utilized. Additionally word that it exhibits the final searched time period inside brackets.
Equally, you can even use regex for the search phrases by urgent Alt+R.
And lastly, use Ctrl+C to come back out of search mode.
If you wish to substitute the searched time period, use Ctr+ keys after which enter the search time period and press enter key.

Subsequent, it is going to ask for the time period you need to substitute the searched gadgets with.

The cursor will transfer to the primary match and Nano will ask to your conformation for changing the matched textual content. Use Y or N to substantiate or deny respectively. Utilizing both of Y or N will transfer to the subsequent match. You might also use A to exchange all matches.

Within the above textual content, I’ve changed the second prevalence of the time period Homicide with Marriage after which it asks whether or not I need to substitute the subsequent prevalence as nicely.
Use Ctrl+C to cease the search and substitute.
You have got made some adjustments to the textual content file on this lesson. However there is no such thing as a want to avoid wasting these adjustments. Press Ctrl+X to exit however don’t go for the save possibility.
Within the subsequent part, you’ll find out about reduce, copy and paste.
Minimize, copy and paste textual content
Open the pattern textual content file first.
💡
Should you don’t need to spend an excessive amount of time remembering the shortcuts, use the mouse.
Choose a textual content with mouse after which use the best click on menu to repeat the textual content. You might also use the Ctrl+Shift+C keyboard shortcut in Ubuntu terminal. Equally, you need to use the best click on and choose paste from the menu or use the Ctrl+Shift+V key mixture.
Nano additionally offers its personal shortcuts for chopping and pasting textual content however that would grow to be complicated for novices.
Transfer your cursor to the start of the textual content you need to copy. Press Alt+A to set a marker. Now use the arrow keys to spotlight the choice.
Upon getting chosen the specified textual content, you possibly can Alt+6 key to repeat the chosen textual content or use Ctrl+Ok to chop the chosen textual content. Use Ctrl+6 to cancel the choice.
Upon getting copied or reduce the chosen textual content, you need to use Ctrl+U to stick it.

If you don’t want to proceed choosing the textual content or copying it, use Alt+A once more to unset the mark.
To recall:
- You should use Ctrl+Shift+C to repeat and Ctrl+Shift+V to stick the content material of the clipboard in most Linux terminals.
- Alternatively, use Alt+A to set the marker, transfer the choice utilizing arrow key after which use Alt+6 to repeat, Ctrl+okay to chop and Ctrl+6 to cancel.
- Use Ctrl+U to stick the copied or reduce textual content.
Now you realize about copy-pasting. The subsequent part will educate you a factor or two about deleting textual content and contours in Nano.
Delete textual content or traces
There isn’t a devoted possibility for deletion in Nano. Chances are you’ll use the Backspace or Delete key to delete one character at a time. Press them repeatedly or maintain them to delete a number of characters. Identical to in any common textual content editor.
You may also use the Ctrl+Ok keys that cuts your entire line. Should you don’t paste it wherever, it’s pretty much as good as deleting a line.
If you wish to delete a number of traces, you could use Ctrl+Ok on all of them one after the other.
Another choice is to make use of the marker (Ctrl+A). Set the marker and transfer the arrow to pick out a portion of textual content. Use Ctrl+Ok to chop the textual content. No want to stick it and the chosen textual content will probably be deleted (in a approach).
Undo and redo
Minimize the fallacious line? Pasted the fallacious textual content choice? It’s simple to make such foolish errors and it’s simple to appropriate these foolish errors.
You may undo and redo your final actions utilizing:
You may repeat these key combos to undo or redo a number of occasions.
Nearly the tip…
Should you discover Nano overwhelming, it is best to attempt Vim or Emacs. You may begin liking Nano.
Fundamental Vim Instructions Each Linux Consumer Should Know [With PDF Cheat Sheet]
A complete information explaining primary vim instructions that will probably be helpful to any Linux person be it a sysadmin or a developer.

It is a good start line for Emacs. Give it a attempt in order for you.
Fundamental Emacs Command Defined in Element
Fighting Emacs? This detailed information offers you adequate details about Emacs instructions so as to begin utilizing Emacs editor easily.

Irrespective of how beginner-friendly Nano is, some individuals could discover the concept of modifying vital recordsdata within the terminal intimidating.
If you’re utilizing Linux desktop the place you possibly can entry a GUI editor, you need to use it to edit these vital recordsdata as root.
Say, you will have Gedit put in in your system and it’s a must to edit the SSH config file as root. You may run Gedit as root from the terminal like this:
sudo gedit /and many others/ssh/ssh_config
It’s going to open a Gedit occasion as root. The command retains on working within the terminal. Make your adjustments and save the file. It’s going to present warning messages if you save and shut Gedit.

We’re nearly on the finish of our terminal fundamentals collection. Within the tenth and the final chapter of the collection, you will find out about getting assist in the Linux terminal.
For now, let me know within the remark part for those who encounter any points.