Code reformatting on save in PhpStorm or other jetbrains ide - intellij-idea

How can I set up that the code is reformatted automatically on saving?
So I don't always have to press Ctrl + Alt +
L.

Actually it is possible.
The idea is to record a very simple macro that selects all the lines (Ctrl+A), formats (Ctrl+Alt+L), deselect all (Up and Down arrow) and saves a file (Ctrl+S), and bind this macro to Ctrl+S.
Free Ctrl+S changing keyboard shortcut for "Save all"
open File > Settings;
search for "keymap" and open it;
search "Save All" and double click the action "Save all";
select "Remove Ctrl+S";
double click "Save All" again and select "Add Keyboard Shortcut";
select "Ctrl+Alt+Shift+S" as first stroke.
Confirm and exit from Settings.
Record the macro
Edit > Macros > Start Macro recording
Press Ctrl+A, then Ctrl+Alt+L, then Up arrow, then Down arrow, and finally then Ctrl+Alt+Shift+S
Stop recording the macro clicking on the Stop button on the bottom right of the page.
Give this macro a name like "Format and Save"
Assign Ctrl+S to "Format and Save"
open File > Settings;
search for "keymap" and open it;
search "Format and Save" and double click the action "Format and Save";
select "Add Keyboard Shortcut";
select "Ctrl+S" as first stroke.
Confirm and exit from Settings.
Finally, enjoy PhpStorm!

I use Save Actions Plugin https://plugins.jetbrains.com/plugin/7642-save-actions for PhpSorm

Settings -> Tools -> Action on Save -> Reformat code

Another thing that I found very helpful in combination with this macro is the
Show Reformat File Dialog.
In case you want to only reformat the code you just changed, you can set the option:
Only VCS changed text
The dialog can be opened through the following shortcut:
CTRL+ALT+SHIFT+L

Checked on PHPStorm 2020 & greater versions and found a plugin named Save Action. But make sure you configure for new and exisiting projects.
You can also export your PHPStorm settings and then unzip & replace/create the following :-
Macro File under Settings/Option/macro.xml
<application>
<component name="ActionMacroManager">
<macro name="Format & Save">
<action id="ReformatCode"/>
<action id="SaveAll"/>
</macro>
</component>
</application>
Settings/Keymaps/Default Copy.xml
<keymap version="1" name="Default copy" parent="Default for XWin">
<action id="DBNavigator.Actions.SourceEditor.Save"/>
<action id="Macro.Format & Save">
<keyboard-shortcut first-keystroke="ctrl s"/>
</action>
<action id="SaveAll">
<keyboard-shortcut first-keystroke="shift ctrl alt s"/>
</action>
</keymap>
Now Zip it back & import it & whenever you press ctrl+s - Auto Code Format will be done.

From v2017.2.4 CTRL + ALT + L doesn't work. Use alt + F8 instead.
Solution:
Use: ALT + F8 to format the code.
When creating a macro as described by Luca Mastrostefano, use:
alt + F8 to format and then ctrl + alt + s to save.

Related

Toggle Comments Keyboard Shortcut (CTRL + /) Does Not Work

I use SAS Enterprise Guide and Teradata Studio Express to code for my day job. WindowsOS.
I use CTRL + / shortcut to comment lines of code out in both apps. Suddenly, this shortcut has stopped working. I've checked all my keyboard and Code Shortcut Key settings in both apps and nothing seems out of place!
All other shortcuts seem to work in both apps, common ones I use like CTRL+C (copy), CTRL+X (cut), CTRL+V (paste) etc
I've got no idea why the 'comment out' shortcut is unable to register. Anyone else encounter this same annoying issue?
CTRL + : will pack your line in /* and */
If you have selected a range, Enterprise Guide will do that with every line from which you selected any character.
CTRL + Shift + : will unpack them
You just have to define it again.
Go to Tools-->options-->Enhanced Editor Keys
Select the Command "Comment the selection...." and press Assign keys
within "Press new shortcut key:" enter the ctrl + : key and press assign

Is there a shortcut to go to the beginning/end of a file in IntelliJ?

