Use nano to edit files
There are several popular command-line text editors.
Of these, nano
is the easiest to use.
For additional options and functionality, see also
the nano
documentation.
Back up a file before editing
To make a backup of a file before editing,
use the cp
command.
cp path/to/filename path/to/filename.bak
Open a file for editing
To open a file for editing,
use the path to the file as the argument to nano
.
nano path/to/filename
For example:
nano apps/APPNAME/public/.htaccess
The nano interface
Once you’ve opened a file for editing,
your terminal will now show the nano
interface and
the contents of the file you’re editing.
To move the cursor, use the arrow keys on your keyboard.
At the bottom of the screen,
you will see a list of commands.
The caret (^
) in a command’s keyboard shortcut means to hold the Ctrl key
while you press the shortcut’s letter.
The only commands you will need to use are:
- Write out (Ctrl-o) — Save the contents of the file you’re editing.
- Exit (Ctrl-x) — Exit
nano
.
As an alternative to using the Ctrl key combinations, you can instead press the Esc key twice and then press the shortcut’s letter key.
- Esc Esc o — Save the contents of the file you’re editing.
- Esc Esc x — Exit
nano
.
Save and exit
To save a file you’re editing, press Ctrl-o. That is, hold down the Ctrl key and press the o key. You’ll be shown a prompt with the name of the file you’re editing. Press Enter to confirm saving to the file.
To exit nano
,
press Ctrl-x.
That is, hold down the Ctrl key and press the x key.
When you exit nano
,
you’ll be asked the following question.
Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?
This is asking if you want to save the file. Press Y to save and then exit. Press N to exit without saving.
View mode
To view a file without editing it,
use the -v
argument to nano
.
nano -v path/to/filename
If you try to make changes to a file while in view mode, you’ll see the message “Key is invalid in view mode.”