Skip to content

Editing Text with nano

Life on the HPCC is filled with text files: batch scripts, configuration files, source code... Sooner or later you will have to create or edit one!

While there are options like VS Code that are closer to the types of text editors used on a normal desktop computer, they can be difficult to setup on the HPCC, and if you're just moving around using the command line, you might want to quickly drop in and edit a file without having to switch programs.

We recommend that new users try nano to edit files on the command line. While there are other text editors you can use directly from the shell (e.g., vim or emacs), nano is much more user-friendly, and easier to start with.

Opening a file

To open a file using nano, type

1
nano <filename>

into the shell, replacing <filename> with the file you'd like to edit. You can either give the full path to the file like

1
nano ~/my/important/text/file.txt

or if you're already in the directory where your file is, you can just use the filename, like

1
2
cd ~/my/important/text
nano file.txt

If the file doesn't exist, nano will create it for you.

Editing a file

Once the file is open, you can use the arrow keys, page up/down, and home/end to move around. You can type anywhere to add text.

Copying and pasting

To copy, first use the mouse to highlight text. Right click, and choose Copy from the menu.

To paste, move the cursor to the position you want to paste using the arrow keys. Right click, and choose Paste from the menu.

The keyboard shortcuts to copy and paste depend on your operating system and terminal program:

In MobaXterm:

Command Shortcut
Copy Text selected with the mouse will be copied by default
Paste shift + insert
Command Shortcut
Copy cmd + c
Paste cmd + v

This depends on your terminal, but usually the following work:

Command Shortcut
Copy ctrl + shift + c
Paste ctrl + shift + v

If you need to scroll through a file to select all of the text you need to copy, the shortcuts in nano become more complicated. A simpler way to do this is to exit nano, and enter

1
cat <filename>

on the command line.

This will output all contents of the filename you give to your command line, and you can select the part you want to copy with the mouse. The copy and paste shortcuts above will work in the same way.

Shortcuts

At the bottom of the screen, you will see some helpful key combinations:

1
2
^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Page ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where Is  ^V Next Page ^U UnCut Text^T To Spell

Each of these combinations starts with ^ symbolizing the ctrl key on your keyboard. For example, to exit, hold ctrl on your keyboard and press x. Even though nano shows an upper case X, you don't need to use the shift key.

The shortcut descriptions are meant to be mnemonic and match the key used, which can lead to some confusion. Some helpful shortcuts in plain language include:

Task Shortcut
Get help ^G
Exit nano ^X
Save a file ^O
Search for text ^W
Delete a line ^K
Paste line that was deleted ^T
Spell check ^T

Saving a file

To save a file, use ^O (that is, hold the ctrl key and press o). You will be asked what filename to save your file to, with the default being the already existing filename. Press the enter or return key without making any changes to the filename to save.

If you'd like to make a copy or save your edits in a new place, change the filename before accepting.

Exiting nano

To exit, use ^X (that is, hold the ctrl key and press x). If you exit before having saved the file, nano will ask if you would like to save before exiting. Press the y key to save changes, the n to discard your changes, or ^C to cancel.