LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Word Count on Kate? (https://www.linuxquestions.org/questions/linux-software-2/word-count-on-kate-824163/)

Raveolution 08-04-2010 12:42 PM

Word Count on Kate?
 
Is there a way I can get KDE Advanced Text Editor (KATE) to do word counts on documents I'm editing?

Thanks!

jasohl 08-04-2010 01:20 PM

Doesn't look like it provides the option by default. But might be a plugin or script that supports it. There are some discussions on it if your Google it.

Raveolution 08-04-2010 02:21 PM

I did google it. I didn't find anything. Posting to LQ was the last resort...

repo 08-04-2010 02:26 PM

http://dissociatedpress.net/2006/04/...count-in-kate/

PTrenholme 08-04-2010 02:34 PM

You can save the file, select "Focus Terminal" to open a terminal window in a tab with it CWD set to where you save the document, and then run wc on the saved document.

Raveolution 08-04-2010 02:50 PM

Quote:

Originally Posted by repo (Post 4055896)

I read that one. I put the script in, rebooted Kate, and it did not work. I've been trying to get that one to work for hours. I did it exactly as Murz said to do it, too.

Raveolution 08-04-2010 03:22 PM

Heh. Okay, well I managed to rewrite the script to give KATE a working word count system. The problem with Murz's script was $msg was not being defined due to some mysterious bug.

So me went paleolithic on that script and me brute force it. Me spruce it up a little, too. Ooga booga.

Do everything Murz said, namely (just in case that web page disappears):

1. Go to Settings – Configure kate
2. In “plugins” enable “External tools”
3. Go to “External tools” tab
4. Click “New”
5. Label: Selection stat

6. Now write the script this way:

Code:

msgww=`echo -E %text |wc -w`;msgwl=`echo -E %text | wc -l`;msgwb=`echo -E %text |wc -c`;msgsw=`echo -E %selection | wc -w`;msgsl=`echo -E %selection|wc -l`;msgsb=`echo -E %selection|wc -c`;kdialog --title "File Statistics" --msgbox "Selection:\nWord Count:        $msgsw \nLines:                $msgsl\nBytes:                $msgsb\n\n\nWhole Document:\nWord Count:        $msgww\nLines:                $msgwl\nBytes:                $msgwb\n\n\n"
7. Executable: kdialog

8. Command line name: selection_stat

Go to Tools, external tools, select "selection stat".

This will work whether you selected some text or want statistics on the entire document.

It now looks almost exactly like what you'd see in Textpad.

PTrenholme 08-04-2010 04:56 PM

O.K., I looked at the code suggested, cut it, and pasted it into Kate. It didn't work for me, either until I pasted it into the Kate terminal. When I did that, I noticed that the backquote, quote, and single-quote characters in the command line had be "converted" to the extended (typographically correct, of course) forms. They look correct in the Kate edit window, but you need to go through and overtype them with the standard keyboard characters. You also need to replace the "-message" by a "--msgbox" in the kdiaog command. (The single, long, dash should, of course, be a double-dash.)

When that's done, you'll get a nice pop-up with the wc outpit in it for whatever you've selected.

Here's what I have in the "Script" section:
Code:

if [ -n "%selection" ]
then
 msg=`echo -E "%selection" |\
 wc -cmwl |\
 sed 's/\s*\([0-9]*\)\s*\([0-9]*\)\s*\([0-9]*\)\s*\([0-9]*\)/Bytes: \4\nCharacters: \3\nWords: \2\nLines: \1/'`
  kdialog --msgbox "$msg"
else
  msg="Please select the text for which you desire statistics."
  kdialog --error "$msg"
fi


Raveolution 08-05-2010 10:10 AM

Quote:

Originally Posted by PTrenholme (Post 4056029)
O.K., I looked at the code suggested, cut it, and pasted it into Kate. It didn't work for me, either until I pasted it into the Kate terminal...

Awesome deal. I knew it wasn't just me. So how do we mark this thread as solved?

GrapefruiTgirl 08-05-2010 10:11 AM

If it's solved, check the "Thread Tools" drop-down right atop the 1st post. :)

begtognen 10-02-2010 08:28 AM

Hmm. I followed the instructions, using PTrenholme's code in the Script section.

I was able to do everything required, but when I exited Kate and restarted, there were no options under External Tools, and no scripts listed under the External Tools menu.

Is this because I'm running Kate under Gnome, do you think? Or am I doing something wrong?

Thanks so much for any help/suggestions you might have.

PTrenholme 10-03-2010 02:04 PM

Hum. Well, I tried replacing the kdialog call with a dialog call, and it still failed to work on a GNOME system. So, all I can think of is that kate is using a KDE-specific method of running an embedded script. (Since kate is part of the KDE development kit, having an embedded KDE dependency is, of course, neither surprising nor a bug.)

Does gedit support script extensions? If it does, perhaps the script, or its equivalent, could be used with it.

begtognen 10-05-2010 06:49 AM

Ptrenholme - Thanks so much for getting back to me. I checked, and it turns out that gedit supports wordcount natively. Hooray!

Zelator 12-11-2011 03:12 AM

Solution works for Kate 3.5.3
 
Thanks Raveolution! I've just installed Mepis 11 and my old fix was no good. Someday the Kate developers may supply a wordcount, but until then I'll have to rely on heroes like you. Or learn to code it myself.

quixote9 05-12-2012 05:58 PM

I know that this is an old thread, but word count in Kate is such a huge gap, and PTrenholme's script at comment #8 actually provides a solution. So I felt I had to point that out, because here it is, 2012, and I searched for hours before stumbling on this. I'm using kde 4.7 running under LinuxMintDebian squeeze, with update4 applied.

Step by step: Sessions > Configure Kate. Under Application > Plugins, I ticked the box for "External Tools". Hit "Apply". That causes an "External Tools" entry to appear as a choice under Application. Select Application > External Tools, select the "New" button. Give the script any label that means something to you, copy and paste PTrenholme's script into the script box, under Executable enter "kdialog" (without the quotes), mime types can be left blank, Save can be None, and command is whatever you want to call this command. For instance "wcount".

Now, if you go to Tools > External Tools one of the choices will be wcount (or whatever you called it). If you use it often, you can go to Sessions > Configure Shortcuts and give it a keyboard shortcut.

(Oh, and hint to the Kate devs: INCLUDE THIS AS STANDARD WITH KATE :D )

Zelator 04-20-2013 12:56 AM

Now UNSOLVED again
 
The history is at https://bugs.kde.org/show_bug.cgi?id=65740
Alas the External Tools plugin has been dropped as too many bugs had crept in - http://kate-editor.org/2011/08/09/ma...-tools-plugin/ - so that solution is out.
At some time in the future we may get something via Python - http://kate-editor.org/2012/06/26/ex...te-with-pytho/

PTrenholme 05-16-2013 10:16 PM

Activate the "Text Filter" plugin, select the text, and start the filter (Tools->Filter Text, or Ctrl+/). In the pop-up box, check the "Copy" box and enter the "wc -w" command in the text box. You word count will be the top entry in your clipboard, which you can paste anywhere you want, or just look at in your copy stack.

Zelator 05-17-2013 03:24 AM

Indeed, the Tools->Filter Text does work, but if, like me, you need to check the word count frequently, it is a bit cumbersome. I have Konsole open with a wc command in it that I can alt/tab to and from very quickly. It's either that or Libre Office until someone fixes Kate. I'd love to do it myself but the learning curve would be over the horizon.

PTrenholme 05-19-2013 03:18 PM

A general solution: word count for any text selection using Klipper
 
O.K., here's a "work around" that works for me:
  1. Create this script, say kwc, in a directory in your search path. (I have a ~/bin directory I use for such things.)
    Code:

    if [ -n "$1" ]
    then
      msg="$(echo "$1" | wc -cmwl | gawk '{printf("%s line%s\n%s word%s\n%s character%s\n",$1,($1>1)?"s":"",$2,($2>1)?"s":"",$3,($3>1)?"s":"")}')"
      kdialog --msgbox "${msg}"
    else
      msg="Please select the text for which you desire statistics."
      kdialog --error "${msg}"
    fi

  2. Make the code executable (e.g., chmod +x ~/bin/kwc)
  3. Install the clipboard tool (I use Klipper since I run under KDE, but I think that GNOME has a similar tool.) Note: The following assumes klipper.
  4. Right click on the clipboard in the task bar, and click the "Enable user actions" box.
  5. Create a action, with no regular expression, containing kwc %s. (Use whatever your name choice you made, and any description you want. Be sure to pass the %s argument to the script -- that's the "current active clipboard contents.")
  6. Open the Klipper "Configure Kilpper" item, and verify that the "Synchronize contents of the clipboard and the selection" is checked, and, optionally -- depending on how else you use you clipboard -- check the "Text selection only" box. (Note that you can also change your history size here.)
  7. Close the Klipper configurarion window.
  8. Go to kate or, actually, anywhere you can do a text selection, and select some text.
  9. Right-click on the Klipper in the task bar, and click on the "Manually select a user action." item, and then select kwc (or whatever you called it) in the pop-up.

maredeb 07-03-2014 11:18 AM

Simple wc solution
 
If you have the terminal activated, there is a simple solution to use wc in it:

* type wc<enter> in terminal, wc is waiting for input now
* select text you want to count
* tools - pipe to terminal
* press ctrl-d in terminal to end the input

voila: wc counts for you

NitroGL 01-12-2016 08:53 AM

A very simple solution
 
A very quick solution is that of using REGEX.

Press "Edit -> Replace..." (you're not going to replace, but if you pressed "Find..." you could not see the "mode" section.
Be sure that "Mode: " is set to "Regular expression"

Write one of the following into "Find: " box to count:
  • characters: [^\0]
  • words: [^\s]
(including square brackets)

Finally, press "Find All". Its result count is what you need.

If you forget it, once used, you can retrieve them in the history of "Find: " box.

pavlo_m 03-07-2018 02:31 AM

Quote:

A very quick solution is that of using REGEX.
Unfortunately this not works. I tried with Kate 15.12.3 and 16.08.0 and they have same problem with search. It stops search after a quite small threshold (few thousand search occurrences) on a quite small data portion. The threshold is not constant and it can count different number on same conditions (text and search). You will not get any alert and just get wrong result. I test with file not stored on disk (just copy/paste few lines in editor window)


All times are GMT -5. The time now is 12:54 PM.