Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to delete all comment lines in Sublime Text 3?

I know in Vim you can delete all comments in a file with a keyboard shortcut, but I have been unsuccessful finding a similar technique in ST3. This would be particularly useful for gemfiles and other files that have distracting comments everywhere.

Looking for a keyboard shortcut or a quick and easy alternative.

Any suggestions?

like image 800
mikeymurph77 Avatar asked Sep 04 '14 16:09

mikeymurph77


People also ask

How do you delete multiple lines in Sublime Text?

Multiple Selections To select multiple regions using the keyboard, select a block of text, then press Ctrl+Shift+L to split it into one selection per line. When you're done with using multiple selections, just press Ctrl+K to trim all but the first.

How do you delete a line in Sublime Text?

To quickly delete a line in your code, place your cursor anywhere in the line and hit Ctrl–Shift–K (Mac and Windows).


2 Answers

The simplest is to use a regex search-and-replace, e.g.,

  • Cmd-Opt-f
  • Select the .* option (regex)
  • Find What: ^#.*\n Note 1
  • Replace With: (nothing)

This won't work for comment blocks. For them you need to be a bit trickier, (very) roughly:

=begin(.|\n)*=end

(Not tested robustly.)

If you do this a lot (which IMO is a little strange) you could bind it to a key.


Note 1: This works for comments at the beginning of lines. It doesn't do comments appended to code lines; if you want to handle that then you can remove the ^ but you'll need to so something about the EOLs that would disappear during replacement. Or you can just ignore the EOL and have some blank lines where line comments were.

like image 168
Dave Newton Avatar answered Oct 19 '22 22:10

Dave Newton


What Dave answered didn't work for me. So do the following if its the same case for you too,

To delete all specific <tag> element including contents inside it:

  • Ctrl + H (on PC)
  • Enable (Regular Expression): .* icon or Alt + R
  • Find What:

    RE syntax to work with a particular tag,

    (?s)<start tag>.*?<end tag>

    Such as, for CSS comment tags,

    (?s)/*.*?*/

    For HTML comment tags,

    (?s)<!--.*?-->

  • Replace With: Leave the field blank

  • Replace All
like image 28
Arif Rahman Avatar answered Oct 19 '22 22:10

Arif Rahman