Disable code formatting in Eclipse with HotKey

I knew that you could prevent code formatting in eclipse by surrounding the code with

// @formatter:off // my awesome formatted code... public Set<Person> transformCustomersToPersons(List<Customer> customers){ Set<Person> persons = new HashSet<Person>(); for (Customer customer : customers) { persons.add(new Person( customer.getFirstname(), customer.getLastname(), customer.getTitle(), customer.getBirthday(), customer.getAddress(0).getCity(), customer.getAddress(0).getStreet(), customer.getAddress(0).getHousenumber(), customer.isMan() ? "m" : "w", customer.getCardNumber()) ); } return persons; } // @formatter:on 

But I do not want to write it manually all the time. It would be ideal to simply mark the code, press the hot key, and the code will be prevented by formatting.

Does anyone know how? thanks!

+6
source share
2 answers

Yes it is possible:

Create a new template: Windows->Preferences->Java->Editor->Templates->New (screenshot)

enter image description here

Give the template name something like non-formater and place the template:

  // @formatter:off ${line_selection}${cursor} // @formatter:on 

Now you can select the text and enter Alt + Shift + Z and the pattern number - here 4

enter image description here

+9
source

You can create your own hotkey by specifying a sequence of keys using extension points. View and edit keyboard shortcuts in eclipse: Eclipse → Settings → General → Editor → Keys.

Add a key for your reading and thus select the codes and run this hot key.

0
source

All Articles