I often need to get quickly to the very beginning or very end of a file in the IntelliJ editor. Is there a shortcut key or button that allows me to do this quickly?
I am running on Windows, if that matters.
Beginning of File: Fn + ⌘ + ←
End of File: Fn + ⌘ + →
Note: This answer was posted prior to windows specificity. Leaving it here to include MacOS alternative.
On Windows, you can use Ctrl+Home and Ctrl+End to go to the beginning and end of a file respectively.
The action is called Move caret to Page Top/Bottom, default shortcuts would depend on the OS/Keymap and can be checked here:
On Windows these are Ctrl+Page Up and Ctrl+Page Down.
To find what keyboard strokes are already defined for this, go to Preferences->Keymap and search for text start for jumping to the beginning of the file and text end for jumping to the end of the file. You'll get something that looks like this:
The right column tells you what keystrokes have been mapped to these functions. Feel free to change them to something that you find more intuitive by double clicking on the right column and following the instructions on the pop-ups.
Under preferences -> Keymap, a shortcut for Scroll to Bottomand Scroll to Top can be set.
By default, they are not assigned.
By default in windows the shortcut is
ctrl+end
You can customize the shortcut. To use only end button as shortcut go to Settings -> Keymap -> Editor action -> move caret to text end
Right click on move caret to text end and update shortcut to end
On a Mac, these following keys:
to start of the file:
option + command + [
to end of the file:
option + command + ]
After spending minutes to search on the web, i don't find the answer for just using cmd + up or down. If you are the same, as a quick workaround, you can cmd + a to select all text and press up or down to move it.

Copy from Excel to Browser

How do I "paste" in Selenium/VBA to browser textfield?
I downloaded and installed this: https://code.google.com/p/selenium-vba/
I managed to start Selenium in Firefox (CTRL-ALT-S) and recorded my steps for automation. I changed the code language to VBA in there and copied it to paste it in Excel.
For those of you who want to know what to do in Excel (2013):
Right click "Start"
Customize Ribbon
Check "Developer Tools" on the right
Click OK > Click on "Developer Tools" > Click "Visual Basic" > Click "Extras" > Click "References" > Look for "SeleniumWrapper Type Library" and check it > Click OK > In Menu bar click on the top arrow beside "Paste UserForm" > Click "Module" > Now paste the VBA Code from Selenium (Firefox) Plugin in there
Everything works perfectly fine but I want it to do repetitive web browser tasks so it should copy Excel cells and paste it in Textboxes in Websites for example in the Google Searchbar.
I already found a solution for the copying part:
Range("A1").Copy
It works but I can't find any working solution for the pasting part. Tested but didn't work:
driver.findElementById("ID").SendKeys "^v" gives out "v" instead of pasting.
driver.findElementById("ID").SendKeys(Keys.chord(Keys.CONTROL, "v")); Doesn't accept ";" and also doesn't work without.
I also tried several "Wait" and "Sleep" codes but neither do I know for sure which of them actually work nor if they are necessary.
driver.action.key_down(: control) Didn't work.
driver.findElementById("ID").keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0076')).perform(); Syntax error, without ";" "Error: List Delimiter" and marks the "." between "String" and "value".
I am grateful for any advice!
The easiest way is to send the text:
Dim driver As New FirefoxDriver
driver.Get "https://en.wikipedia.org/wiki/Main_Page"
driver.FindElementById("searchInput").SendKeys [A1]
If you really need to use the clipboard:
Dim Keys As New Selenium.Keys
Dim driver As New Selenium.FirefoxDriver
driver.Get "https://en.wikipedia.org/wiki/Main_Page"
driver.SetClipBoard [A1]
driver.FindElementById("searchInput").SendKeys Keys.Control, "v"
Of course it depends on the kind of element you want to paste the text to, but how about just setting the element's value directly;
driver.findElementById("ID").text = Range("A1").value2

IntelliJ IDEA code reformat options

When I press Ctrl + Alt + L, IDEA removes unused import. I want to turn this feature off, leave the unused imports there. Where can I affect this setting?
First check:
File | Settings | Editor | Formatting | Show "Reformat Code" dialog
Then when you hit Ctrl + Alt + L pop-up dialog will appear.
Uncheck Optimize imports and check Do not show this dialog in the future. Hit Enter or click Run. That's it!
In IDEA 2018, you can hit Ctrl + Alt + Shift + L and the reformat file pop-up will appear. Then you can check/uncheck the "Optimize Imports" option.

In NetBeans IDE, is there a key combo to select up or down many lines?

I always run into this situation: cursor is on line 5 and I want to select everything down to line 16. I have to resort to shift-arrow, arrow, arrow, or use the mouse.
NetBeans has "go to line" (Control-G) but you can't hold down shift while doing that, so no good for selecting. There's also "bookmarks," but same problem.
Anyone know of a cool trick that solves this problem?
I'm open to alternate tool suggestions for OS X.
You can use Shift-PgDn or Shift-PgUp to select the next "page" of information.
The only thing that comes to mind is to create an editor macro.
Eg:
Tools > Options > Editor > Macros
Click "New" Enter a name:
Enter a name for the macro: select-5-down
For the macro code enter:
selection-down
selection-down
selection-down
selection-down
selection-down
Click "Set Shortcut" and assign a key binding. I used Alt-M.
Now each click of Alt-M extends the selection by 5 more lines.