In IntelliJ IDEA how do I replace text with a new line? - java

I wanted to replace all commas with commas and a newline using IntelliJ IDEA's replace function. What do I put in the search box? In vim I'd use &\r.

You need to check the Regex box and use "\n" for the new line character:

Use Multiline button, no Regex is needed.
edit: the multiline button is missing since IntelliJ 15, but you can enable it by clicking into the textfield and pressing Alt+Enter or Ctrl+Shift+Enter

For Intellij Ultimate 2017.3 on Mac, command-shift-enter works

Hit CTRL+F and check the regex checkbox. Then search for , and replace it with ,\n.

The easiest way that I have done it is to use the regular expression form of replace.
Chances are that you don't want to replace the {, but just keep in my escaping them if you do want to do so.

On intellij Ultimate 2017.1:
I didn't need regex. But I could make the multiline replace appear.
I entered \n in the field I wanted to replace
I placed my cursor in the field where I wanted to enter the replacement text, and clicked Ctrl-Shift + Enter. Here I just hit return

For those looking for the old multiline replace in inteliJ with version > 15.x. It seems somewhat hidden, but if you select multiple lines > click CTRL+F, then immediately click CTRL+R you are presented with the original multiline replace.
This is working on Mac IntelliJ 2016.1.3 with ⌘+F > ⌘+R

A clean approach would be to add (?m) in front of the regular expression, which turns on the multi line mode. This has the advantage that you can also use it in the global file search (Ctrl-Shift-F).
Example: (?m)\{(.|\n)*?\} searches for multi-line blocks surrounded by curly braces.

The is related but not exactly what you asked. But I needed it and I can imagine others do to.
So I had the problem in Node.js where I wanted to split a reject into call into a log and reject for clarity
reject(error)
into
appLogger.log(error, 'error')
reject(error)
In normal mode, I did find and replace
Find: reject(error)
Replace: appLogger.log(error, 'error')\n reject(error)
Then in regex mode I did a second find and replace:
Find: \\n
Replace \n

Ctrl + Shift + R while the replaced text is selected:
This works for Replace in Path (WebStorm 2018.2.3):
see here

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.

Insert line break in editor

I'm trying to add a line break with the Find function in eclipse - What I want to do is put a line after each method in all my classes [I liked them all close together but now its hard to read through]. so I figure theres 2 ways to do this.
1. Find all with "public" and change to "\npublic", \n being the new line in a string to give you the idea of what I mean.
2. A convenient eclipse shortcut that separates all the methods in a class with an empty line.
Any ideas on how to achieve this? I'll be happy as long as it works and I don't have to go through every method in the project myself and push enter .-.
For #2, you can set the Eclipse auto-formatter to do so for you.
Look under the Preferences panel; in the left side (tree), select Java Code Style (click the arrow to expand the section), then Formatter. You can then decide to use a single formatting style for all of your projects, or check the box to apply a special set of formatting rules to just the current project. Some of the options include where to locate whitespace and newlines.
You can also use the “Save Actions” panel to re-apply the same formatting rules every time you save your files. Personally, I use (and team members as well) this method, to ensure that the formatting for the files is always consistent. By mechanically reformatting in this way, changes to the file will always show up “properly” in Subversion/Git — I don't accidentally have whitespace-only changes being introduced into files showing up as “false positive” changes.
Check box 'Regular Expressions' and use '\R' for Linebreak in the 'Replace with' box in eclipse
configure the auto-formatter its a bit of work for the first time but its worth, when ever you get ugly formatted code you can fix this in seconds ..

How to toggle commenting and uncommeting code in IDEA?

Is there a way to comment/uncomment a block of selected text in IDEA?
Yes, highlight the block and type Ctrl + Shift + /. To uncomment, just repeat.
with Shift this makes for block style comments /**/, without it for line style comments Ctrl + / -> \\ (tested with IDEA 10U)
Jetbrains provides a printable reference card with the default set of Windows and Mac key bindings.
For the benefit of others who might stumble across this question & answers... In addition to the windows key binding provided in other answers, there is also a keystroke for toggling a comment bock in IntelliJ IDEA on the Mac.
Line Comment
⌘+/
Block Comment
⌘+SHIFT+/
The Cmd+Shift+/ doesn't work under Mac with Spanish keyboard layout, because this shortcut it's bind to the main character on the key, which in this case is 7, not /.
For someone else having the same issue, pressing the Cmd+- (Ctrl+- in Windows) is the shorcut for single-line comment while Cmd+Shift+- works for multi-line comments.
use this Simbol
/*
write any thing
*/
For windows
Ctrl + Shift + /
For mac
Cmd+Shift+-
If the Cmd+Shift+/ doesn't work for you (often the case with spanish / german and other keyboards) and you don't want to set it to US/EN keyboard, then use:
Cmd+Alt+s to open the IntelliJ-settings -> select Keymap and search for "Comment with Line Comment" or "Comment with Block Comment". Then rightclick on it and select "add Keyboard Shortcut" and set your own shortcut, for example: Cmd+Shift+NumPad /

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