IntelliJ - Count number of words in a text selection

Viewed 1726

IntelliJ shows the number of characters and linebreak at the right bottom corner of the screen when we select a text. I'm trying to figure out if there's a way to count number of words in a text selection.

3 Answers

The TeXiFy IDEA plugin offers great latex support and has a word count function included:

enter image description here

This word count function only counts "real" words and ignores commands. Hence, it's only able to work wit tex files.

See this screenshot for the result: enter image description here

It looks like there's no built-in way to do this, but we can leverage External Tools to accomplish this.

I'm using IntelliJ on Linux, so wrote a bash script like the below using wc

$ cat wordCounter.sh
#!/bin/bash
echo -e "$1" | wc -w

$ chmod +x wordCounter.sh #make it executable

Then, in IDE navigate to File -> Settings -> External Tools -> (Click on + to add a new tool)

External Tool details:

  • Name: word counter
  • Program: /path/to/wordCounter.sh
  • Arguments: "$SelectedText$"
  • Working Directory: $ProjectFileDir$

Click on Ok -> Apply. Now to count words, select the text, then right click and select External Tools -> word counter

The selected text will be passed to the script, and will output the total number of words.

@Junaid's answer works well if you don't have any quotes in the highlighted text, which messes up the count in certain circumstances. If you're OK with counting all the words in the WHOLE FILE an alternative using the same basic approach to follow the same process with the following alterations:

$ cat wordCounter.sh
#!/bin/bash
wc -l "$1"

This feeds the file itself directly into wc. Then in the External Tools section, use this for the Arguments field: "$FilePath$" (including the quotes).

Related