Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime text 2 how to delete comments only

Tags:

sublimetext2

In my sass code, I have inline comments and I wish to remove these in sublime text. Is it possible to permanently delete all comment content alone?

@function emCalc($values) {
  $emValues: '';
  $max: length($values); //Get the total number of parameters passed
  @for $i from 1 through $max {
    $value: (nth($values, $i)); //Take the individual parameter
    $value: $value / ($value * 0 + 1); //Doing this gets you one unit (1px)
    $value: $value / $em-base * 1em; //Divide the px value by emBase and return the value as em
    $emValues: #{$emValues + $value}; //Append to array
    @if $i < $max {
      $emValues: #{$emValues + " "}; //Adding space between parameters (except last), if there are multiple parameters
    }
  }
  @return $emValues; //Call emCalc like so emCalc(10, 20, 30, 40) it should return margin: 0.625em 1.25em 1.875em 2.5em
}
like image 337
Vinay Raghu Avatar asked Jun 28 '13 11:06

Vinay Raghu


3 Answers

You'll need to double check this (have a backup handy!), but the following regular expression should work in the "replace" window, with regular expressions enabled (the * icon):

  1. Open the "replace" window (ctrl + h / cmd + option + f)
  2. Enable regular expression matching by making sure the * icon is selected
  3. Enter the following in the "Find What?" box

    \/\/.*
    
  4. Leave the "replace with" box empty to delete found text.

  5. Click "Replace All"

Edit

as @ollie noted, this also will delete any urls prefixed with //. The following (lightly tested) regex should serve to better target comments: (^\/\/.*)|(\s+\/\/.*)

Edit v2

A solution for single and multi-line comments (^\/\/.*)|(\s+\/\/.*)|((\/\*)(.|\n)+?(\*\/))

like image 173
Nick Tomlin Avatar answered Jan 05 '23 02:01

Nick Tomlin


If you have no other possibility, you could select every // (Select first // then CtrlD while there's comments left if my memory is correct).

Then press ShiftEnd to select every end of line with a // and Del ! :)

(There's probably a plugin for that, but this is the simplest method I think. This suggest that all your // refers to the beginning of a comment, of course)

like image 33
Maxime Lorant Avatar answered Jan 05 '23 02:01

Maxime Lorant


None of the answers here seem to take advantage of the fact that the syntax highlighting has already determined where all the comments are - just execute this in the Python console (View menu -> Show Console) to select all comments:

view.sel().clear(); view.sel().add_all(view.find_by_selector('comment'))

(press Enter after typing/pasting it to execute it) then press Delete to delete all the selections and Esc to go back to single selection mode

like image 44
Keith Hall Avatar answered Jan 05 '23 03:01

Keith Hall