How to select multiple places in code in Android Studio ?
I want to insert the same text in multiple places in my source code. Like a have more than one mouse cursor. I used to do it in VS2012 so easily though not sure how to do it in Android Studio ?
I found how and lets share this cool feature with you. I found three cool features:
For multiple selection just hold alt + shift then select whenever you want to change by mouse click then type some thing you can write at multiple places at the same time.
Another cool feature is column selection. This lets you to click in a great manner and greatly of help especially when you are refactoring.
In most systems it works with holding middleMouseButton and dragging over your code and in others it works by holding alt and selecting code it acts like below:
the third cool feature is sublime selection it finds the same word in code and let you change that or append that easily. you can do that by pressing alt + j on Windows / Linux and ctrl + g in mac. Look how it works:
Also as #Narayana said in comments, Ctrl + Shift + Alt + j selects all occurrences in one shot, for one-shot refactoring.
You can use Alt + Shift and click multiple locations to for multiple cursor.
To select similar occurrences in files use Alt + j.
For more details : Click Here
Multiline Caret (without mouse)
Windows: CTRL + CTRL(Hold) + ↑ / ↓
Mac: ⌘ + ⌘(Hold) + ↑ / ↓
ESC will end multiline mode.
Change Multi-caret Hotkey
To add a custom Keymap, CTRL+SHIFT+A, type keymap and click on the one with Settings as subtext. Search for Clone Caret Above and Clone Caret Below.
I mapped mine to ALT+SHIFT+↑ / ↓.
Bonus
Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.
For both Mac and Windows, just open the Context Menu and click on the "Column selection mode" to enable or disable the behaviour..
On a MacOS you can use:
Tap: Control + Command + G - Select all the same value
Tap: Control + G - Every tap combination select the new same value
Hold: Option + Mouse Click - Select multiline with a mouse
Hold: Option + Shift and Tap: Mouse Click - duplicate cursor for a each tap place
Press Shift + Alt + Insert combination to edit in Column selection mode.
On a mac I like to do ⌥ (option) + shift + mouse click on multiple lines in Android Studio 3.1.3.
Use the following:
ALT + SHIFT
on Mac , hold OPTION + SHIFT then use the mouse to highlight what you want to select
On Windows, you can try a plugin whose name is ConyEdit. It has a great column mode based on regular expression.
I'm using android studio arctic fox and pressing Alt alone and then selecting code is working properly in windows.
Related
In Eclipse, when I enter code Thread.sleep(100), the code will be wrappered by try catch after entering keys: ctrl+1 and enter. In Android studio what can I do for wrapper it by try catch easily? Is there a shortcut?
Highlight the code you want to surround with try/catch
For Windows it is:
Press Ctrl + Alt + t
For Mac it is:
Press cmd + Alt + t
Select the item you want (#7 for try / catch)
There is no need to "type" try first however, highlighting your code works better.
Highlight the necessary code you need to surround with, and press one of the key combinations depending upon your Operating System.
Windows:
Ctrl + Alt + T
Linux(Ubuntu):
Ctrl + Windows + Alt + T
Mac:
Cmd + Alt + T
A list of Surround With options would pop (like in the below image). You can select the appropriate item of your choice either by pressing the respective number of the item on keyboard or manually via mouse click.
Type "try",
then press alt + ctrl + t,
then select "try / catch".
In Ubuntu Gnome, Ctrl + Alt + T has been taken already by "Launch terminal", run command gnome-control-center keyboard to check:
Either you disable it by press Back Space OR use Ctrl + Alt + Win + T as alternative.
Keep in mind that if "try / catch" option doesn't shows up, it might caused by warning such as "unexpected token" in your code. You need to fix it first.
Try this
Write 'try'
select this word(try)
press Ctrl-Alt-T
Select appropriate one
On Mac it's: cmd + alt + T. Also for most used shortcuts in Android Studio I'd recommend this link http://www.developerphil.com/android-studio-tips-of-the-day-roundup-2/
Select the code you want to surround with try/catch and press Alt + Shift + Z, this will bring up try/catch along with any other applicable templates like this:
To generate a try/catch block type 'try' and press ctrl + space.
The Ctrl + Shift + T shortcut launches the terminal in the eclipse version I am using - 2019-03.
Short Cut of Generating predefined/custom templates
Press Ctrl + Alt + T
Short Cut of Generating predefined/custom templates
Press Ctrl + Alt + T
Surround with many blocks like
try/catch
try/catch/finally
if
if/else
{}
< Edit fold > Commits
Is it possible to some how setup IntelliJ IDEA so that I can column select with the cursor keys similarly to how I might in Notepad++, Visual Studio, or FlashDevelop.
For instance when I'm typing code I almost always do my navigation solely through use of the keyboard. In the IDEs mentioned previously I can quickly select blocks of code by holding Shift + Alt then tapping ↑ to extend my cursor across the lines above. I can then hold Shift + Alt + Ctrl and tap ← or → to quickly jump across words and select the chunk of text I want.
In IntelliJ IDEA I have to constantly enable and disable Column Selection Mode using the Shift + Alt + Insert and even then it doesn't quite function as it does in the other IDEs or Text Editors.
Any ideas?
You can do column editing using the Edit | Column Selection Mode.
The shortcut to turn it on/off is Alt+Shift+Insert. You navigate with arrow keys to select blocks of text.
Multiline Caret (without mouse)
Windows/Linux: CTRL + CTRL(Hold) + ↑ / ↓
Mac: ⌘ + ⌘(Hold) + ↑ / ↓
ESC will end multiline mode.
Change Multi-caret Hotkey
To add a custom Keymap, CTRL+SHIFT+A, type keymap and click on the one with Settings as subtext. Search for Clone Caret Above and Clone Caret Below.
I mapped mine to ALT+SHIFT+↑ / ↓.
Bonus
Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.
It is also possible to select holding middle mouse key.
Go to the Settings | Keymap and set a shortcut for Clone Caret Above and for Clone Caret Below
Most convenient way is to:
MAC: Hold Option+Shift and click with mouse.
Windows: Hold Alt and click with mouse.
On a mac, to toggle block select on/off:
shift command 8
⇧⌘8
For me on Mac:
Press once 'option' key, release it, and press and hold it again.
Now navigate with cursor to select desired code.
Press 'Esc' to exit column mode! :)
I think #Meo's answer is the most correct, but if your hand happens to be on the mouse, you can also accomplish this with Ctrl+Alt+Shift+Left Mouse Button Click. Or look for the Add Rectangular Selection on Mouse Drag in the keymap settings.
In Windows or Linux I press two times Ctrl
For Mac cmd+shift+* didn't work for me. I changed the keymap to something else and now it works seamlessly.
How can I collapse all definitions and methods within the Android Studio editor?
Visual Studio has that option on Edit-->Outlining, but I cannot find a similar feature in Android Studio. I am sure that this feature exists.
How can I access Android Studio's outlining feature?
It is called Folding in Android Studio.
First make sure it is enabled in config (it should be by default). Go to File -> Settings, then under the IDE Settings area find Editor -> General -> Code Folding, check the Show code folding outline.
To collapse/expand items use the Code -> Folding menu.
Edit:
To customize the keyboard shortcuts for these open Settings (File -> Settings) then select Keymap under IDE Settings. Now type folding into the search box (top right). Setup the keyboard shortcut for the various folding actions :)
For Windows:
Minimize: CTRL + SHFT + '-'
Expand: CTRL + SHFT + '+'
For Mac:
Minimize: COMM + SHFT + '-'
Expand: COMM + SHFT + '+'
Here's screenshot for quick reference:
View -> Tool Windows -> Structure
It's the equivalent to outline in eclipse. It gives you a far better overview of the class than folding.
my macbookpro
shift + cmd + ¡ (character or button before delete) Expand All
shift + cmd + ' (character or button after zero) Collapse All
windows
ctrl + shift + + Expand All
ctrl + shift + - Collapse All
If you want turn off all collapsing you need uncheck all fields in the Settings > Editor > General > Code Folding
Also you can use hot keys
ctrl + shift + + — expand code blocks
ctrl + shift + - — collapse code blocks
Windows:
Collapse: ctrl + shift + Numpad-
Expand: ctrl + shift + Numpad+
This is valid for windows users:
Select a bunch of code, then press ctrl+.. This folds your block
Then to remove the folding, move to the first line where the fold starts. Don't select the code, just click in that first line, and again press ctrl+.
This work for me in Windows:
minimize: CTRL + SHIFT + -
expand: CTRL + SHFT + +
Windows 10
ctrl + shift + + to Expand All
ctrl + shift + - to Collapse All
What if i want to save that folding for future and adjust whenever i re-open that file?
Step 1:
We use custom folding
Specify custom folding with comment //region for start and //endregion for end
//region anything that you want to write (you can even leave it blank)
...
//endregion
Step 2:
Go to Settings >> Editor >> General >> Code Folding >> Untick everything >> 'Tick Custom Folding Regions' & 'Tick Show Code Folding Outline' >> Apply >> Ok
Screenshot: https://i.stack.imgur.com/sGuce.png
Step 3:
Restart Android Studio. You will see that the demarcated region has been collapsed.
Here are some helpful shortcuts for folding stuff.
Select Some Code And Press Ctrl + Alt + T to auto Insert Region folding around the code
To Collapse Specific lines of Code, Select the code and Press Ctrl + .(dot/full stop) & Same for the reverse i.e. expand again
To collapse All method bodies/ if conditions etc etc Press Ctrl + Shift + -(hyphen)
To Expand All Press Ctrl + Shift + +(plus)
(for windows)
If you are talking about editor fold just like fcom + tab in netbeans
ctrl + alt+ t
and then add desired text in the desc block
New Android Studio come up with a lot of new functions. These are the commands you can use to fold different type of code.
I had this issue too. I figured out to click gear button where right top of project menu section. Then I unselected Show members option.
For Mac you can use cmd + shift + -(hyphen/minus) to fold and to unfold every thing use cmd + shift + +
For commenting methods separately, do code folding recursively
with ctrl + alt + -
The simplest way I know to collapse the methods is to use the command to collapse all and then the command to expand recursively
like this: crtl+shift+- then crtl++.
you can press crtl+shift+-, release shift then click + for convenience.
Optionally to collapse single method inside class rather than all methods #once use cmd and - on Mac
Cheers
Sometimes I have to write Java class where I need to define multiple fields of the same type. For example, I know that I will need to duplicate private final String on the next 4 lines.
Is it possible to spawn multiple carets in IntelliJ editor, so I can type on mutliple lines at the sime time?
IntelliJ IDEA 14.1 & 15
You can press Alt + Shift and using the mouse left click you can put many carets.
E.g.:
will become
typing "added" only once.
If you want to have a continuous vertical line, it's enough to press Alt + drag your mouse vertically. If there are lines with a length lower than the current position, the vertical line will be broken (carets will be placed at the end of every line) if "Allow placement of caret after end of line" is disabled (in File > Settings... > Editor > General > Virtual Space).
Another way to add a continuous vertical line is to hit Ctrl twice and then press up or down arrow key (supported in JetBrains 2016.X products, and possibly earlier).
If you cannot get the shortcuts working, check what they're currently assigned to under Settings > Keymap Add or Remove Caret, Clone Caret Above and Clone Caret Below. If they look right and you're on Linux, your window manager may be capturing the combination, e.g., for Alt + window drag operations.
See:
http://blog.jetbrains.com/idea/2015/02/multiple-selections-in-editor-using-mouse/
https://www.jetbrains.com/help/idea/2016.3/multicursor.html
I think "Column Selection Mode" could help you. You can enable it in the context menu in the editor. Then you can select multiple lines and type same text at once.
Column Select
For Windows, you can use CTRL+CTRL(Hold)+↑ / ↓. For Mac, replace the CTRL with ⌘.
Change Multi-caret Hotkey
To add a custom Keymap, CTRL+SHIFT+A, type keymap and click on the one with Settings as subtext. Search for Clone Caret Above and Clone Caret Below.
I mapped mine to ALT+SHIFT+↑ / ↓.
Bonus
Try holding combinations of CTRL, SHIFT, and arrows for improved selection power.
On Windows:
You can enable the Column Selection Mode (Alt + Shift + Insert)
And then Shift + ↑ / ↓ can select multiple columns
Hit Esc to go back to single cursor
Add/remove a caret: Alt + Shift + Mouse Click
Remove all carets: Esc
As for now(2018.9), you just:
Press middle mouse key to create a four-rows-high caret
Type what you want
Or,
Create the first line,
Press the "Duplicate entire line" key combination to create a new, identical line.
By the way, I think "multiple caret" should be like carets enabling me to edit different locations which are not necessarily vertically aligned. In Sublime Text you can press Ctrl and click. And in IntelliJ you can only "Clone caret up/down", that is to say, they are vertically aligned, which is not flexible.
I have started developing with Android Studio. In Eclipse I was using Ctrl + Shift + F, but in Android Studio it does not work. It will be different.
How can I jump to any method in a .java file? I was using Ctrl + O in Eclipse.
Is it available? What is the shortcut for code formatting?
Windows: Ctrl + Alt + L
Linux: Ctrl + Shift + Alt + L
macOS: Option + Command + L
Reference: Key Commands and here are all of the commands for Windows/ Linux users and for Mac users.
As Rohit faced a problem in Ubuntu with the format code shortcut, this is due to the Ctrl + Alt + L key being used to lock the screen in Ubuntu.
I found that Ubuntu handles this keyboard shortcut first. So you should bind the Ctrl + Alt + L keyboard shortcut to something else so that it doesn't conflict with Ubuntu.
Steps
Go to System Tools → System Settings → Keyboard → Shortcuts tab → System → Lock Screen.
Select the row New Accelerator..., then press any special key with the Alpha key (e.g. Shift + L). You should've successfully changed the keyboard shortcut.
Check if the keyboard shortcut now works in Android Studio.
Alternative method
Ctrl + Alt + Shift + L (to show a dialog)
You can also use Eclipse shortcuts.
Windows and Linux: Go to menu File → Settings → Keymap
macOS: Go to menu Preferences → Keymap and choose Eclipse from the drop-down menu.
Using MenuBar
Select code which you want to reformat
Click on Code in the menu bar and select Reformat Code as shown in the image below:
In Android studio 3.5, there is problem with rearranging the xml code, it also rearrange the views as well, so need to use these settings first.
Settings -> Editor -> Code Style -> XML-> Set From -> Predefined Style > Android.
Update for Android Studio 4+
Since Android Studio 4+, you might face issue shortcuts on mac. Shortcuts not working after updating to Android studio 4, in Mac? have an answer for that.
You can also use Eclipse's keyboard shortcuts: just go to menu Preferences → keymap and choose Eclipse from the dropdown menu.
The actual path is: menu File → Settings → Keymap (under IDE settings)
Check Keyboard Commands given in the Android Studio Tips & Trick documentation:
Just select the code and
on Windows do Ctrl + Alt + L
on Linux do Ctrl + Super + Alt + L
on Mac do CMD + Alt + L
This solution from junaidp
For formatting code in Android Studio on Linux you could instead use Ctrl + Alt + Super + L. You could use this and avoid having to change the system shortcut. (Super key is the Windows icon key besides the Alt key).
For code formatting in Android Studio:
Ctrl + Alt + L (Windows/Linux)
Option + Cmd + L (Mac)
The user can also use Eclipse's keyboard shortcuts: just go on menu Setting → Preferences → Keymap and choose Eclipse (or any one you like) from the dropdown menu.
You will have to apply all Eclipse shortcuts with Android Studio before using all those shortcuts.
Procedure:
Steps:
Setting -> KeyMap -> Select Eclipse -> Apply -> OK
Now you can use all Eclipse shortcuts in Android Studio...
Have some snapshots here.
Check this. Also you can change it as per your preference.
For auto formatting your Java/XML files in Android Studio, use following commands:
As you are looking for a single point to go with your work as Eclipse did, there is no such built-in functionality in Android Studio, but it gives you the ability to do it through a macro. Follow these steps:
In Android Studio, open any of your source code files.
Press Ctrl + Alt + O This is used to find/optimize unused import.
If a dialog open select "Don't show it again" and hit Run.
Go to Edit > Macros > Start Macro Recording.
Press Ctrl + Alt + O.
Press Ctrl + Alt + L. This formats your code. (Shift+Ctrl+Alt+L for Ubuntu).
Press Ctrl + S. This saves your file;)
Go to Edit > Macros > Stop Macro Recording.
Save this created macro with a name, for instance: “Auto Formatting".
Boom, you have now successfully created your macro. Now just add/assign a shortcut to it:
Open Preferences.
Search in the left bar for Keymap.
In the right hand pane, click in the search bar and type the name of your saved macro ("Auto Formatting".)
Double click on your item. There might be two, it doesn’t matter which one you click on.
Click Add Keyboard Shortcut.
Set your keyboard shortcut to Ctrl + S.
Finally, confirm overriding Ctrl + S to be your new macro.
Just to add to #user2340612 answer to switch keymaps to Eclipse, the path for Android Studio 1.0.1 is:
Menu File → Settings → Keymap (under the Editor option) → Keymaps = Eclipse
Ctrl+Alt+L might conflict with Lock Screen shortcut in Ubuntu. In such case you can change the keyboard shortcut for Reformatting Code as follows:
File-> Settings-> IDE Settings-> Keymap
Search for Reformat Code and change the keyboard shortcut.
Really, I went to this thread because of my Ubuntu locks screen after this shortcut Ctrl + Alt + L. So if you are have the same problem just go to the Settings - Keyboard - Shortcuts - System and change the default shortcut for the "Lock screen".
In addition to auto-formatting that the other answers describe, another trick is to highlight the text that you want to indent and then press
Tab to increase the indent or
Shift+Tab to decrease the indent.
Check out the Keyboard Shortcut docs for more.
For those who are wondering about the alignment issue inside bracket, JetBrains has this as in their issue tracking.
Here is the answer:
How do I align/format code in Android Studio?
I think is clear that for code formatting in Android Studio the combination keys are:
CTRL + ALT + L (Win/ Linux)
OPTION + CMD + L (Mac)
However, we forgot to answer about the Jumping into the method.
Well to go into any declaration/implementation there three ways:
Goto Declaration
CTRL + B or CTRL + CLICK (Win/ Linux)
CMD + B or CMD + CLICK (Mac)
Goto Implementation
These commands show a list of all the classes/interfaces that are implementing the selected class/interface. On variables, it has the same effect as Goto Declaration.
CTRL + ALT + B (Win/ Linux)
CMD + ALT + B (Mac)
Goto Type Declaration
These shortcuts will go into the declaration of the “AnyClass” class.
CTRL + SHIFT + B (Win/ Linux)
CTRL + SHIFT + B (Mac)
Additionally, there is a shortcut for Goto the Super Class. This will open the parent of the current symbol. Pretty much the opposite of Goto Implementation. For overridden methods, it will open its parent implementation.
CTRL + U (Win/ Linux)
CMD + U (Mac)
The shortcut that worked for me is
SHIFT+ALT+CMD+L
You can optimize imports to remove the ones you don't use, and auto import the new classes.
If you are using the Dart plugin, go to Android Studio, menu File -> Settings.
And search for "reformat code with", click "Reformat code with dartfmt" under the main menu:
Click "Add keyboard Shortcut".
Then press Ctrl + Alt + L and the shortcut should work (If Ctrl + Alt + L make the computer sleep/suspend, change the shortcut in your system settings to something else. Otherwise, both shortcuts will collide).
Some times even I type Ctrl+Alt+L is not working in XML, so found this way to make it work.
Go to Settings --> Editor --> Code Style --> Select Default --> Ok.
For your reference see the screenshot:
Try this.
On Windows do Ctrl + Alt + L
On Linux do Ctrl + Shift + Alt + L for dialog to open and then reformat.
On Mac do CMD + Alt + L
Note: Here many answers for Linux is just Ctrl + Alt + L
which is wrong. In Linux, doing Ctrl + Alt + L locks the system.
It's Ctrl + Alt + L for Windows.
For a complete list of keyboard shortcuts please take a look at the user manual: https://developer.android.com/studio/intro/keyboard-shortcuts.html
There are some shortcut keys for rearrange or reformat code in Android studio.
Linux: Ctrl + Shift + Alt + L
mac OS: Option + Command + L
Windows: Ctrl + Alt + L
I have had an experience using Ctrl + Alt + L in Windows that this shortcut did not work at all because it was reserved by another software, running at the background; which I realized later.
I decided to mention this issue as I thought it may be helpful . . .
You can use the following shortcut for code formatting:
Ctrl+Alt+L
The best key where you can find all commands in Eclipse is Ctrl + Shift + L.
By pressing this you can get all the commands in Eclipse.
One important is Ctrl + Shift + O to import and un-import useless imports.