TranslateProject/sources/tech/20230411.4 ⭐️⭐️ Linux Terminal Basics 9 Editing Files in Linux Terminal.md

17 KiB
Raw Permalink Blame History

Linux Terminal Basics #9: Editing Files in Linux Terminal

You have learned a bunch of file operations so far in this Terminal Basics series. You learned to create new files, delete existing ones, and copy and move them.

It is time to take it to the next level. Let's see how to edit files in the Linux terminal.

If you are writing bash shell scripts, you can use the GUI text editors like Gedit and run them in the terminal.

But at times, you'll find yourself in a situation where you have to edit existing files in the terminal itself. For example, modifying config files located in the /etc directory.

As a desktop Linux user, you could still use GUI editors for editing config files even as root. I'll show it to you later.

However, knowing how to edit files in the command line is better.

Editing files in Linux terminal

You may use the cat command if you just have to add a few lines at the bottom of an existing file. But in order to properly edit a file, you'll need a proper text editor.

There is simply no shortage of terminal-based text editors in Linux. Vi, Vim, Nano, Emacs are just a few of the most popular ones out there.

But here is the thing. All of them have a learning curve involved. You don't have the comfort of the GUI. You don't have menus to interact with the editor with your mouse.

Instead, you have to use (and remember) keyboard shortcuts.

I find Nano to be a good starting point for new users. It is the default text editor in Ubuntu and many other Linux distributions.

Of course, there is a learning curve, but it is not as steep as that of Vim or Emacs. It keeps on displaying the most relevant keyboard shortcuts at the bottom. This helps you navigate even if you don't remember the exact shortcut.

For this reason, I'll be covering the absolute basics of the Nano editor here. Youll learn all the essentials you need to know to start using Nano for editing files in the Linux terminal.

Using Nano editor

Nano can be used to edit text files, script files, program files etc. Please remember that it is not a word processor and cannot be used to edit docs or PDF files. For simple text editing of conf files, scripts, or text files, Nano is a great choice.

🚧 You should have Nano installed on your system to follow this tutorial.

I'll be using a text file named agatha_complete.txt. It consists of the names of all Agatha Christies books under her name. You can download it from this link if you plan to follow the steps on your system.

Agatha completeSample text fileagatha_complete.txt3 KBdownload-circle

Explore the Nano editor interface

Open the Nano editor with the following command:

nano

Youll notice a new interface in your terminal that reads like GNU nano and displays New Buffer. New Buffer means Nano is working on a new file.

This is equivalent to opening a new unsaved file in a text editor like Gedit or Notepad.

Nano editor interface

Nano editor shows essential keyboard shortcuts you need to use for editing at the bottom of the editor. This way, you wont get stuck at exiting the editor like Vim.

The wider your terminal window, the more shortcuts it shows.

You should get familiar with the symbols in Nano.

  • The caret symbol (^) means Ctrl key
  • The M character mean the Alt key

📋 When it says ^X Exit, it means to use Ctrl+X keys to exit the editor. When it says M-U Undo, it means use Alt+U key to undo your last action.

One more thing. It shows the characters in caps in the keyboard. But it doesnt mean uppercase character. ^X means Ctrl + x key on the keyboard, not Ctrl+Shift+x key (to get the uppercase X).

You may also get a detailed help document inside the editor by pressing Ctrl+G.

Getting help in Nano editor

Now that you are a bit familiar with the interface, exit the Nano editor with Ctrl+X keys. Since you have not made any changes to this opened unsaved file, you wont be asked to save it.

Awesome! You now have some ideas about the editor. In the next section, youll learn to create and edit files with Nano.

Create or open files in Nano

You can open a file for editing in Nano like this:

nano filename

If the file doesnt exist, it will still open the editor and when you exit, youll have the option for saving the text to my_file.

You may also open a new file without any name as well (like new document) with Nano like this:

nano

Try it. In a terminal, just write nano and enter.

New file in Nano editor

Did you notice “New Buffer”? Since you did not give the file any name, it indicates that is a new, unsaved file in the memory buffer.

You can start writing or modifying the text straightaway in Nano. There are no special insert modes or anything of that sort. It is almost like using a regular text editor, at least for writing and editing.

If you make any changes to the file (new or existing), youll notice that an asterisk (*) appears beside the file name or New Buffer (meaning a new, unsaved file).

