DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Editor Templates in Eclipse

Editor Templates in Eclipse

Erich Styger user avatar by
Erich Styger
·
Mar. 12, 13 · Interview
Like (0)
Save
Tweet
Share
10.68K Views

Join the DZone community and get the full member experience.

Join For Free

eclipse is a good ide. at a first glance, it does the job. good enough. at least for myself, i was not that much excited when i used it the first time. i came from the microsoft visual studio world, and have used many other proprietary ide’s. so eclipse was just ‘yet another one’. but what eclipse makes really great is the incredible wealth of functionality which is not visible right away. yes, this is the same for any other software tool: it takes time to explore, and once you know things well, you do not want to switch or even consider something different. same for me.

the other thing is: after some time, i get used to things, and i do not appreciate it that much any more. only until someone reminds me that maybe things are not that well-known? this is what happened to me two days ago: i did some editing in eclipse, while a colleague was watching me doing this. then he said something like this

“hey, what did you do? what was that ?!? how did you do that?”

i do not remember his exact words, as i was surprised as well. i did not do anything special? kinda standard eclipse thing. well, maybe not.

:!: warning: using this eclipse feature here might be highly addictive :-) .

the for loop

what i did was something like this: i started writing a function like this:

writing a function

writing a function

then i added a ‘for’:

adding a for

adding a for

and now the magic: i pressed ctrl+space to open the content assist:

content assist

content assist

:idea: pressing ctrl+space reduces the list of items to template proposals only.

i selected the ‘ for – for loop ‘ item and hit enter. this adds the code for the loop:

added for loop

added for loop

notice the blue boxes. my cursor stays on ‘var’, and i can simply type my variable i want to use, and it changes all loop variable instances automatically (!!!):

changed loop variable

changed loop variable

next i press tab which moves me to the ‘max’ variable:

on max variable

on max variable

i type in my loop max variable and press tab again: this moves me to the body of the loop:

entered max variable

entered max variable

now i enter my loop body:

entered loop body

entered loop body

so here again:

for – ctrl+space – enter – i – tab – bufsize – tab – buf[i] = 0;

is all what i needed :-) . a cool standard eclipse editor feature: editor templates.

template on selection

it is possible to apply a template on a selection. for example i can select one or more source lines:

selected text

selected text

then i press ctrl+space and select the for-loop template. this wraps a for() loop around it:

for loop around selection

for loop around selection

template positions

let’s have a look at the stage when i pressed ctrl+space and selected that template:

template positions

template positions

the blue boxes indicate positions where i will jump to if i press tab . the green line in the for() body indicates the place i will jump to if i press enter .

template preferences

the eclipse editor templates are configured using the menu window > preferences > c/c++ > editor > templates. the template example i was using above looks like this:

for-loop editor template

for-loop editor template

if i do not like a template, i can create a new one or edit an existing one:

editing a template

editing a template

variables are of the form:

${variablename}

pressing the ‘insert variable…’ button offers a set o built-in variables:

built-in variables

built-in variables

:idea: use ctrl+space while editing the template to get a list of built-in variables.

anything not a built-in variable is a custom variable (e.g. ${var}). using the variable name multiple times references the same variable content.

:idea: it is possible to use an ‘empty’ variable as well with ${} . that way i can have things which are not replaced with anything if i want to have it that way.

custom editor templates

adding new templates is really easy. for example i have a template ‘taskcreate’ to create the source code to create a freertos task:

if (frtos1_xtaskcreate(${name}, (signed portchar *)"${name}", configminimal_stack_size+${size}, null, tskidle_priority+${prio}, null) != pdpass) {
  for(;;){} /* error! out of heap? */
}
${cursor}

or another template to implement the framework for a task function:

static porttask_function(${taskname}, pvparameters) {
  (void)pvparameters; /* not used */

  for(;;) {
  ${cursor}
  frtos1_vtaskdelay(${delayms}/porttick_rate_ms);
  }
}

that way i’m really fast adding a new tasks. i simply type the word ‘task’ followed by ctrl+space:

task templates

task templates

and within seconds i have created the framework for a new task:

created task

created task

comment templates

another way to use templates is for comments. for example i can define a comment like this:

comment template

comment template

then i type

/**fct

and press ctrl+tab, and it will replace it with my template :-) .

:idea: eclox for doxygen comes with other built-in comment templates.

exporting/importing

to share templates, i use the import and export buttons:

import and export of editor templates

import and export of editor templates

summary

editor templates are a powerful way to extend the eclipse editor and to improve productivity. this is one of the greatest eclipse features in my view.

happy templating :-)

Template Eclipse

Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • Using JSON Web Encryption (JWE)
  • Unleashing the Power of JavaScript Modules: A Beginner’s Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: