Using Nano Editor on Linux

Using Nano Editor on Linux

GNU nano is an easy to use command line text editor for Unix and Linux operating systems(including Oracle Linux). It includes all the basic functionality you’d expect from a regular text editor, like syntax highlighting, multiple buffers, search and replace with regular expression support, spellchecking, UTF-8 encoding, and more. It is a popular text editor among Linux users and has a small learning curve.You can also undo and redo actions as well as record macros(a set of actions) for reuse.
In this blog I will explain some of the useful commands in Nano to help you get started, grouped into various categories. There are a lot more commands but I figure these ae some of the most useful.

Starting out

To start nano on linux, simply enter:

nano

This creates an empty buffer and you can start typing commands in.

To create a new file or edit an existing file, use:

  • nano <filename>
    NB: At the bottom of the nano window, there is a list of the most basic command shortcuts to use with the nano editor.

All commands are prefixed with either ^ or M character.

  1. The caret symbol (^) represents the Ctrl key. For example, the ^J commands mean to
    press the Ctrl and J keys at the same time.
  2. The letter M represents the Alt key.

Getting Help

To get a list of commands, use:
Ctrl+G — Show Nano help

Searching and Replacing

  • to search for text, Use Ctrl+W — Start a search; type the search string and press Enter.
  • To move to the next match Use Alt+W — Find next match

To Search and Replace

  • Ctrl+\ : It will prompt you to enter the search string, followed by what you want it replaced
    with. It then moves through the matches, asking you to replace this occurrence with “Y”, or to
    skip with “N” and you can type “A” to replace all occurences. Do Ctrl+c to stop.

Copying, cutting, and pasting

To select text move your cursor to the text and do:

  • Alt+A : Set or toggle the selection mark; use arrow keys to expand the selection.
  • Ctrl+6 : Toggle the selection mark (press again to cancel the selection).
  • Alt+6 : Copy the selected text or current line to Nano’s cutbuffer.
  • Ctrl+K : Cut the current line or selected text to Nano’s cutbuffer.
  • Ctrl+U : Paste from Nano’s cutbuffer.
  • Ctrl+R : Insert (read) another file into the current buffer; then type the filename and press Enter.
  • Alt+T : Cut from the cursor to the end of the current line or according to your Nano version; behavior can vary.

Saving and Exiting

  • Ctrl+O : Write (save) the file (recommended; prompts for filename if new).
  • Ctrl+X : Exit; if there are unsaved changes Nano will prompt to save (Y/N) and then ask for a filename.

Navigating in Nano

  • Ctrl+A Moves to the beginning of the line
  • Ctrl+E moves to the end of the line
  • Ctrl+Y page up
  • Ctrl+V page down
  • Ctrl+B move back one character
  • Ctrl+F move forward one character
  • Ctrl+P move to previous line
  • Ctrl+N move to the next line
  • Ctrl+J Justify a paragraph
  • Ctrl+A Move to the beginning of the line. (Use one canonical Ctrl+A entry.)

You can also move around in nano using the arrow keys .

Recording Macros

What is a macro?

A macro is a sequence of commands which can be captured by the editor and then re-ran.

Why use them?

Sometimes , while editing code you may need to do the same thing on a couple of lines that is not quite as straightforward as searching for and replacing text. Imagine editing a table DDL SQL script and you may need to change a couple of NUMBER columns to VARCHAR2(10 CHAR) for example and put a comma at the end of the changed line. You can instruct Nano to learn this sequence of command (recording a macro) you use and then re-do the same commands for each line you modify.

To start recoding a macro, use:

  • Alt+: (Alt+Shift+;)

Then execute the series of keystrokes you’d like to be record and then to stop, press:

  • Alt+: (Alt+Shift+;)

again to end the recording.

To replay the macro, use;

  • Alt+;

Macros are really great for editing a file where a lot of repitive tasks are required.

Miscellaneous

  • ALT+U undo the last operation
  • ALT+E redo the last operation
  • ALT+shift+] indent text (just use tab)
  • ALT+shift+[ Unindent selection
  • Ctrl+S Save (write) the file. Note: Ctrl+S can be intercepted by terminal flow-control on some systems.
  • Alt+D Count the number of words, lines, and characters

NANO or VIM?

Ah yes the classic debate ! Which text editor is better? or which Should I use. In my experience, many people find vim to be difficult to learn because you have to switch between different modes as well as many other grievances they may have. However, I believe vim is the one editor you can be guaranteed that almost every Linux distro will have whereas nano is not. Nano is quite easier to learn than vim especially if you’re new to Linux Editors and it can do a lot of the stuff that vim can. Personally, I prefer vim in my day to day work and I use it more often, I can’t say one or the other is better I think it depends on the person but I do agree nano is much easier to learn if you’re just starting out.

The below links can be helpful to learn more about both text editors.

Cheers 🎊

Leave a Comment

Your email address will not be published. Required fields are marked *