Format multiline code to single line in Visual Studio
Asked Answered
S

7

24

Is there a keyboard shortcut or fast way to change the code below to a single line in Visual Studio 2013? I also have ReSharper installed.

Multi

new XElement("Option",
            new XAttribute("Name", "FileDelete"),
            "1"
        ),

Single

new XElement("Option", new XAttribute("Name", "FileDelete"),"1" ),
Suffering answered 13/6, 2014 at 13:15 Comment(0)
H
14

Just select all the text

and press (control + j)

and it will become 1 line of code

Hospitalize answered 4/11, 2017 at 8:35 Comment(3)
What is the name of the command behind Ctrl + J my key-binding is different from default.Consonant
does it work in 2022Havard
(control + j) open a internal terminal in my visual studio code.Virgina
K
14

For VS2019, default binding is set to Shift + Alt + L + J

Or you could rebind this to something else by going to Tools -> Options -> Keyboard -> search for 'join'

Rebind Edit.JoinLines action to something like (Text Editor) Ctrl + J then press Assign

enter image description here

Kobe answered 12/6, 2021 at 13:50 Comment(2)
This is the only answer that seems to directly address the OP's question.Kipper
Shift + Alt + L + J works in VS2022Informative
P
10

I setup find/replace for quick use with a regex expression like so:

(note: I use VS 2015, so your hotkeys may be different)

  1. Use Ctrl+H to open quick find replace.
  2. Make sure the "Use Regular Expressions" button is active/toggled-on, and that you are set to search in "Selection" (Not "Document" or "Entire Solution" or whatever)
  3. Type
    \s+
    and a space ()
    in the "find" and "replace with" boxes respectively.
  4. Press Esc key to exit quick find/replace.
  5. Now, as long as you don't change anything, you can select any text you want to make single line, and use the following hotkey sequence to quickly format it:
    1. Ctrl+H (Open quick find/replace)
    2. Alt+A (Replace any occurrence of 1 or more White Spc chars with a single space.)
    3. Enter (Close the popup window that says "X Occurrences Found")
    4. Esc (Exit quick find/replace and return to your code)

I use this all the time after visual studio does things like implementing interfaces to turn stuff like

public SomeType SomeProperty {
    get {
        throw new NotImplementedException();
    }
    set {
        throw new NotImplementedException();
    }
}

into stuff like

public SomeType SomeProperty { get { return someField; } set { /*Some Simple Set Code*/; } }
Pigfish answered 25/10, 2016 at 17:1 Comment(2)
Confirmed this works in VS2012. Saved me a ton of time!Clow
Great answer! Like watersnake said...saved me tons of time. Thank you!Problematic
D
4

To make it with ReSharper, you should uncheck the option "Keep existing line breaks" in ReSharper/Options/Code Editing/C#/Formatting style/Line Breaks and Wrapping.

Or just add this line into your .dotSettings

<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_USER_LINEBREAKS/@EntryValue">False</s:Boolean>

Then you could format your code using Code Cleanup Tool (default shortcut is Ctrl+Alt+F) or just by typing semicolons or braces.

Dagley answered 8/10, 2014 at 19:23 Comment(0)
P
2

For me doing Ctrl + J opens the Linux terminal and does not format multiple lines to one line.

This is the fastest way on Linux

  1. Hit Ctrl + Shift + P
  2. Join Lines
Penchant answered 31/12, 2020 at 19:48 Comment(0)
I
0

You can change your VS settings to automatically format code in whatever way you want, then select and retype any line/block-ending character (';' or '}') after the text you want formatted and VS will format it for you.

Intoxicating answered 24/2, 2018 at 21:3 Comment(0)
N
0

You can accomplish this using CodeMaid. The default keybinding is F3, but the command is called CodeMaid.JoinLines if you want to change it

Nievesniflheim answered 24/10, 2018 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.