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

只愿长相守 提交于 2019-12-03 00:51:22

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("")

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!