开发者

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

I'm trying to add a开发者_C百科 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 file -- does anyone know how to do this? I know some Textmate or regex trick must exist, just not sure what...


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


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.


to match use the following regEx

^[ ]+|[ ]+$

and replace it by empty sting("")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