Keyboard shortcut to "Comment" a line in NANO?

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 ''


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:

  • Meta-; (emacs)
  • Alt-/ (micro)