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

青春壹個敷衍的年華 提交于 2020-01-22 08:16:26

问题


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 file -- does anyone know how to do this? I know some Textmate or regex trick must exist, just not sure what...


回答1:


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




回答2:


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.




回答3:


to match use the following regEx

^[ ]+|[ ]+$

and replace it by empty sting("")



来源:https://stackoverflow.com/questions/2412690/textmate-regex-strip-whitespace-from-beginning-end-of-file

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