Textmate/Regex: Strip whitespace from beginning/end of file

前端 未结 3 731
梦毁少年i
梦毁少年i 2021-02-04 22:59

I\'m trying to add a macro/command to Textmate for some file cleanup and the last tidbit I haven\'t figured out is simply to remove blank lines from the beginning and end of a f

相关标签:
3条回答
  • 2021-02-04 23:12

    For the tasks you mentioned, you can use built-in commands from the "Text" Bundle.

    Even if you just want to script your own, i would suggest using these as templates for your own efforts.

    To access these:

    Removing Trailing Spaces:

    • ctrl-cmd-t to bring up the context-sensitive Bundle menu

    • begin typing "remove trailing", before you finish "remove", you should see the "Remove Trailing Spaces" Command move to the top of the menu (this Command is in the "Text" Bundle, one of the Bundles included with the TextMate )

    • "enter" will execute the command (assume the cursor is positioned correctly, etc.)

    Alternatively, you can access this Command using a key equivalent, but since one is not assigned in the default TextMate installation, you'll need to assign it yourself, which is simple to do:

    • ctrl-alt-cmd-b to bring up the Bundle Editor

    • find the Text Bundle then click on the "Remove Trailing Spaces" Command

    • In the upper right-hand-side of the Editor, toggle "Settings" and enter either a key equivalent or a "tab trigger" (which is activated by entering some key combination you assigned followed by the tab key.

    Remove Leading Spaces:

    This one's a macro rather than a command. It's probably easiest to access it via it's pre-configured key-equivalent, which is cmd-del

    0 讨论(0)
  • 2021-02-04 23:13

    to match use the following regEx

    ^[ ]+|[ ]+$
    

    and replace it by empty sting("")

    0 讨论(0)
  • 2021-02-04 23:24

    This regex will remove the whitespace at the beginning of the file

    Find ^[\r\n\t ]+ and replace with (nothing).

    And this one will remove the whitespace at the end

    Find [\r\n\t ]+$ and replace with (nothing).

    I've never used Textmate regular expressions. There can be a \s (whitespace) class you can use instead of [\r\n\t ]. Also, you might need to turn multi-line mode on, if there is one.

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