Nano is a command-line text editor that has been a staple in the Linux ecosystem since its creation. Its story dates back to the mid-1990s when Chris Allegretta, a developer working on the popular Linux distribution Emacs, decided to create a simpler text editor for users who didn’t want to deal with the complexities of other editors like Vim or Emacs. The result was a lightweight, easy-to-use, and accessible text editor that quickly became popular. Despite the constant debate within the Linux community about the best text editor, Nano has remained a solid and reliable choice in Unix-based systems, used by thousands of administrators and developers for its simplicity.

If you’ve ever needed to edit configuration files, scripts, or web pages on a server, you know how tedious the process can be when using FTP. In FTP, you need to download the file to your computer, edit it, and then upload it again to replace the original file. While this method works, it’s not the most efficient. Fortunately, there is a faster and simpler solution: SSH and Nano.

What is Nano?

Nano is a text editor that works directly from the command line, meaning you don’t need a graphical user interface to use it. This makes it ideal for servers that don’t have a graphical environment or for machines with limited resources. Unlike other editors like Vim or Emacs, Nano is extremely easy to learn and use, especially for those new to the command line.

Although there are many other text editors available, Nano has managed to maintain popularity, largely due to its simplicity and speed. According to a recent survey by StackOverflow, Nano is still one of the most popular text editors among Linux users, with more than 9% of users regularly using it. This shows that, while there are many alternatives, Nano remains a reliable option for many users in the world of system administration and development.

Prerequisites

Before you start editing files over SSH using Nano, you should ensure you have the following:

  1. Access to a Remote Server: You need access to a server that you can connect to via SSH. Any server supporting SSH will work, whether it’s running Linux, Windows, or macOS. You also need to know the server’s IP address or hostname, your username and password (or SSH key), and the SSH port number (usually 22).
  2. Terminal Emulator: You need a terminal emulator on your local machine to run SSH commands. You can use any terminal emulator you’re comfortable with, such as iTerm, PuTTY, Terminal (macOS), or PowerShell (Windows).

Once you have all the prerequisites, you are ready to start editing files over SSH using Nano. Let’s dive in!

How to Connect to a Remote Server Using SSH

To connect to your remote server via SSH, open your terminal emulator and type the following command:

ssh username@hostname -p port

Replace username with your remote server’s username, hostname with the IP address or hostname of the server, and port with the SSH port number (typically 22). For example:

ssh maria@192.168.1.100 -p 22

After running the command, you will be prompted to enter your password or SSH key passphrase. Once logged in, you should see a welcome message and the command prompt of your remote server.

How to Open a File in Nano

Once logged into your server, you can begin editing files. To open a file in Nano, use the following command:

nano filename

Replace filename with the name of the file you want to edit. If the file doesn’t exist, Nano will create a new one. If the file does exist, Nano will open it and display its contents.

How to Save and Exit Nano

After editing the file, you can save it by pressing Ctrl + O. Nano will prompt you to confirm the file name and then save it. Press Enter to confirm.

To exit Nano, press Ctrl + X. If you’ve made changes, Nano will ask if you want to save the file before exiting. You can press Y to save the file or N to discard changes.

How to Search and Replace Text in Nano

To search for specific text in the file, press Ctrl + W and type the text you want to find. Nano will highlight the first occurrence and move the cursor to it. You can press Ctrl + W again to find the next occurrence, or Alt + W to find the previous occurrence.

To replace text, press Ctrl + \. Enter the text you want to replace and then the replacement text. Nano will prompt you to confirm replacing the first occurrence. You can press Y to replace it, N to skip it, A to replace all occurrences, or Ctrl + C to cancel.

How to Copy, Cut, and Paste Text in Nano

To copy text, first highlight it by pressing Alt + A, then move the cursor to the end of the text. The highlighted text will appear in color. Next, press Alt + 6 to copy the text to the clipboard. To cut the text, press Ctrl + K after selecting the text.

To paste the text, move the cursor to where you want to insert it and press Ctrl + U.

Undo and Redo in Nano

If you make a mistake while editing, you can undo changes by pressing Alt + U. To redo changes, press Alt + E. Undo and redo only work for the current session, so if you save and exit the file, the history is cleared.

How to Enable Syntax Highlighting in Nano

Syntax highlighting makes code easier to read by coloring different parts according to their function. Nano supports syntax highlighting for various programming languages, including PHP, Python, and C.

To enable syntax highlighting in Nano, use the -Y option when opening the file and specify the language:

nano -Y php wp-config.php

This will open the file with syntax highlighting for PHP.

Other Useful Nano Commands and Shortcuts

Nano offers many useful commands and shortcuts that can make editing faster. You can see a list of commands at the bottom of the screen. The ^ symbol means Ctrl. For example, ^G means Ctrl + G.

Here are some other useful shortcuts:

  • Ctrl + G: Display the help menu
  • Ctrl + C: Display the current line and column number
  • Ctrl + R: Read a file and insert it into the current file
  • Ctrl + Y: Go to the previous page
  • Ctrl + V: Go to the next page
  • Ctrl + A: Move to the beginning of the current line
  • Ctrl + E: Move to the end of the current line
  • Ctrl + Z: Suspend Nano and return to the shell

You can also use the arrow keys, Page Up, Page Down, Home, and End keys to navigate the file.

Conclusion

In this guide, you’ve learned how to use the Nano text editor, a simple and efficient command-line tool for editing files on remote servers. You’ve learned how to open, create, and save files, edit text, search and replace text, cut and paste text, and more. You also learned some of the most important keyboard shortcuts to make your editing experience smoother and faster.

Nano is a perfect tool for editing files directly from the terminal without needing a graphical interface. It’s lightweight, easy to use, and extremely useful for server administration, especially when working with headless servers or systems with limited resources.

With these tips, you can now edit configuration files and scripts remotely with confidence and efficiency. Happy editing!

Scroll to Top