39

I love NANO. It's always there, it can slice and dice, and its NOT VIM!

But I am constantly hitting Command / - inadvertently - as I go along, out of habit.. Most IDE's use this convention to "comment out" a line. NANO has great syntax highlighting.. so it knows the context of my documents.. but I haven't been able to figure out a way to get it to perform this basic, yet relatively important function.. Hand-commenting is for the birds.

I suppose this is sorta a super-user question, but in my experience, this would get a bunch of blank stares and then a few people asking who Nano was, so... here it is...

Oh and PS, brownie points for anyone that knows how to get DashCode to be able to do this as well... It would be the finest Javascript IDE EVER if it could perform this trick... and I'm convinced Apple omitted the feature for just that reason, lol.

Alex Gray
  • 16,007
  • 9
  • 96
  • 118

3 Answers3

15

The simplest workaround I've found:

  • Comment-out:
    set the cursor at the first row that should be commented-out
    hit twice 'M-R' (or 'Alt-r'; in order to Replace a RegExp)
    Search for: '^'
    Replace with: '# '
    Replace this instance?: 'y'
    press 'y' for each row to be commented-out

  • Comment-in:
    The same procedure, replacing '# ' by ''

Roland Bischof
  • 250
  • 3
  • 6
14

New in nano 2.6

(2016 June 17)

comment/uncomment lines with default binding M-3 (Meta-3)

(http://git.savannah.gnu.org/cgit/nano.git/tree/NEWS)

Customization

If you do not like the default mapping, note following:

Using Ctrl-/ by rebinding it in ~.nanorc with bind ^/ comment main is not possible. On a linux console this is equivalent to a backspace (https://savannah.gnu.org/bugs/?53248).

Some possible alternatives could be:

Hotschke
  • 9,402
  • 6
  • 46
  • 53
  • 2
    In case someone comes across this, and runs CentOS: standard repos have an old version, so here's a newer version that supports commenting (and also has colored syntax highlighting) - https://centos.pkgs.org/7/ghettoforge-plus-x86_64/nano-2.7.4-3.gf.el7.x86_64.rpm.html To install without repo: `yum remove nano` and then `rpm -ivh http://mirror.ghettoforge.org/distributions/gf/el/7/plus/x86_64//nano-2.7.4-3.gf.el7.x86_64.rpm` – Tanuki Aug 21 '18 at 10:34
  • @TheGodfather see https://www.nano-editor.org/dist/latest/nano.html#Commands-1 – Hotschke Jan 26 '20 at 09:05
  • I was looking for an answer to the very same question an your solution (`bind ^/ comment main`) **works** for me on Fedora 32 while using GNU nano, version 4.9.3. – tukusejssirs Jun 07 '20 at 21:02
11

While accepted answer is de jure correct, it doesn't really help if you're not proficient in Nano already.

Indeed since Nano 2.6 this functionality is available as M-3 (Meta-3). But what it actually means (see doc) is that comment\uncomment can be done via these hotkeys:

<Alt> + 3     // <Alt> is default <Meta> command
<Esc> + 3     // Single <Esc> should also work

And since usually you need to comment out several lines, you can use Alt + a (or Meta + a / Esc + a) to switch to selection mode which will allow to select several lines. Then comment command will be applied on all selected lines at once.

Related question: How to comment multiple lines in nano at once?

The Godfather
  • 4,235
  • 4
  • 39
  • 61
  • This doesn't work for me on macOS 12.6.1. Tried option (alt) and every other combination of the special keys, all gave an error beep or printed a 3 or £ – Max Dec 20 '22 at 00:02