Is there a way to have my line reformatted upon completing it in IntelliJ? (For those familiar, this kind of feature is available on FlashDevelop.)
Let's says I just entered this code in PHP:
$var=array("a","b","c");
Upon entering the semicolon, I would like the editor to reformat it to (or whatever my configuration states):
$var = array("a", "b", "c");
This auto-reformat trigger could also be executed on brackets and other line-terminators characters.
There is no such feature in IDEA. You can use Reformat Code (Ctrl + Alt + L) action when needed.
You can actually do something like.
Go to where you want to insert ;
Tools/Start Macro Recording
Type in ;
Then select Code/Auto Indent Lines
Tools/Stop Macro Recording
Name the macro "reformat"
Now go to settings, Keymap, Macros, select "reformat"
Assign a keyboard shortcut of ;
You may have to play with it a bit to get your cursor in the right spot after the macro runs.
This is based on jhormans answer above and i know that the question is more than 6 years old. But IntelliJ IDEA 2017.1.5 still does not have a feature that automatically reformats the current line when semicolon is pressed like VisualStudios.
The workaround is to create a macro as described by jhorman but after pressing ';' run Code -> Reformat Code. Then stop recording and under Settings/Keymap/Macros add the shortcut "Shift+Comma" to the macro. Now when typing the semicolon by pressing Shift + Comma in the code, the code will be reformated aswell. The only shortcoming: the whole code will be reformated each time Shift+Comma is pressed. This may take a second on large files.
This workaround will only work if you just have one line terminator like semicolon. To automatically reformat the code on multiple line terminators (closing brackets, semicolon, ...) you would need to add the shortcuts to "Settings" -> "Keymap" -> "Main Menu" -> "Code" -> "Reformat Code" directly. In this case there's no need to create a macro.
Just formatting the current line isn't possible as far as i know.
Related
The shortcut to comment out code in TOAD (11.6.16) is CTRL+B
However, the problem is that it does not let you comment out an exact selection within a block of code. So, when I press CTRL+B it simply comments out all the lines of the code , so I can't comment out a specific column with /* .. */ via keystroke.
Is there a way to run the arbitrary /* .. */ commenting ?
Not exactly, but you can get there by a lesser known feature. The Search and Replace macro. In the Editor hit Ctrl+R to get to the replace dialog. On the toolbar click the right-most button to get to the macro editor. Copy the entire contents, below, and paste into that dialog within the left side tree. It'll create a macro for you that I made. Hit close, etc. to get out of that dialog. Now, in the editor whenever you make your selection you can invoke this by using the little dropdown next to the replace button on the Editor toolbar. It's not exactly as easy as using keyboard shortcut, but it works and Search/Replace macros are super powerful for more complex work. Here's the dropdown I'm referring to.
Here's the code to copy/paste. Grab everything from (and including) "object" through to (and including) the "end"
object TComponentCollection
Items = {
545046300F5474645265706C6163654D6163726F000B446973706C61794E616D
65060D436F6D6D656E7420426C6F636B0D49676E6F72654661696C757265080A
4D6163726F47726F7570080C5365617263684F726967696E070D736F456E7469
726553636F70650C577261705365617263686573080F50726F6D70744F6E5265
706C616365080A5465787446696E6465720A9C000000545046300F5474645465
78745265706C61636572000D4361736553656E7369746976650811526567756C
617245787072657373696F6E090A5365617263685465726D0606283F73292E2A
0F536561726368446972656374696F6E07097364466F72776172640A57686F6C
65576F726473080B5265706C6163655465787406062F2A24302A2F135265706C
6163655769746854656D706C617465080000054974656D730A32000000545046
301F547464437573746F6D5365617263685265706C6163654D6163726F4C6973
7400054974656D730A0000000000000000}
end
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.
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.
I'm using Intellij IDEA for about a year and I still wonder if there is some type of smart shortcut for this scenario:
new In|[ENTER]
new Integer(|)
new Integer(10|)
new Integer(10|)[RIGHT ARROW]
new Integer(10).| (any method name)
This may sounds silly, but I find myself using the right arrow too much during my work day, so I'm looking for a simple solution to skip this part when typing. Is there any smart shortcut in Intellij IDEA to start typing method name (with dot ahead) while the caret is still where the arguments are (inside parentheses)?
You can create your own macro and assign a Keymap to it like so*:
Edit | Macros | Start Macro Recording
Perform the sequence of steps, ⌘→ followed by .
Edit | Macros | Stop Macro Recording
Name the macro, e.g. "Go to line end and start method"
In Settings, search for the macro name (to make it easier to find), select in Keymap | Macros
Right-click and select Add Keyboard Shortcut, assign the "First Stroke" to ⌃⇧., for example
OK all the dialogs and try it out!
* These are the steps on Mac, there might be slight differences for other OSs.
I was programming in PHP and typing mysql_connect, and by default the single-quotation is automatically closed, so I would have something like this mysql_connect('localhost[cursor is here]'). I still need to type my username and password, it just seems really out of the way to press the right arrow key to escape the single-quotations for writing the other arguments (username/password). Is there a hot key similar to ctrl+enter that can help move my cursor out of the quotations but not to the next line?
I often use Cmd-Right Arrow to move the the end of the line. I also make heavy use of Opt-Right Arrow and Opt-Left Arrow to move words at a time instead of characters.
My solution to this was to record my own macro; it moves the caret to the right, inserts a comma and then a space. I've mapped the macro to command and < (shift and comma). Activating the macro on your code results in:
mysql_connect('localhost', [cursor is here])