Search code examples
visual-studiovisual-studio-2013resharpercode-formatting

Visual Studio selectively convert multiple lines of code to a single line - ReSharper?


Let's say I have a trivial if statement as follows:

if(a > b)
{
    return false;
}

Whilst the above matches my defined coding style perfectly, what I want to be able to do is highlight these 4 lines of code, and then very quickly be convert them into a single line as follows:

if(a > b) { return false; }

Is there a way of doing this selectively. I do not want to do it throughout the file - only the lines I highlight. In many cases I will want the if statement left on multiple lines.

I have ReSharper if that helps.


Solution

  • Install the VsVim Extension and then you can place your cursor on the if keyword and then press CTRL+ALT+ twice (assuming you are using the Resharper Visual Studio keymap) to select the whole if block and then press SHIFT+J to join all the lines in to one.

    If VsVim is not for you then you could try the CodeMaid Extension that adds join line functionality (using CTRL+M, J)