Writing text in Nano editor

That seems good. In the next section, youll see how to save files and exit the Nano editor interface.

Saving and exiting in Nano

Nothing is saved immediately to the file automatically unless you explicitly do so. When you exit the editor using Ctrl+X keyboard shortcut, youll be asked whether you want to save the file.

Save new file in Nano

  • Y to save the file and exit the editor
  • N to discard changes
  • C to cancel saving but continue to edit

If you choose to save the file by pressing the Y key, youll be asked to give the file a name. Name it my_file.txt.

Saving a new file in Nano text editor

📋 The .txt extension is not necessary because the file is already a text file even if you do not use the extension. However, it is a good practice to keep the file extension for comprehension.

Enter the name and press the enter key. Your file will be saved and youll be out of the Nano editor interface. You can see that the text file has been created in your current directory.

New file created in Nano

📋 If you are habitual of using Ctrl+S for saving the file in a text editor and you subconsciously press that in Nano, nothing happens. Why “nothing happens” is important? Because if you press Ctrl+S in a Linux terminal, it freezes the output screen and you cannot type or do anything. You can get back from this “frozen terminal” by pressing Ctrl+Q.

Perform a “save as” operation in Nano

In Gedit or Notepad, you get the “save as” option to save the changes made to an existing file as a new file. This way, the original files remain unchanged and you create a new file with the modified text.

You can do it in Nano editor too and the good thing is that you dont need to remember another keyboard shortcut. You can use the same Ctrl+X keys that you used for saving and exiting.

Lets see it in action. Open the sample file you had downloaded earlier.

nano agatha_complete.txt

If you dont make any changes, Ctrl+X will simply close the editor. You dont want that, do you?

So just press enter and then backspace key. This will insert a new line and then delete it as well. This way, nothing in the text file is changes and yet Nano will see it as a modified file.

If you press Ctrl+X and press Y to confirm the save, youll come to the screen where it shows the file name. What you can do is to change the file name here by pressing the backspace key and typing a new name.

Save as different filename in Nano editor

It will ask you to confirm saving it under a different name. Press Y to confirm this decision.

Save as different filename in Nano editor

I named it agatha_complete.back as an indication that it is a “backup” of a file of the same name. Its just for convenience. There is no real significance behind the .back extension.

So, you have learned to save files with Nano in this lesson. In the next section, youll learn to move around a text file.

Moving around in a file

Open the agatha_complete.txt file with Nano. You know how to open files with Nano editor, right?

nano agatha_complete.txt

Now you have a text file with several lines. How do you switch to other lines or to the next page or to the end of the line?

Mouse clicks dont work here. Use the arrow keys to move up and down, left and right.

You can use the Home key or Ctrl+A to move to the beginning of a line and End key or Ctrl+E to move to the end of a line. Ctrl+Y/Page Up and Ctrl+V/Page Down keys can be used to scroll by pages.

  • Use arrow keys for moving around
  • Use Ctrl+A or Home key to go to the beginning of a line
  • Use Ctrl+E or End key to go to the end of a line
  • Use Ctrl+Y or Page Up keys to go up by one page
  • Use Ctrl+V or Page Down keys to go down by one page

You have not made any changes to the file. Exit it.

Now, open the same file again but using this command:

nano -l agatha_complete.txt

Did you notice something different? The -l option displays the line numbers in the left-hand side.

Why did I show that to you? Because I want you to learn to go to a specific line now. To do that, use Ctrl+_ (underscore) key combination.

📋 The Help options get changed at the bottom. Thats the beauty of Nano. If you choose a special keyboard shortcut, it starts showing the options that can be used with that key combination.

In the above picture, you can enter a line or column number. At the same time, it shows that you can enter Ctrl+Y to go to the first line of the file (it is different from the regular Ctrl+Y for moving one page up).

Using Ctrl+T on the same screen, you can go to a certain text. Thats almost like searching for a specific text.

And that brings us to the topic of the next section, which is search and replace.

Search and replace

You still have the sample text file opened, right? If not, open it again. Lets how to to search for text and replace it with something else.

If you want to search for a certain text, use Ctrl+W and then enter the term you want to search and press enter. The cursor will move to the first match. To go to the next match, use Alt+W keys.

Search for text in Nano editor

By default, the search is case-insensitive. You can perform a case-sensitive search by pressing Alt+C when you are about to perform a search.

