Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Generate Java setters and getters automatically
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Generate Java setters and getters automatically
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=288 |previous=286 |next=290 |created=2002 |complexity=intermediate |author=Sheer El-Showk |version=5.7 |rating=6/3 |category1=Automated Text Insertion |category2=Java }} This mapping makes it much simpler to write new java classes by automating a repetitive coding task, the creation of setters and getters. Note that a ready-made [[#Plugins|plugin]] exists to do much the same thing. To use, first write a basic class with the following format: <pre> public class MyClass { private boolean isActive } </pre> Note the getters/setters comment -- these are important as they are used to place the getters and setters. The mapping is which should be added to the .vimrc file is: <pre> map jgs mawv/ <CR>"ty/ <CR>wvwh"ny/getters<CR>$a<CR><CR><Esc>xxapublic <Esc>"tpa<Esc>"npbiget<Esc>l~ea()<CR>{<CR><Tab>return <Esc>"npa;<CR>}<Esc>=<CR><Esc>/setters<CR>$a<CR><CR><Esc>xxapublic void <Esc>"npbiset<Esc>l~ea(<Esc>"tpa <Esc>"npa)<CR>{<CR><Tab>this.<Esc>"npa= <Esc>"npa;<CR>}<Esc>=<CR>`ak </pre> 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': <pre> private <type> <variable> = <initvalue>' ^ </pre> Then type: <pre> jgs </pre> This will create the first getter/setter and then move up to the next variable. A user can continue typing <code>jgs</code> until all the getters/setters have been generated. ==Plugins== *The {{script|id=490|text=java_getset.vim}} script makes it dead simple to auto generate getters and setters, plus its easy to install and configure. *[http://code.google.com/p/lh-vim/wiki/lhRefactor lh-refactor] handles getters and setters among other refactorings (and languages) ==Comments== This is a great idea, but this implementation is a little lacking. When I first used it, it put my getters and setters in comments (since it searchers for // getters, and doing an <code>$a<CR></code> on that line continues the comment). Additionally, it assumes a tabstop of 8 instead of letting the file type indent do the work, plus the spacing is a little messed up. What I usually do is type all my private members. I got to the first one and hit <code>qa</code> to start macro recording to register ''a''. Then I make my getter and setter using only commands that operate on words and not characters. When done, hit <code>q</code> again, and now register a has your macro. Comment from Jeremy; This mapping caused a delay on the last j press in normal mode for me. It was quite irritating. I would suggest pointing the mapping to a different key sequence where the first key is not a common navigation key.
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Script
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab