How can I trim trailing whitespace in Visual Studio 2012 on save? - whitespace

Now that macros are gone from Visual Studio 2012, what is the best way to automatically trim trailing whitespace whenever a file is saved?
I'm aware that Format Document (Ctrl + K, Ctrl + D) will do this, but it presumably changes the file in other ways, and it's not automatic either.
The motivation is given here:
Trailing whitespace is evil. Don't commit evil into your repo

There are at least two extensions that can do this. One is CodeMaid which explicitly will trim trailing blanks on save, and the other is Productivity Power Tools which can run the Format Document automatically on save.
To add an extension from within Visual Studio 2012, select menu Tools → Extensions and Updates.... In the window, select Online on the left and enter your search string for CodeMaid or Productivity Power Tools in the box on the upper right.

Ctrl + H
Find what:
[\u0020]+\r?\n
Replace with:
\r\n
\s instead of [\u0020] delete empty line feeds, probably because it matches line feeds...

You can use Mads Kristensen's Trailing Whitespace Visualizer. It has a keyboard shortcut to remove trailing whitespace from the current file. Ctrl + K, Ctrl + \
Note: this doesn't run on save. You have to explicitly run the command. On the bright side, it visually highlights the offending whitespace, so it's easier to identify cleanup opportunities even in code you haven't modified.

EditorConfig has an option for removing trailing whitespace, and it has a Visual Studio extension. You'll need to add trim_trailing_whitespace = true to your .editorconfig to make it work.

Related

Ctrl+Click on a quote to select everything in quote in Visual Studio [duplicate]

Is there a keyboard shortcut (or customizable keyboard shortcut) to select text inside a single quote '' and double quotes "" in Visual Studio 2012.
eg:
string name = "John Doe";
instead of dragging the mouse to select all of John Doe, is there a way to just point the cursor inside the double quotes and
press a key combination to select it? Wanting to avoid usage of mouse a much a possible.
I found a similar question
Visual Studio / R# Keyboard shortcuts: select string with or without quotes
but the shortcut given will only select a particular text, not the entire text inside quotes
TIA
In researching this I have found two answers:
Answer the first
Ctrl+Shift+W will select the text of the word that your cursor is on within a string. Press it again to select the text of the entire string without the quotes, again to select the quotes, again to select the next logical container and so on.
Answer the second
If you use ReSharper you can use Ctrl+Alt+Right Arrow to do the same thing as Ctrl+Shift+W. You can also use Ctrl+Alt+Left Arrow to reduce your selection by one logical container.
In Visual Studio 2013 it was Ctrl+Right Click. You can use Ctrl+Shift+] for Visual Studio 2015 ;)
TLDR; CTRL+W+W
I am using visual studio 2017 with ReSharper. I don't know if it is ReSharper that added the command or if it is native to VS2017 but using my set-up it is possible to select the text within quotes by putting the cursor in between the quotes and hitting CTRL+W+W
The first CTRL+W will select the word closest to the cursor. Hitting W again will select everything between the quotes.
As of VS2019, with the cursor inside the string, ALT+SHIFT+= will select the whole string, including the surrounding quotes.
I am not aware of such keyboard shortcut.
The following quick trick uses mouse and includes double quotes.
Just double click left to the first double quote, VS will select the entire string (including the enclosing double quotes).
Apparently the functionality that you're looking for doesn't exists (http://msdn.microsoft.com/en-us/library/da5kh0wa.aspx) For do that I use ctrl+shift+ arrows to select quickly but is not a visual studio functionality is global for all text editors.
regards.
In Visual Studio 2013 I was able to do this by Ctrl+Left Click on the start of the string quote (that is on the left side of the (") sign). Moving to VS 2015 this functionality no longer works for me and I would love to know how to trun it back on.
Use (Ctrl + Shift + W) in Visual studio. just move cursor before " and use this combinations of keys.

Visual studio - select word with hyphen

When you ctrl+click or double click a word in the Visual Studio text editor, it stops the selection at a hyphen. So, for example, you cannot select a full GUID with ctrl+click. Is there a way to remove the hyphen as a word separator?
There is an extension that solves this issue for visual studio. "Select Hyphenated" # marketplace.visualstudio.com
There is also a setting in vscode:
"editor.wordSeparators": "`~!##$%^&*()-=+[{]}\\|;:'\",.<>/?",
By removing the hyphen, double clicking selects whole hyphenated words.
I also removed the period and hash for selecting css classes and ids.

Convert virtual spaces into real tabs in visual studio

In Visual studio when I create a newline it sets a caret to the indent that I want. However it's not a real tab indentation until I will type any character at line. So all my empty lines are still fully empty (without '\t') and i want to make VS to convert this virtual indentation into real tabs.
I saw this question is pretty close to what I want but in differense from that I don't write an extension but search an existing solution. Though if there's no other way i will have to do it.

How to navigate to a closing bracket with ReSharper?

Say you have a large amount of C# code in an if statement. If you place your carat next to the opening bracket, is there a hotkey or something in ReSharper that will automatically take you to the closing bracket?
VS offers this shortcut, regardless of whether you have R# installed.
Ctrl + ] will take you to the opening brace. Subsequent presses will jump between the RHS/LHS of the scope.
See Go to Matching Brace in Visual Studio? (now as an answer as requested!)
ReSharper assigns the shortcut (Control + ´ - I have german keyboard) to a different command. In order to restore it go to Tools - Options - Environment - Keyboard, search for Edit.GotoBrace and enter the desired shortcut key.
See to what command it is currently assigned and then remove it for this command first by searching this command and clicking Remove. Then again search for Edit.GotoBrace and assign the shortcut.
Directly assigning without Removing it first didn´t work.
Visual Studio's shortcut is (under the IntelliJ shortcut set): Control + ] when your cursor is on the opening brace goes to the ending brace. The inverse is also true.

How can I make VS2010 behave like VS2008 w/r/t indentation?

Situation
I have a plain text file where indentation is important.
line 1
line 1.1 (indented two spaces)
line 1.2 (indented two spaces)
line 1.2.3 (indented four spaces)
In Visual Studio 2008, when I pressed enter, the next line would also be indented four spaces.
However, in Visual Studio 2010, when I press enter, the next line is indented one tab.
Question
Does anybody know where, in the mountain of preferences under Tools > Options, I can return to the way that Visual Studio 2008 worked?
Under Options > Text Editor > Plain Text > Tabs, I see the following:
If I select "None", then I get no indentation when I move to the next line. If I select "Block", then I get TAB indentation (even though the previous line is spaces).
In Visual Studio 2008, my indentation is set to "Block", and I get spaces.
I have no idea what "Smart" indenting is, or why it is disabled.
"Smart" indenting is essentially asking the language service to do indentation, which covers cases like adding an extra indent level after an { in C# files. Since plain text files don't have a language service, it isn't available here. If it was, the behavior (matching indentation from the line above) would be whatever the language decides to do, though I'm pretty certain it respects the "Insert spaces"/"Keeps tabs" option.
"Block" indenting is asking the editor to take care of it, which it takes to mean "maintain the same indent level as the previous line". It appears that, in VS2008, it copied the indentation from the previous line, whereas VS2010 respects the "Insert spaces"/"Keep tabs" setting.
Can you file a bug on Connect about it? I'm not sure if it was changed on purpose or not, so it will help for this to go to our (the editor team's) triage people to make sure.

Resources