21

does Visual Studio 2010 not have a "join lines" keyboard shortcut?

EDIT - That is when on line X anywhere, I hit a shortcut key once, and then line X+1 joins to line X (eliminating CR between them so to speak)

Greg
  • 34,042
  • 79
  • 253
  • 454
  • I'm not going to do the legwork for you, but you might look into how VsVim implements the `J` operator to join lines. – Mark Rushakoff Oct 01 '10 at 00:14
  • What do you mean? Deleting at lines' beginning/end will join two lines. What are you thinking of doing? – Graham Perks Sep 30 '10 at 23:40
  • That is when on line X anywhere, I hit a shortcut key once, and then line X+1 joins to line X (eliminating CR between them so to speak) – Greg Sep 30 '10 at 23:43
  • @Greg- It appears your three identical comments need to be merged not joined. :-) – Ray Sep 30 '10 at 23:46

7 Answers7

19

This is not exactly what you want, but I find it useful nonetheless.

If you are at the end of the first line, press Ctrl+Del to join the next line and delete any white space between them.

You still have to be at the end of the line, but this will work on virtually every editor, without any modifications.

javs
  • 798
  • 10
  • 28
16

I use the CodeMaid extension for this, it provides a Ctrl + M Ctrl + J shortcut to join lines (and some other useful things too)

Jasper
  • 2,166
  • 4
  • 30
  • 50
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
5

As I far as I know it does not.

However, you can create and save a new VS macro using the following code:

Sub JoinLines()
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ExecuteCommand("Edit.Delete")
    DTE.ActiveDocument.Selection.EndOfLine()
End Sub

and assign a keyboard shortcut to it (like CTRL + j)

This code will join the current line with the one right below it.

Ray
  • 187,153
  • 97
  • 222
  • 204
  • I think he wants to join with the line below it, meaning that you can probably eliminate the first 2 lines of the macro. – Gabe Oct 01 '10 at 00:11
  • Assuming he's asking because he's a vim user, you'd actually want: EndOfLine, delete, insert a space (I think), move left one character. – Noah Richards Oct 01 '10 at 01:08
  • this looks good - just stuck trying to figure out in VS2010 how to do the keyboard assignment to the JoinLines Sub method... – Greg Oct 01 '10 at 10:42
  • @Greg- `Tools -> Options -> Environment -> Keyboard`. In text field for `Show commands containing:`, simply type `JoinLines` and your new macro should display. Then in `Press shortcut keys`, type `Ctrl + j`, click `Assign`, and finally `OK`. It should work after that. – Ray Oct 01 '10 at 16:01
  • Macros are not supported in Visual Studio 2012/2013: http://stackoverflow.com/questions/12062515/can-i-record-play-macros-in-visual-studio-2012-2013 – northben Jun 02 '14 at 02:16
  • How do I make a macro in VS Code? – mylord Mar 20 '16 at 15:44
2

If you want the join feature to act like Vim (pressing Shift + J) then use this macro that joins, inserts space and places cursor after the space:

Sub JoinLines()
    Dim textSelection As TextSelection = DTE.ActiveDocument.Selection
    With textSelection
        .EndOfLine()
        .Insert(" ")
        .Delete(1)
    End With
End Sub

Just assign it to something like Alt + J (as Ctrl + J and Ctrl + Shift + J are taken).

Jasper
  • 2,166
  • 4
  • 30
  • 50
Piers Myers
  • 10,611
  • 6
  • 46
  • 61
2

Microsoft Visual Studio Professional 2019 (am on Version 16.8.4) now has a shortcut inbuilt yayy!
It's shift+alt+ L & then shift+alt+ J
I remember the shortcut thinking of Lines(L) - Join(J) (it could have been J ... L but ah well, grouping i think)
This is available in Menu Edit -> Advanced -> Join lines

gawkface
  • 2,104
  • 2
  • 27
  • 29
  • 1
    Be aware that this has odd behaviour and a random undesired behaviour (as of v16.8.5): the last character(s) on a line joined to is randomly deleted, sometimes more than one character is deleted (multiple characters removed may only occur with older versions); if the cursor is on the end of line, then the cursor is moved to the next line after the join (really annoying if you are doing multiple joins). – Metalskin Feb 16 '21 at 04:29
1

I had been using CodeMaid for this, but it is very slow with large files. To replicate CodeMaid's behaviour with a macro, I've combined Ray Vega's and javs' solutions into the following:

    Sub JoinLines()
      DTE.ActiveDocument.Selection.EndOfLine()
      DTE.ExecuteCommand("Edit.WordDeleteToEnd")
      DTE.ActiveDocument.Selection.Insert(" ")
    End Sub

Note: As macros have been dropped in VS2013, I'm using the Visual Commander extension, so the macro actually looks more like:

Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic

Public Class C
  Implements VisualCommanderExt.ICommand

  Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
    DTE.ActiveDocument.Selection.EndOfLine()
    DTE.ExecuteCommand("Edit.WordDeleteToEnd")
    DTE.ActiveDocument.Selection.Insert(" ")
  End Sub
End Class
Nigel Scott
  • 643
  • 6
  • 12
0

Try the End and then the Delete key sequence.

End moves to the end of the line and the Delete key removes the EOL after the cursor.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • This answer ignores the important fact that you want to delete any whitespace leading the second line that gets joined. That's really what is wanted out of a Visual Studio equivalent of Vim's j key. – tpartee Jan 27 '16 at 23:18
  • This copies the spaces (indentation from next line to the line we are joining. Thus extra spaces. – Santosh Kumar Mar 03 '18 at 13:20