← ServerPilot Docs

How to Use Nano to Edit Files

There are several text editors for the command line, but nano is probably the easiest to use.

Editing files at the command line can save you time as you don't need to download the files, edit them locally, and then upload them back to the server. Instead, you can just edit files directly on your server.

Before you edit a file, first make a copy of it!
That way, if you accidentally introduce errors, you have a backup you can restore.

To make a backup of a file, use the cp ("copy") command:

cp path/to/filename path/to/filename.bak

The above command creates a copy of the file in the same directory as the original file.

How to Open a File in nano

First, log into your server using SSH. If you are editing an app's files, log in as the app's system user. If you are editing system files, you will need to log in as root.

Be careful editing files as root!
ServerPilot can't provide support for customized servers.

Once you've logged in, you can open a file in nano by typing:

nano path/to/filename

For example:

nano apps/APPNAME/public/.htaccess

Using nano

Your terminal screen will now show the nano interface and the the contents of the file you're editing. At the bottom of the screen, you will see a list of commands (shortcuts) you can execute:

You'll only need to use a few of these commands.

The caret ("^") means the CTRL key. So, ^G means CTRL+g.

The biggest difference between nano and other editors is you can't use your mouse. Instead, you use your arrow keys, Home, End, Page Up, and Page Down to move the cursor around the page.

Using the arrow keys, move the cursor to the text you wish to change, then begin typing or erasing as you would normally in any text editor.

Getting Help

You can type CTRL+g to bring up useful Help Documentation. Use your arrow keys or the shortcuts to scroll through the documentation.

Press CTRL+x to exit the help documentation and return to editing your file.

Searching

To search for a word or string in the file, press CTRL+w ("where is") and nano will prompt you to enter what you're looking for:

This search is not case-sensitive. Searching for "DATAbase" will find the same strings as searching for "database".

Saving Your Work

You can save the file you're editing by typing CTRL+o ("write out"). You will be prompted for the name of the file to save. If you wish to overwrite the existing file, just press ENTER. If you want to save to a different filename, type in the different filename and press ENTER.

When you're done, exit nano by typing CTRL+x. Before exiting, nano will ask you if you wish to save the file:

Type y to save and exit, type n to abandon your changes and exit.

Tips

If you have difficulty holding CTRL and pressing another key at the same time, you can instead press and release the ESC key twice and then press the other key. In other words, the key sequence:

ESC ESC x

works the same in nano as:

CTRL+x

If you only want to view a file, not edit it, you can open the file in read-only mode with the -v argument:

nano -v path/to/filename

The -v means "View mode." If you try to make any changes while in view mode, you'll see the message "Key is invalid in view mode."

Where to Learn More

For most users, this tutorial covers all you'll want to know. If you'd like to learn about more of nano's features, check out the official documentation.

Last updated: April 20, 2018

Launch your first site in 5 minutes