Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count selected (or found) characters and lines in Visual Studio 2017?

I am developing a combined Embedded + Desktop application. Memory constraints on the embedded platform make it important to know the size and count of items that will be sent and received. When I make size or element count changes on one system I need to coordinate those changes with the other system.

To get this information, I currently select and copy the elements of interest within visual studio and paste them into an external text editor that can give me the character count and the line count.

I would like to find a way to see that same information (count of selected lines and count of selected characters) within Visual Studio (2017).

The only way I've found so far is to perform find and replace within a selection, which does report the count of replacements. If I use regular expressions for the match I can get a count of whatever matched, so with two passes I can get the count of characters and lines replaced -- but then I have to Ctrl-Z to undo the damage, and this just seems like a bad idea. There must be a better way.

Is there any way to get the count of found or selected elements other than using Find & Replace and then un-doing the damage?

Virtually every other editor I've seen permits doing a "find" within a selection and tells you the count of matches, but I've not found any way to make Visual Studio show the count of matches, even though all the found elements are color-marked. I have to do "replace all" to get the count.

I see that there have been periodic feature requests for this same functionality going back quite a few years, but up to now they have been closed for lack of votes. I found macros for VS2010 that can do this, but I don't find any evidence that macros exist in VS2017. I have not found any marketplace extension that gives the count of selected lines and selected characters.

Note that I'm not looking to count lines of code in a file, or a project, or a solution. I'm only seeking to count the characters and lines "on the fly" in a selection within the Visual Studio IDE. I searched, but did not find any questions that appear to be duplicates of this question.

Edit: Sadly, nearly 4 years later it seems there still is no native way to do this in any version of Visual Studio up to and including VS 2022. VS Code does show the count of selected characters, but that doesn't help since I need the full IDE of Visual Studio. Sigh.

like image 810
Craig.Feied Avatar asked Sep 09 '18 16:09

Craig.Feied


People also ask

How do I count characters in Visual Studio?

Open a Markdown file and the status bar will display the number of characters (including newlines) present in the file. It will be updated as you type.

How do I count the number of lines of code in Visual Studio project?

We can check all of the lines of code in a file or directory by the command cloc --by-file.

How do you count characters in a line?

Select the TOOLS menu and then WORD COUNT. A dialogue box will appear containing the character count. Character counting tools many times will provide additional information, such as the character count with and without spaces. In addition, many of the programs will perform a character count of a selected text.


2 Answers

VSCode displays the number of characters currently selected in the status bar, e.g. (143 selected)

like image 75
SMKrizan Avatar answered Oct 07 '22 01:10

SMKrizan


You can use the following command (C# language) with my Visual Commander extension to count selected characters and lines:

public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
    {
        EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
        System.Windows.MessageBox.Show("Lines=" + (1 + ts.BottomLine - ts.TopLine) + " Characters=" + ts.Text.Length);
    }
}

(BTW, Visual Commander can also run a VS 2010 macro.)

like image 44
Sergey Vlasov Avatar answered Oct 07 '22 01:10

Sergey Vlasov