Add piece of code to shortcut - java

Is there a way in Eclipse to bound a piece of code to a shortcut?
I use System.out.println() quite often, for testing and other things, and it would be so much easier if I had to press you ctrl + something instead of typing it out every time..

For System.out.println, you can type something like:
"test"
Then highlight it, press CTRL+Space, type sysout, and press enter.
As Pescis noted in the comments, you can also type sysout followed by CTRL+Space. It gives you the System.out.println(); with a focus inside the brackets. (Thanks Pescis!)
To create new templates, go to Window -> Preferences -> Java -> Editor -> Templates.
Here is a link to some ready-made templates, if you are interested.

Type syso and ctrl + space for System.out.println()

Related

How can I change the post fix template in Eclipse? [duplicate]

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Eclipse - Enclose selection instead of replacing

In Eclipse, when i select part of a text and press any key, the whole selection is replaced.
I would like to know how to make it, instead of replacing the text, add the key pressed to both ends of the selection, as it happens on visual studio code.
Example:
System.out.println(HelloWorld);
Select Hello World
System.out.println(HelloWorld);
Then press the " button. The code will turn to
System.out.println(");
Can i config the IDE so it will instead change it to
System.out.println("HelloWorld");
There is a already answered question like this one, but considering that one as 7 years and a lot of version old, and also considering that the answer on that one is kind of a work around, i would like to know if there's any new plugin or configuration that make it work better
From what I gather, you are wanting to do something like this?
Go to Java > Editor > Templates and add a new template, e. g. called quote, as follows:
"${word_selection}"${cursor}
Then, in the editor, write a text you want to quote, select it, press Ctrl+Space, type quote and hit Return. The highlighted text should be quoted now.
If you don't get template proposals when pressing Ctrl+Space, make sure you have them checked in Java > Editor > Content Assist > Advanced.
Just tried it and it works for me!
You can use this method for other things, like (${word_selection})${cursor} for parenthesis or '${word_selection}'${cursor} for single quote.

Eclipse not giving me variable name suggestions

well I've seen in Tips & Tricks of Eclipse that it's possible to get a variable name generated by eclipse by clicking Ctrl + Space. However I'm getting empty suggestions. Fors instance I'm typing this and click ctrl + space:
private Color
And I am supposed to get some name suggestions on it? All I'm getting is an empty list of suggestions. So what's turned off? Any idea?
Thanks in advance.
This is what I want to achieve:
I believe you are trying to get variable name e.g. color after private Color where Color is you class i.e. private Color color. I see that working in my eclipse.
To verify your settings, go to below settings and verify as they look good to you.
Windows -> Preferences -> Java -> Editor ->Content Assist
and
Windows -> Preferences -> Java -> Editor ->Content Assist -> Advanced
This is the default behavior after entering a Java type and a space, and then pressing CTRL+Space to activate auto-complete. For example, if you enter:
private Color
then activate auto-complete it will suggest some variable names for you.
An easier way than having to press CTRL+Space all the time is to change the characters which automatically activate auto-complete. I find it very useful to have all characters which could possible be variable names to activate auto-complete. Try having ._abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ as Auto activation triggers for Java found in Preferences -> Java -> Editor -> Content Assist:
This way you can achieve how Visual Studio handles auto-complete.
Goto Window -> preferences -> java -> Editor -> content Assist ->Advances -> Select all the check boxes. DONE :)
private? It looks like you're creating something new.. eclipse cannot suggest in this case.
Autocomplete is for existing variables/functions/classes/etc.
I tried them one after the other and found out that the correct one is:
Windows -> Preferences -> Java -> Editor ->Content Assist -> Advanced: check Word Proposals
In Eclipse, it can get a variable name that you already made.
For example,
int awesomeVariable;
awesomeVariable = 50;
int superVariable;
superVariable =
If I press Ctrl + Space after that =, it would generate some things that it could fill in. For example, it might suggest awesomeVariable. Basically, it doesn't generate a name for you, it just auto-completes with things you have already created.
Ctrl + Space is maybe shortcuts of IM, so it is covered, I suggest you change the shortcuts of this function, for example: Alt + /,
if you don't know how to change shortcuts, please see the following steps:
click Ctrl + Alt + L twice,
find "Content Assist",
change Binding value to you like shortcuts(cannot same the other shortcuts),
click Apply button.
Thanks for everyones time and help. :)
I finally found it and the answer to this was that in my case the "java.awt.Color" wasn't imported and eclipse doesn't work on this one if it's not imported.
So it does suggest the name for you if your file had imported the class already, but if it hadn't it won't work.
I guess it makes the whole function pretty useless, but unfortunately that's how eclipse works. :/
it just suggesting you can also declare the variable by that name..it just helping you because it was a tool write..nothing to worry about that..you can give your own name as you like kk..
import Color Class to your class: import java.awt.Color;

How to add shortcut keys for java code in eclipse

Say I type "sout", the intellisense should expand it to "System.out.println()". Is there a way to adding such templates?
The feature is called "code templates" in Eclipse. You can add templates with:
Window->Preferences->Java->Editor->Templates.
Two good articles:
Don't write the code, generate it
Custom Templates
Also, this SO question:
Useful Eclipse Java Code Templates
System.out.println() is already mapped to sysout, so you may save time by learning a few of the existing templates first.
Type "Sysout" and then Ctrl+Space. It expands to
System.out.println();
Type syso and ctrl + space for System.out.println()
type "syso" and then press ctrl + space
OR
type "sysout" and then press ctrl + space
This is one more option: go to Windows > Preference > Java > Editor > Content Assit. Look in "Auto Activation" zone, sure that "Enable auto activation" is checked and add more charactor (like "abcd....yz, default is ".") to auto show content assist menu as your typing.
I've been Eclipse-free for over a year now, but I believe Eclipse calls these "Templates". Look in your settings for them. You invoke a template by typing its abbreviation and pressing the normal code completion hotkey (ctrl+space by default) or using the Tab key. The standard eclipse shortcut for System.out.println() is "sysout", so "sysout" would do what you want.
Here's another stackoverflow question that has some more details about it:
How to use the "sysout" snippet in Eclipse with selected text?
Step 1 -> Top apple icon -> System preference -> keyboard -> Shortcut>spotlight -> uncheck.
Step 2 ->Eclipse -> General -> keys -> search -> content Assist ->
Binding (cmd button + space) -> Apply&close.

How to use the "sysout" snippet in Eclipse with selected text?

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Categories

Resources