14

Normally I want nano to replace tabs with spaces, so I use set tabstospaces in my .nanorc file. Occasionally I'd like to use nano to make a quick edit to makefiles where I need real tab characters.

Is there any way to dynamically toggle tabstospaces? Most of the other options have keys to toggle them, but I can't find something for this. I've also tried using ^I (which by default is bound to the tab function) to insert a tab, but that honors the tabstospaces setting.

My current workaround is to take set tabstospaces out of my .nanorc file and to add shell aliases instead:

 alias nanotabs="$(which nano)"
 alias nano="$(which nano) --tabstospaces"
jamesdlin
  • 838
  • :) By tabs, at first I thought you meant switching between Nano's buffers (Alt+right and Alt+left); I leave this comment for anyone who might be looking for that. – Brōtsyorfuzthrāx Sep 30 '22 at 05:48
  • you could create a wrapper for nano, which calls nano with custom options when editing Makefile or *.mk files – milahu Aug 07 '23 at 15:37

1 Answers1

17

The shortcut that toggles tabstospaces is Meta+O (the letter O, not the number 0). (In earlier versions, it was Shift+Alt+Q or Meta+Q.)

You will see the prompt changing to:

[ Conversion of typed tabs to spaces disabled ]

or

[ Conversion of typed tabs to spaces enabled ]

respectively.

Since version 1.3.1, you can also insert a literal tab if you enter Verbatim Input mode with Shift+Alt+V (or Meta+V).
If you then type Tab, nano will insert a literal tab character, irrespective of your .nanorc settings. It will then revert to regular input mode (so you'll have to enter Verbatim Input mode again if you need to type a second literal tab and so on).
You can also add your own Verbatim Input mode shortcut to .nanorc, e.g. Ctrl+T:

#Edit
bind ^T verbatim main
Stephen Kitt
  • 434,908
don_crissti
  • 82,805
  • Aha! I don't know how I missed seeing M-Q in the built-in help (curses at the inability to search nano's help). – jamesdlin Jan 15 '15 at 21:34
  • 2
    in nano since version 3 or so the shortcut is now meta+O https://www.nano-editor.org/dist/v3/nano.html#Feature-Toggles I did not found mention of the change in the changelog https://www.nano-editor.org/dist/v3/ChangeLog – Lesmana Feb 24 '19 at 15:54