62

Sometimes, a graphical text editor like gedit or kate cannot be used (because you're in a virtual console for example). Luckily, there are text editors for the terminal. An easy one is nano, but I cannot understand how to work with it.

If I start nano by running nano, the bottom text is supposed to help me but all I see are things like ^G Get Help ^O WriteOut.

  1. How can I open text files for editing?
  2. How can I save the file?
  3. How can I quit the editor without saving the changes?
  4. How to edit? I heard that you've to enter some commands to begin editing in vi, is this true for nano too?
  5. Sometimes, if I manage to open a file, the text is unreadable due to its colors. How can I disable these colors? (see the image below)
  6. In the some files, lines are truncated because those do not fit in the screen. How can I prevent that from happening? (see the image below)

nano screenshot

1
  • Actually, all your questions are soo much easier to figure out by yourself for nano compared to for example emacs - not to mention vi/vim! Oct 10, 2020 at 6:41

5 Answers 5

81

Ctrl + G will let you read the help. nano can do some pretty nice things so you might want to pootle around in there for a bit.

When you see ^G (et al) it means Ctrl + G. In the help pages, M-H means Alt + H.

How can I open text files for editing?

This is the default in nano. Open and file and you're set to start editing:

nano filename

Note: you won't be able to save unless you have write permissions for that file.

How can I save the file?

F3 will let you save without exiting. Otherwise, Ctrl + X will prompt you if you've made changes. Press Y when it asks, and Enter to confirm the filename.

How can I quit the editor without saving the changes?

Ctrl + X, then N when it asks if you want to save.

How to edit? I heard that you've to enter some commands to begin editing in vi, is this true for nano too?

As above, no. nano is simple. It drops you in edit mode as soon as it opens. You can use arrow keys, Page Up / Page Down and Home / End as in gedit. You cannot use the mouse for moving the cursor position.

Sometimes, if I manage to open a file, the text is unreadable due to its colors. How can I disable these colors?

Colours are loaded through the nanorc framework. These are files that are loaded when nano loads which basically spell out the syntax highlighting. To toggle syntax highlighting, press Alt + Y. To disable it permanently for certain file types, edit /etc/nanorc and put a hash mark (#) before include "/usr/share/nano/*.nanorc".

In the some files, lines are truncated because those do not fit in the screen. How can I prevent that from happening?

Well I've been trying to find something but the best I could see was enabling soft-line-wrap with the funky key-combination of: Alt + $ (Alt + Shift + 4). To enable soft line wrapping by default, add the below line to ~/.nanorc:

set softwrap

More information about this configuration file can be found at man nanorc.

8
  • Is there a way to skip confirmation of filename when saving? Jun 20, 2013 at 19:19
  • @PiotrDobrogost Not that I know of... Is it really that annoying?
    – Oli
    Jun 20, 2013 at 20:46
  • 1
    Well, if you take into account that 99,99% of time you save the file which you opened then having to confirm its name is pure nonsense. Jun 20, 2013 at 20:58
  • @Oli, So.. you prefer nano to vim?
    – Pacerier
    Aug 9, 2016 at 8:54
  • @Pacerier Sure. And I prefer graphical editors (ie Sublime Text) to both nano or Vim (or emacs for that matter). That's obviously not to say that any of these is objectively better, I just find that when I need to do a quick edit from the command line, nano is right mix of power and simplicity. And I think Escape,:wq is a lot less intuitive (for a new user) than the Control+X flow (which is displayed on screen) which is why I'd suggest it to any new user needing a basic editor. You may think I'm espousing a lot of my pro-nano opinion in my answer but the question really is about nano.
    – Oli
    Aug 9, 2016 at 9:36
6

In the bottom text the ^ stands for Ctrl and M- stands for Alt

So^G is Ctrl-G and M-Y is Alt-Y (that one toggles the colors).

You can toggle line truncation with M-$. See the help pages (^G) for more functions.

6

The combination of ^ and a letter means you're supposed to press Ctrl and that letter. So when nano says ^X Exit that means you're gonna quit nano by pressing Ctrl+X.

  1. To open a file called /etc/nanorc you can start nano with:

    nano /etc/nanorc
    

    Note that /etc/groups is not writeable by regular users, therefore you need sudo:

    sudo nano /etc/nanorc
    

    If you've already opened nano, you can press Ctrl+R to open a file.

  2. On exit (Ctrl+X) nano will ask you whether to save the file. You can save it manually with F3.
  3. Answer n for no in the Ctrl+X dialogue.
  4. Basic editing is done by using the arrow keys to navigate and typing. Plain and simple.
  5. Syntax highlighting is disabled in a hidden file called .nanorc in your home directory along with other options. Alt+Y toggles it.
  6. In the aforementioned .nanorc you can add the line set fill 80 which will break long lines at 80 characters length. Alt+M toggles it.

Here is a list of stettings you can configure in .nanorc.

5
  • +1 for the example. Are there any side effects of setting set fill 80? When editing configuration files for example?
    – Lekensteyn
    Jul 23, 2011 at 10:16
  • nano doesn't save the linebreaks to the file. It just displays the lines that way.
    – con-f-use
    Jul 23, 2011 at 10:34
  • Alt+M toggles mouse support. set fill 80 seems to affect Ctrl + J and I can't reproduce the splitting feature. Perhaps it was a misinterpretation of softwrap.
    – Lekensteyn
    Jul 23, 2011 at 10:42
  • Alt + M was wrong on my part. As to set fill 80 - it works for me. Stragely I can't toggle highlighting with Alt + Y. Might be, that we have different versions or my system is screwed up.
    – con-f-use
    Jul 23, 2011 at 11:02
  • 1
    nano allows to change the key bindings. Check your ~/.nanorc and /etc/nanorc. I've 2.2.6-1 installed (Natty) and a QWERTY keyboard layout.
    – Lekensteyn
    Jul 23, 2011 at 11:04
2

CtrlX: Exit the editor. If you've edited text without saving, you'll be prompted as to whether you really want to exit.

CtrlO: Write (output) the current contents of the text buffer to a file. A filename prompt will appear; press CtrlT to open the file navigator shown above.

CtrlR: Read a text file into the current editing session. At the filename prompt, hit CtrlT for the file navigator.

CtrlK: Cut a line into the clipboard. You can press this repeatedly to copy multiple lines, which are then stored as one chunk.

CtrlJ: Justify (fill out) a paragraph of text. By default, this reflows text to match the width of the editing window.

CtrlU: Uncut text, or rather, paste it from the clipboard. Note that after a Justify operation, this turns into unjustify.

CtrlT: Check spelling.

CtrlW: Find a word or phrase. At the prompt, use the cursor keys to go through previous search terms, or hit CtrlR to move into replace mode. Alternatively you can hit CtrlT to go to a specific line.

CtrlC: Show current line number and file information.

CtrlG: Get help; this provides information on navigating through files and common keyboard commands.

0

You are asking that question because you are used to modern text editors with Ctrl+x, Ctrl+c, Ctrl+v, Ctrl+z, and marking using shift+arrows. You can quickly transform your ugly nano into a modern CLI editor.

Just create the file ~/.nanorc with these key bindings:

set atblanks            # don't cut words when doing line wrap
set nohelp              # don't display help footer
set softwrap            # don't break lines
set suspend             # don't suspend the shell
set tabsize 4           # tab stops every 4 spaces
set tabstospaces        # convert tabs to spaces
set constantshow        # show constants
set linenumbers         # show line numbers Alt+N to toggle
set casesensitive       # case sensitive
set zap                 # zap to the end of the line
set autoindent          # auto indent
set indicator           # show the cursor position
set minibar             # show the minibar
set nonewlines          # don't insert newlines
#set backup             # don't make backups
#set backupdir "/tmp"   # where to put the backups

bind ^X cut main        # CTRL+X - Cut
bind ^C copy main       # CTRC+C - Copy
bind ^V paste all       # CTRL+V - Past
bind ^Q exit all        # CTRL+Q - Quit program
bind ^S savefile main   # CTRL+S - Save
bind ^Z undo all        # CTRL+Z - Undo
bind ^Y redo all        # CTRL+Y - Redo
bind ^H replace main    # CTRL+H - Replace
bind ^F whereis main    # CTRL+F - Find
bind F3 findnext main   # F3     - Find next
bind ^/ comment main    # CTRL+/ - Comment

include /usr/share/nano/*

It will become something really close to micro editor, but with a tiny footprint.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .