How to shift multiple lines of code in IntelliJ by a single space? - intellij-idea

Am trying to shift multiple lines of code in intellij IDEA by a single space using Tab shortcut but it always shifts by 4 spaces rather than a space. I have tried Edit > Convert Indent > To spaces but to no avail.
How do you shift multiple lines of code by a single space without messing with the default configuration for the file?

As #Andrey already suggested, I would use the Multi Cursor feature that has been around since intellij idea version 13.
I like the Add selection on Next Occurrence feature very much. It helps me to edit multiple lines very fast.
This screenshot shows the action in the Settings -> Keymap screen.

Related

Intellij: delete all blank space until next non-blank character

There is no easier way to explain what I want to do than a picture:
I would like to reduce the time it takes me to refactor HTML code by deleting all the white space behind (or before, doesn't matter that much) my caret until the next non-blank character, emphasized by the highlighted blue whitespace I would like to delete. I found a way to do this on vim, but I want to do this on Intellij.
Try Ctrl+Alt+J (⌃⇧J on Mac) on the <a>... line to perform the 'Join Lines' action.
More information on the feature can be found here and here.

Intellij IDEA 14 - remove indents on empty lines

I have an annoying bug regarding intellij 14.0.3. The issue is that it keeps indents on empty lines and I can't remove that whitespace in any way. Under code style, I have not checked the checkbox "keep indents on empty lines" and judging from the display how that functionality works I'd say it would do it.
However, it still keeps the indents and that creates bad diffs in git since whitespace is added. Is this a bug? Can I in any way remove them? I have tried to uncheck that checkbox under both the language I use and the main one. None of them seems to change it.
Try enabling the Strip trailing spaces on save option in Settings/Editor/General.
You can choose whether this should be performed for All lines or only the lines you modify to avoid creating unnecessary diffs.
The whitespace is stripped when you explicitly hit CTRL+S or automatically after some period (IntelliJ has autosaving).
One thing to note is that if you have cursor on an empty line and there are some spaces before it, hitting CTRL + S won't strip the whitespace, because this would probably be annoying as your cursor would jump to the beginning of the line if the file was autosaved by IntelliJ (I read somewhere on YouTrack that this was a design decision).
Here is a screenshot of the option I describe:
What I did to strip spaces without having to open & save each file is running a regex in the find and replace window:
Ctrl+Shift+R
Text to find ^ +\n Find every line that starts with (^) one or more spaces ( +) and nothing else (/n).
Replace with: \n A new line.
General > Regular expression (obviously important to check this box)
Eventually you may want to limit the scope because this will be quite the lengthy operation
Find and eventually continue if IDEA warns for the high amount of occurrences.
Click All Files to run the actual replace operation. It might take some time before IDEA to respond.

Intellij Idea Code wrapping when formatted

I am new to Intellij Idea. I didn't find any information on how to wrap the code after certain characters (ex: 150 characters, break the line)
I want to break the code after the vertical line, the following code must come in next line.
Note: I used Reformat code option, but it did just realigned the code with some white spaces etc, but not wrapping up the code. It's difficult to see the end of the code.
You go to Settings (alt+ctrl+s) then select Code Style - General
Here you can configure your margins and if you want the code to wrap after it reaches the end of it

Adding spaces in multiple lines simultaneously - in Pycharm

I occasionally need to go line-by line and change the indentation of the code.
Is there a shortcut in Pycharm where I can add spaces (format the indent) in the begining of multiple lines simultaneously without processing each line individually?
Highlight/ select the lines you want indented, then press TAB as often as needed until they reach the proper indent level.
You can remove spaces with SHIFT TAB.
You can also use CTRL+ALT+I to auto-indent the selection.
Select the block that you want to indent then press TAB. It will work.
To auto indent all the codes in Pycharm just press these sequencies:
CTR + A to select all the text files and then press
CTRL+ALT+I
Select all that you want to indent, then do CTRL+ALT+I to indent all of it. This should work in Pycharm
Use shift +alt on windows machine
Continuation indent for Mac pro: Preferences -> Editor -> Code Style -> the language -> Tabs and Indents
If Ctrl + ALT + I is not working and every time needs to press the Insert button to get the pointer.
So go to Tools -> Select Vim if it is checked then it will be disabled.
Now you can intend code directly selecting multiple lines.

How to remove unnecessary blank line on code formatting in IntelliJ?

Is it possible to remove empty/blank lines using code formatting in Intellij IDEA?
Yes. It is possible to configure the number of blank lines in the settings menu (CTRL+ATL+S): File -> Settings -> Editor -> Code Style -> Java(or Scala or whatever your language is) -> Blank Lines
File >> Setting >> Editor >> Code style java >>Blank lines tab
You should change to 0 in code label(as picture), It would remove all unnecessary blank line when press format shortcut: ctrl + alt + L
You can find and replace with regex option also ^(?:[\t ]*(?:\r?\n|\r))+. It searches all empty lines in file. You need to just replace it with empty
I use regular expressions to remove extra blank lines from the code. Here are the instructions.
Open Find and Replace dialog. Use shortcut CTRL+SHIFT+R.
In the search box type ^(?:[\t ]*(?:\r?\n|\r)){2,}. This will search for two ore more blank lines.
In a replace box type \n. This will replace with one blank line.
Open Reformat Code dialog and click Run. Use shortcut CTRL+ALT+L.
This works on all JetBrains IDEs. Use the screenshot as referece.
Just in case it helps someone using newer versions of Intellij, in Intellij IDEA 2016.2.4 it is File -> Other Settings -> Default Settings -> Editor -> Code Style -> Java(or Scala or whatever your language is) -> Blank Lines
For those trying to remove blank lines at the end of the file, this became a feature as of August 2020.
The proper place to configure this action is under Settings|Editor|General|On Save. Check "Remove trailing blank lines"