Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard shortcut to "Comment" a line in NANO?

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.

like image 914
Alex Gray Avatar asked Aug 24 '11 03:08

Alex Gray


People also ask

What is the shortcut for commenting on a line?

Select the Code to Comment. Press Ctrl + K . Press Ctrl + C .

What does Ctrl o do in nano?

Saving your work: To save your edited file to disk, press Ctrl-o . Nano displays the current filename. (To save the file under a different name, delete the filename that Nano displays and type a new one.)

What does Ctrl S do in nano?

Since at least Nano v3, the native Ctrl+S shortcut will directly save the file. Ctrl+O is the "Save As…" feature.

How do you use keyboard shortcuts to comment?

If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.


2 Answers

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

like image 136
Roland Bischof Avatar answered Oct 02 '22 11:10

Roland Bischof


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)
like image 40
Hotschke Avatar answered Oct 02 '22 09:10

Hotschke