Case sensitive search in Nano editor

Once again, look at the bottom for options that can be used. Also note that it shows the last searched term inside brackets.

Similarly, you can also use regex for the search terms by pressing Alt+R.

And lastly, use Ctrl+C to come out of search mode.

If you want to replace the searched term, use Ctr+\ keys and then enter the search term and press enter key.

Search and replace text in Nano

Next, it will ask for the term you want to replace the searched items with.

Enter text to be replaced with in Nano

The cursor will move to the first match and Nano will ask for your conformation for replacing the matched text. Use Y or N to confirm or deny respectively. Using either of Y or N will move to the next match. You may also use A to replace all matches.

Replacing text in Nano editor

In the above text, I have replaced the second occurrence of the term Murder with Marriage and then it asks whether I want to replace the next occurrence as well.

Use Ctrl+C to stop the search and replace.

You have made some changes to the text file in this lesson. But there is no need to save those changes. Press Ctrl+X to exit but dont go for the save option.

In the next section, youll learn about cut, copy and paste.

Cut, copy and paste text

Open the sample text file first.

💡 If you dont want to spend too much time remembering the shortcuts, use the mouse.

Select a text with mouse and then use the right click menu to copy the text. You may also use the Ctrl+Shift+C keyboard shortcut in Ubuntu terminal. Similarly, you can use the right click and select paste from the menu or use the Ctrl+Shift+V key combination.

Nano also provides its own shortcuts for cutting and pasting text but that could become confusing for beginners.

Move your cursor to the beginning of the text you want to copy. Press Alt+A to set a marker. Now use the arrow keys to highlight the selection.

Once you have selected the desired text, you can Alt+6 key to copy the selected text or use Ctrl+K to cut the selected text. Use Ctrl+6 to cancel the selection.

Once you have copied or cut the selected text, you can use Ctrl+U to paste it.

Cut, copy and paste in Nano editor

If you do not want to continue selecting the text or copying it, use Alt+A again to unset the mark.

To recall:

  • You can use Ctrl+Shift+C to copy and Ctrl+Shift+V to paste the content of the clipboard in most Linux terminals.
  • Alternatively, use Alt+A to set the marker, move the selection using arrow key and then use Alt+6 to copy, Ctrl+k to cut and Ctrl+6 to cancel.
  • Use Ctrl+U to paste the copied or cut text.

Now you know about copy-pasting. The next section will teach you a thing or two about deleting text and lines in Nano.

Delete text or lines

There is no dedicated option for deletion in Nano. You may use the Backspace or Delete key to delete one character at a time. Press them repeatedly or hold them to delete multiple characters. Just like in any regular text editor.

You can also use the Ctrl+K keys that cuts the entire line. If you dont paste it anywhere, its as good as deleting a line.

If you want to delete multiple lines, you may use Ctrl+K on all of them one by one.

Another option is to use the marker (Ctrl+A). Set the marker and move the arrow to select a portion of text. Use Ctrl+K to cut the text. No need to paste it and the selected text will be deleted (in a way).

Undo and redo

Cut the wrong line? Pasted the wrong text selection? Its easy to make such silly mistakes and its easy to correct those silly mistakes.

You can undo and redo your last actions using:

  • Alt+U : Undo
  • Alt+E : Redo

You can repeat these key combinations to undo or redo multiple times.

Almost the end...

If you find Nano overwhelming, you should try Vim or Emacs. You'll start liking Nano.

This is a good starting point for Emacs. Give it a try if you want.

No matter how beginner-friendly Nano is, some people may find the idea of editing important files in the terminal intimidating.

If you are using Linux desktop where you can access a GUI editor, you can use it to edit those important files as root.

Say, you have Gedit installed on your system and you have to edit the SSH config file as root. You can run Gedit as root from the terminal like this:

sudo gedit /etc/ssh/ssh_config

It will open a Gedit instance as root. The command keeps on running in the terminal. Make your changes and save the file. It will show warning messages when you save and close Gedit.

Using gedit to edit config files

We are almost at the end of our terminal basics series. In the tenth and the last chapter of the series, you'll learn about getting help in the Linux terminal.

For now, let me know in the comment section if you encounter any issues.


via: https://itsfoss.com/edit-files-linux/

作者:Abhishek Prakash 选题:lkxed 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出