Editing Text with GNU nano

Throughout this book, you’ve created files with echo, touch, and cat, and you’ve edited files with sed, but sometimes you need to make more substantial modifications to existing files. Or, to be more honest, sometimes it’s just more comfortable to use a more familiar method of creating and editing text. GNU nano[14] is a basic text editor that’s great for beginners, and it’s available in a lot of environments out of the box. Let’s look at how to use nano to create and edit files, and how to configure it to enable more options.

 

Joe asks:
Joe asks:
Why not Vim or Emacs?

Both Vim and Emacs require a significant amount of time and effort to demonstrate. There are entire books devoted to each. Additionally, picking Vim over Emacs would upset Emacs fans, while using Emacs over Vim would upset Vim fans. So I decided it would be best to frustrate both and use nano instead.

On a more serious note, nano is the default shell editor on macOS and Ubuntu, so it’s worth learning, as you may encounter it. Once you outgrow it, explore Vim and Emacs and decide if they’re worth the investment.

 

Let’s use nano to create and edit a basic HTML file. Create the file nano.html using the nano command:

 $ ​​nano​​ ​​nano.html

The nano editor appears in your terminal:

images/environment/nano.png

Enter the following text in the editor:

 <!DOCTYPE html>
 <html lang=​"en-US"​>
  <head>
  <meta charset=​"utf-8"​>
  <title>Nano</title>
  </head>
  <body>
  <p>I made this in Nano</p>
  </body>
 </html>

Once you’ve entered the text, save the file by exiting the editor with Ctrl+x. You’ll be prompted to save the file. Press y, followed by Enter to accept.

To change the file later, open it again with nano.

nano looks like a basic editor when you first work with it, but it’s quite capable. It has some handy features you’ll want to turn on to make it more useful, like syntax highlighting. You can customize nano by creating a configuration file at ~/.nanorc.

On Ubuntu, nano already has syntax highlighting and some other features enabled by default. These settings are located in the file /etc/nanorc, which nano reads first. Changes in your local nano configuration will override these settings.

On macOS, the default version of nano does not support syntax highlighting, so you will need to install a newer version of nano using Homebrew to follow along:

 $ ​​brew​​ ​​install​​ ​​nano

This installs a more up-to-date version of nano at /usr/local/bin/nano. This directory is specified first in the default PATH variable on macOS, so when you use the nano command, it’s the one that will be run. You can verify this with which nano.

Use nano to create and edit the configuration file. That’s right—you’re going to use nano to edit nano’s configuration file. Good times.

 $ ​​nano​​ ​​~/.nanorc

First, enable syntax highlighting by including nano’s syntax highlighters. You’ll do this by including some additional configuration files, and the files you’ll include depend on which OS you’re using.

If you’re on Ubuntu, add this line to load all of the syntax highlighter files:

 include ​"/usr/share/nano/*.nanorc"

On macOS with nano installed via Homebrew, add this line instead:

 include /usr/local/share/nano/​*​.nanorc

Next, add this line which will allow you to suspend nano as a background process, which is useful if you need to run a command while nano is running:

 set suspend

You’ll learn more about suspending programs in Running and Managing Jobs.

Then add this line to turn off word-wrapping:

 set nowrap

Finally, add this to make nano show line numbers on the left-hand side:

 set linenumbers

Save the file and exit the editor (Ctrl+x, y, Enter.)

Now open your HTML file in nano again:

 $ ​​nano​​ ​​nano.html

You’ll now see line numbers and a nicely colorized output:

images/environment/nano-configured.png

Using nano, you can inspect and edit any file on your system without leaving your terminal. Let’s use nano to create files to configure the environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.21