70

Is there a way to select and edit multiple rows of code in netbeans?
Visual Studio allows a similar operation where you press alt and select multiple lines using the dragging motion (left click) of mouse ?

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
  • This is probably a better fit for http://superuser.com – beatgammit Nov 25 '12 at 09:26
  • 8
    I disagree, I am pretty active on super user and its not really the right place for this question. It lies in a bit of grey area and stackoverflow suits the purpose better. for e.g. http://stackoverflow.com/questions/9460945/how-to-multiple-line-editing-in-eclipse. – Shashank Shekhar Nov 25 '12 at 09:32
  • 2
    Yeah, it is a programming tool, which is why I didn't vote to close or downvote. – beatgammit Nov 25 '12 at 09:34

6 Answers6

96

It's called rectangular selection.
It can be enabled by Crtl+Shift+R

Screenshots for steps:

Rectangular Selection

By Petr on Oct 20, 2011

If you use development build, you probably noticed that NetBeans editor added Rectangular Selection action. I don't need this functionality every day, but it can be helpful in some case. One of such case is when you need to delete line numbers in a text or code copied from a tutorial. Like on the picture below.

sample code

You can select the rectangle after pressing Rectangular Selection toggle button in the editor toolbar or pressing CTRL+SHIFT+R shortcut.

Rectangular Selection toggle button

The selection can be easily done with mouse or keyboard. When you use keyboard, just place the caret on a corner, keep down SHIFT and with the arrows keys you select what you need.

keep down SHIFT and with the arrows keys you select what you need

When the selection is done, you can easily delete all the line numbers with pressing DELETE key. Then you have to exit from the rectangle selection mode (CTRL+SHIFT+R or toggle button in the editor toolbar).

If you write a text , then the text is placed on every line in the selected area. This can be useful for example for changing access modifiers of more fields in a class at once.

If you write a text , then the text is placed on every line in the selected area

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
  • 12
    Is it possible to place the cursor in different locations? i. e. in the first word, in the second word of second line and third word from third line. – Julian Moreno Feb 06 '14 at 15:40
  • 1
    I don't think so, it selects by columns in the page, so if the three words in the three rows start from the same column you can do it – Shashank Shekhar Feb 06 '14 at 16:04
  • 3
    its possible to place independent cursors with ctrl+shift+left-click - at least in the latest builds – easteregg Jun 27 '16 at 08:34
  • There is a new way. It works exactly like in Sublime. See my answer. – DevWL Aug 05 '16 at 07:27
44

A more flexible alternative to the Rectangular Selection has now been incorporated in Netbeans 8.2, which supports multiple cursors.

Demonstration of multiple carets

Keyboard shortcuts are listed on the Netbeans Wiki:

Description                      Windows/Linux      Mac
==============================   ================   ================
Add/remove caret                 Ctrl+Shift+Click   Cmd+Shift+Click 
Add caret for next occurence     Ctrl+J             Cmd+J   
Add caret for each occurrence    Ctrl+Alt+Shift+J   Ctrl+Cmd+Shift+J    
Add caret on line above          Alt+Shift+[        Ctrl+Shift+[ or
                                                     Alt+Cmd+Up
Add caret on line below          Alt+Shift+]        Ctrl+Shift+] or
                                                     Alt+Cmd+Down   
Remove last added caret          Alt+Shift+J        Ctrl+Shift+J    
Paste over the multiple carets   Ctrl+Shift+L       Cmd+Shift+L or
                                                     Alt+Cmd+V  
Remove all extra carets          Escape             Escape
Martin Smith
  • 3,687
  • 1
  • 24
  • 51
11

After Netbeans 8.2 release

Since Netbeans 8.2 this functionality is available out of the box. Just press Shift + Ctrl and hold it down, now left click your mouse anywhere as many times as you need to.

NetBeans Development version allows you to edit multiple lines like in Sublime Text.

Don't bother to read the answer below. You don't need to download Development release any longer.

Before Netbeans 8.2 release

Yes you can! Works exactly as in SublimeText but it is not default feature so You will have to download NetBeans development build.

enter image description here

After installation press Shift + Ctrl + Alt now left click anywhere in the code while holding Shift + Ctrl + Alt. Easy and very useful.

This is how it looks like in NetBeans:

enter image description here

See video presentation at: youtube.com/watch?v=iWejyPL5YQI

When you install new NetBeans you don't have to uninstall the old one. Just leave it be. Development build will ask you if you want to import all the plugins and presets you where using in your previous NetBeans installation.

DevWL
  • 17,345
  • 6
  • 90
  • 86
  • 3
    In Ubuntu (GNU/Linux) I found that the shortcut is Shift+Ctrl, and that the Alt stops it working. – halfer Feb 22 '17 at 13:57
  • 1
    Some here in another distro. But I don't think it's an OS thing. It's just Shift + Ctrl, not Alt. See ms609 answer for more. – mayid Apr 03 '17 at 19:50
  • 1
    Yeah, Shift+Ctrl on Win 7 too – Eagle May 18 '17 at 11:36
  • On windows 10 with NetBeans 8.2 (updated 6.10.2017) works this: hold down Shift+Ctrl and click on places where you want to have your cursors -> just like in sublime text, works prefectly! – Ales Oct 06 '17 at 18:42
5

From Netbeans Wiki: Add/Remove caret and enter multi-caret mode

Shortcut Windows/Linux:

Ctrl + Shift + Click


Shortcut Mac:

Cmd + Shift + Click

Kelly
  • 573
  • 6
  • 13
4

In netbeans 8.2 on Linux the hotkey is shift + ctrl and holding left button's mouse. From my own experience. It is easy ascertainable.

sadalsuud
  • 460
  • 4
  • 12
0

It seems to be a bit different for previous versions of Netbeans and different OS's. For my part, for Netbeans 8.2 on Windows 10, holding down Shift + Ctrl and left clicking works.