Tip #288: A keymapping to generate Java setters and getters automatically
tip karma |
Rating 5/2, Viewed by 599
|
created: |
|
July 25, 2002 19:25 |
|
complexity: |
|
intermediate |
author: |
|
Sheer El-Showk |
|
as of Vim: |
|
5.7 |
This mapping makes it much simpler to write new java classes by simplifying some of the dull repetative coding (ie setters and getters).
To use, first write a basic class with the following format:
public class MyClass
{
private <type> <varname> = <initvalue>;
private <type> <varname> = initvalue>;
// getters
// setters
}
Note the getters/setters comment -- they are important as they are used to place the getters and setters.
The mapping is:
map jgs mawv/ <Enter>"ty/ <Enter>wvwh"ny/getters<Enter>$a<Enter><Enter>public <Esc>"tpa<Esc>"npbiget<Esc>l~ea()<Enter>{<Enter><Tab>return <Esc>"npa;<Enter>}<Esc>=<Enter><Esc>/setters<Enter>$a<Enter><Enter>public void <Esc>"npbiset<Esc>l~ea(<Esc>"tpa <Esc>"npa)<Enter>{<Enter><Tab>this.<Esc>"npa=<Esc>"npa;<Enter>}<Esc>=<Enter>`ak
(the above should be one long line with no spaces between the end of the lines above).
To use this to generate a class go to the variable that should have a setter/getter and place the curser at the beginning of the 'private':
private <type> <variable> = <initvalue>'
^
Then type:
jgs
this will create the first getter/setter and then move up to the next variable. You can just keep typing jgs until all the getters/setters have been generated.
This should mapping isn't perfect and someone could probably make it a little cleaner. It could also relatively easily be adapted to C++. Please feel free to send me any feedback/enhancements as I am trying to compile a list of these.
<<Cool trick to change numbers |
Alternative <escape> that allows you to do a "quick and dirty insert" and get out into normal mode >>
Additional Notes
|