How to customize Eclipse's text editor code formatting - eclipse

How can I set my Eclipse's code formatter to allow code rows longer than 80 characters. I know that very long lines is hard to read but in my opinion 80 characters per line is very small value for wide screen monitors.

In Preferences, go to Java > Code Style > Formatter and edit/create the formatter and go to tab Line Wrapping. There in you can set the Maximum line width.
Further on you can in General > Editors > Text Editors also set the Print margin column which should shift up that margin (a 1px wide vertical line in the right side of your code) to the desired position. This however doesn't affect the formatter, only the human eye.

This answer was a bit old, and the path have changed with new versions of Eclipse.
To set the Maximum line width, go to Project > Properties. In the Properties window, go to Java Code Style > Formatter. Select your Active Profile (let the one which is already selected, this is the one you are currently using) and click on Edit. In the tab Line Wrapping, you can set the Maximum line width (80 by default). You can also go to the tab Comments and uncheck Enable Line Comment Formatting if you want your comments not to be formatted on multiple lines with Ctrl+Shift+F.
Don't forget to change the name of the Profile Name; if you don't change it, you won't be able to save your changes (provided the one you are editing is Built-In
Also, you can set the Print margin column in Window > Preferences > General > Editors > Text Editors.

Regarding the "small" value (80), this has just changed (in August 2014).
See "Eclipse is not a terminal anymore – Default line size of Java code formatter is now 120"
(By Lars Vogel)
Project > Properties, Java Code Style > Formatter.
Edit tab Line Wrapping
A three-years old issue is finally addressed:
bug 356851: Default line size of Java code formatter should be increased to 120
It is not mentioned in the Eclipse Project 4.5 (Mars) M1 - New and Noteworthy, but it is still "Verified for 4.5 M1 using I20140804-2000 build".

Related

Print margins stuck in Eclipse

I have print margins enabled in the text editor settings to show at 100 characters. However, it is stuck on showing at 120 characters. I have tried restarting Eclipse, enabling/disabling the setting, and setting it to multiple values. It does disappear if I disable the setting, but if it is enabled it is stuck showing at 120 characters. Any advice?
The Java code style formatter settings actually override the Print margin column value in General->Editors->Text Editors. Go to Java->Code Style->Formatter and either select New or Edit. Under the Line Wrapping tab there is an option called Maximum line width, which is the value we want to change!

Unable to change eclipse line length or comment line length

I've tried changing all of the line wrapping parameters, both increasing and decreasing the maximum line length for source and comments, as well as disabling comment formatting. However, I'm unable to affect the code formatter's line-splitting behavior. No matter what changes I make to the format, line splitting occurs at 120 characters for both comments and source code. I've also tried upgrading eclipse from 3.6 to 3.7.
Any ideas why this is happening?
Right-click on your project and go to Properties
Check if Java Code Style > Formatter > Enable project specific settings is checked
If so, check the Active profile
My guess is that there was a different Active profile selected than the one you were editing. Deleting the project and recreating it caused the project to return to the default code style profile, where Enable project specific settings is unchecked.
Did you change the lines width from :
Preferences > Java > Code Style > Formatter >> then editing the maximum line width ?

How do I change the font size when printing from Eclipse?

Sometimes I'd like to print my code and read it during lunch. In Eclipse I'm using 10pt Helvetica font, but printing at this size is a waste of paper I think. I changed to font size 6pt, and the print was perfect; fully readable and paper saving.
However, coding in 6pt font is very hard and stressing for my eyes, so I cannot keep the setting. Also, changing font size everytime I print is a pain.
Is there a way to keep 10pt for my editor AND have the printer run at 6pt, at the same time?
I ended up changing the paper size to A5 and printing to PDF, then printing the PDF to A4.
The best workaround I've found is to copy-and-paste the entire program into a text editor where one can tweak the page/font settings (I use gedit) and print from there. A pain, but less so than changing the font size within Eclipse.
This question was awhile back but I found a work-around to the original question posted. When I select File-Print then a Print menu options comes up (every print menu options is different depending on the OS. This is on a Macbook) I scaled the size of the printing down. Works great to read and more importantly, save trees!:-) Scaling the printing of source code
I tried scaling printout to 50-75% and it works for me also: Large font on screen but small font to fit all in less amount of paper
Give this a try:
Go to preferences > appearance > colors and fonts then select 'basic'.
In that drop down select 'text font'. Click 'edit' make your changes and print. When you're done you may want to change it back by clicking reset.
I found this information in two places
1: http://blog.alagad.com/2007/06/15/changing-the-font-size-in-eclipse/
2: http://people.reed.edu/~jerry/121/handouts/04a-eclipse-printing.pdf [PDF]
I agree, Eclipse should have a 'Print font' setting.
This solution is late, but works so well, that I'm posting it anyway.
However, I have found a pretty good solution for me: format for Ledger (11x17) but scale to Letter (8.5x11). This is essentially 6 pt, which gets me 116 lines down, 140 cols across, in Portrait mode.
I like this so much, that for personal work, I have tweaked Eclipse to assume a 120 column print margin. Here's how:
Set Editing font to Consolas 10 pt. (This is my preference, however it doesn't make much difference, as you will see. (Window > Preferences > General > Appearance > Colors and Fonts > Basic > Text Font)
Set Print margin at 120 characters. (Window > Preferences > General > Editors > Text Editors > Show print margin > Print margin column 120)
Set code style formatter's line wrapping to 120 characters. (Window > Preferences > Java > Code Style > Formatter > Active Profile > [your custom
profile name] Edit > Line Wrapping > Maximum line width > 120).
The details of how you select the Paper size to format for, vary by printer. For me, it is done from File > Print... > Preferences > Basic > Paper Size > Ledger.
Likewise, selecting page scaling is a printer preference. For me, it is File > Print... > Preferences > Advanced > Scaling > Fit to Paper Size > Letter.
Using Mars.1 Release (4.5.1) I still see this as a problem. I tried the scaling in the printer dialog as suggested by Allen and Eclipsed, and my Brother printer under Windows used just as much paper, using same page breaks, leaving white space at bottom of each page.
So do an experiment with a small selection before trying this. Your mileage may vary.
I'm almost ashamed to admit it, but, so that I could get colors, and preview before printing, I ended up pasting into Microsoft Word and printing from there.
Just print to pdf with A3 paper size then print from your pdf reader. Works for me! Can get about 100 lines of code per sheet.
The mismatch of screen font size with printed font size is supposed to be fixed in Eclipse version 3.7 (Indigo). Perhaps you need a newer version of eclipse.
I had a similar problem, and was given the following by a colleague. The following should be entered on the command line:
To check default font: query default.print.font
To change font temporarily: print.font "Courier New-10"
To change font permanently: default.print.font "Courier New-8"
Hope it helps

How to disable auto formatting upon save in Eclipse?

