Sublime text 2 how to delete comments only

前端 未结 6 1874
既然无缘
既然无缘 2020-12-23 12:30

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?

@functio         


        
相关标签:
6条回答
  • 2020-12-23 13:19

    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

    0 讨论(0)
  • 2020-12-23 13:19

    None of the other answers cover all cases (multi-line comments, single line comments, double-slash comments and slash-star/star-slash comments).

    In order to match all possible cases (without matching URLs), use the following:

    (^[\s]*?\/\/.*)|(/\*[\s\S]+?\*/)
    
    0 讨论(0)
  • 2020-12-23 13:26

    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)+?(\*\/))

    0 讨论(0)
  • 2020-12-23 13:27

    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)

    0 讨论(0)
  • 2020-12-23 13:34

    Well well, there is an easy way to do it in mac Sublime Text if you are sure there are no // in print statements.

    search for // and hit that cmd+ctrl+G and then to select the whole line which has hit cmd+shift+Arrow and delete it. Assuming you have used only single line comments

    0 讨论(0)
  • 2020-12-23 13:36

    Here is what I do in ST3 in HTML to strip all comments, especially nasty comments embedded within <p> body text, for example ...

    1. package control install SelectUntil

    2. quit and restart sublime

    3. ctrl+f <!--

    4. alt+enter to select all instances of <!--

    5. ctrl+shift+s will pull up an input field, where you can type: -->

    6. hit delete!

    0 讨论(0)
提交回复
热议问题