2

Can anyone tell me if there's a quick way to format your code in Text Mate, similar to pressing ctrl K+D in Visual studio?

Thanks!


Edit by Damien_The_Unbeliever:

For those not familiar with Ctrl K+D, it doesn't just indent code - it reformats it using the generally established formatting conventions in the editor - it may replace spaces with tabs or vice-versa for the indentation, ensure code is consistently indented, move braces to separate lines, etc.

Finnnn
  • 3,530
  • 6
  • 46
  • 69

2 Answers2

4

TextMate reindenting and reformatting varies a little depending on the language you're using.

You can generally use the Text menu, that depending wether you have an active selection or not it will show you different commands under it. For example, if you have selected a section of code, there will be a Indent Selection menu item. If you have no active selection, it will be Indent Line.

To have this working properly, be sure to select the current language, if it isn't assigned yet (like on unsaved documents). If you're working with HTML, it will simply indent the lines depending on what's above it. It will keep line breaks intact.

If you need something to break out tags on new lines and properly format the document, you should use the Tidy command that is found in the Bundles menu, under HTML (or simply by using the shortcut CTRL+SHIFT+H. If you have a selection active at the moment that you use it, it will simply reindent that section. If instead you have no selection, it will properly reformat the whole document, including checking for tag validity and other errors.

The Bundles for other language have similar commands, like XML (still Tidy) and Javascript (that has a Reformat Document command).

As an ending note, I simply suggest to look into the Bundles menu; there are many little gems in it. ;)

mcdado
  • 718
  • 1
  • 8
  • 15
3

Did you look in the menu bar? Under Text you have a couple of Reformat… entries that may fit your needs.

Beside these native features, some bundles — like the JavaScript one — have custom Reformat… commands : click on the little cog button at the bottom and explore your current language's bundle's content.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • I can't see any Reformat option under Edit, but the javascript option is there. I'll have a look for some other bundles that do the same. I was hoping for a simple short cut that will tidy any code of any language. Perhaps asking for too much... – Finnnn Jun 01 '11 at 13:54
  • 1
    TextMate is a nice text editor but definetely not match to a serious IDE (or more serious text editors like vim or emacs) for many things. Sorry, it's "Text", not "Edit". There is "Reformat Paragraph" and "Reformat And Justify". – romainl Jun 01 '11 at 14:08
  • Format options for CSS and JavaScript are not bad, but I am looking for one to help with PHP code. The standard PHP Bundle does not seem to offer this. Wee PHP beautifier seems to not be able to handle PHP and HTML together in one file. Will try Netbeans until I have found a solution – rhand Jan 23 '13 at 04:05