Eclipse keeps auto formatting upon save, for example: it is insisting the following code should be in a single line once I save, despite breaking it up the way I want to.
#Transactional(rollbackFor = DataAccessException.class, readOnly = false, timeout = 30, propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT)
Window > Preferences > Java > Editor > Save Actions.
Can also be configured at the project level. Project > Properties > Java Editor > Save Actions.
Absolutely maddening improvement. For if statements do the following:
Window | Preferences | Java | Code Style | Formatter | Control statements tab
Select Keep simple if on one line.
You might also try the Line wrapping tab
This is a Late answer, But just for novice to understand it clearly I have a snapshot of the setting.
Please follow the path on left.
And change highlighted setting.
If you wish to keep the auto-formatter, but don't want this behavior, you can also configure it in the fomatter :
right click on the project -> properties -> java code style -> formatter
(If you don't have a project specific formatter the global configuration can be accessed this way : Window -> Preference -> Java -> Code Style -> Formatter).
Edit... the active profile, open the Line Wrapping tab, and there check the Never join already wrapped lines option.
This way you can wrap lines wherever you want and eclipse won't remove your line breaks (which is a must if you wish to have readable lambda expressions), but keep the formatter active.
This option is there at least since eclipse Luna
Here is a screenshot where this option is :
Just wanted to add a tip. If you want to avoid the line wrapping you can simply increase the "Maximum line width" value to something enormous. As long as that is sufficiently high eclipse won't do any wrapping.
Preferences > Java > Code Style > Formatter > Edit > Line Wrapping > Maximum line width

Is there an Eclipse line-width marker?

I have a specific project where I need to wrap every code line at 65 characters. I have set up the eclipse Java code formatter properly for this. But what I really want is a vertical line to be drawn in the editor showing where the max line width while I am typing, not just when I run the formmater. I know this feature is available in some capacity because it is displayed in the code formatter property page.
I don't see any option in eclipse to turn this on and I didn't see any plug-ins that do it on Eclipse Plugin Central
Look in Windows / Preferences (at least on Windows - IIRC it moves around for different operating systems) then:
General -> Editors -> Text Editors -> Show Print Margin
Tick this and it should show the line.
As a quick way of finding this, use the search filter in the top and filter on "margin".
Notes from the comments - unverified by me, but I have no reason to doubt them:
It has changed somehow in 2016: For details see [here] (https://bugs.eclipse.org/bugs/show_bug.cgi?id=495490#c2) You have to set it in the formatter: From menu [Window]-->[Preferences], select [Java]-->[Code Style]-->[Formatter], and then edit your formatter profile. In the tab page [Line wrapping], you can find a setting named "Maximum line width". Change this setting, and the print margin in Java source editor will be changed too.
In Eclipse Luna (4.4):
Choose menu Window\Preference . Look at top-left corner, in search box type filter text, type: margin.
In section Apperance color option, Choose Print margin. Choose Show print margin. In text box Print margin column , type 65 as what you want.
#Jon Skeet's answer is incomplete.
(1/2) First, do what he said:
Window --> Preferences --> General --> Editors --> Text Editors --> check the box for Show Print Margin
Ticking this box will show the vertical line.
As a quick way of finding this, use the search filter in the top and filter on "margin".
However, this only shows the line, but under most situations the "Print margin column" value there is flat-out ignored.
To set the column number for where the line should be, do what #John Percival Hackworth mentions here:
(2/2) Go to:
Window --> Preferences --> C/C++ [or whatever language you are using] --> Code Style --> Formatter --> click Edit --> under the Line Wrapping tab set the value you desire for Maximum line width.
Side note:
Use Alt + Shift + Y to toggle soft line wrapping on and off. It will soft wrap (ie: no carriage return) at the end of the screen, however, not at the column you set above.
How do you enforce hard line wrapping at the column you set above (ie: that adds a carriage return)? I don't know yet. If you figure it out let me know. In Sublime Text 3 (a much better editor but with a much worse indexer/function definition finder :() it's Alt + Q.
Update: I think it may be possible with the "CppStyle" plugin, which uses clang-format, by using Ctrl + Shift + F to apply the auto-format, but I don't know the exact instructions to make it work yet.
Related:
Set tab width: Changing editor tab width in eclipse 3.5
After some months with Espressif, but also with other brands plugged-in Eclipse, I found how to enlarge maximum line width. I made a lot of attempts and show how to do for Espressif-IDE:
Right click a project->properties->C/C++ General->Formatter
->Enable Project specific settings->
New->Give your profile a name and base it on a built-in formatter: I choose BSD/Allman->Edit this new profile->within Line Wrapping tab type for example 200 for Maximum line width->Apply changes.
Format source files: you'll have long lines.
Before I did the same manouvres starting from:
Window->Preferences->C/C++->code Style->Formatter... : that never worked.