Loading ...
Sorry, an error occurred while loading the content.

Re: Word count

Expand Messages
  • Alan G. Isaac
    ... Sorry. I hit send rather than delete when I realized you asked not for a word count but for counts of a word. Cheers, Alan
    Message 1 of 7 , Sep 3, 2001
    • 0 Attachment
      > On Mon, 3 Sep 2001, T S Aravinda wrote:
      >> How to count the number of occurrences of a word in a file?
      >> :%s:\<word\>:&:g
      >> Will give the word count, but I am trying to write a Vim function
      >> to accomplish the same, for better presentation of results. I wrote

      On Mon, 3 Sep 2001, Alan G. Isaac wrote:
      > noremap ;wc <esc>ggVGg<ctrl-g>
      > Alan Isaac

      Sorry. I hit send rather than delete when I realized
      you asked not for a word count but for counts of a
      word.
      Cheers,
      Alan
    • Sven Guckes
      ... This is a job for grep ! ... this just adds more change to the buffer and might get you into trouble with the autowrite feature.. Sven -- Sven
      Message 2 of 7 , Sep 4, 2001
      • 0 Attachment
        * T S Aravinda <tsa@...> [010903 04:41]:
        > How to count the number of occurrences of a word in a file?

        This is a job for "grep"!
        :!grep -c word %
        :r!grep -c word %

        > :%s:\<word\>:&:g

        this just adds more change to the buffer and might
        get you into trouble with the "autowrite" feature..

        Sven

        --
        Sven Guckes@... http://www.vim.org/ [vim-versions]
        Latest Vim VERSIONS http://www.vim.org/hist.html "release history"
        release dates given latest user release: VIM-5.8.7 [010630]
        in format "yymmdd". latest developer release: VIM-6.0au [010902] BETA!
      • Benji Fisher
        ... [function snipped] ... One way is to use Normal-mode searches. Something like ... $put=a:str Add the pattern as the new last line 1 Go to top of
        Message 3 of 7 , Sep 4, 2001
        • 0 Attachment
          T S Aravinda wrote:
          >
          > Hi,
          >
          > How to count the number of occurrences of a word in a file?
          >
          > :%s:\<word\>:&:g
          >
          > Will give the word count, but I am trying to write a Vim function
          > to accomplish the same, for better presentation of results. I wrote
          > the following function:
          >
          [function snipped]
          >
          > Problem with this is, it only counts one word per line, even if many
          > words are present on the same line.
          >
          > How to overcome this problem and write a function that correctly counts
          > the number of occurrences?

          One way is to use Normal-mode searches. Something like

          -----------------code snippet---------------------------
          $put=a:str " Add the pattern as the new last line
          1 " Go to top of file.
          let cnt = 0
          execute "normal! /" . a:str . "\<CR>"
          while line(".") != line("$")
          let cnt = cnt + 1
          execute "normal! /" . a:str . "\<CR>"
          endwhile
          undo " Remove the dummy line.
          ---------------------------------------------------------

          Because of the extra line you add, you do not have to worry about the
          'wrapscan' option, and you do not have to worry about an error message if the
          search fails.

          In Vim 6.0, you can do pretty much the same thing with the search()
          function. This gives a return code, and lets you use it as if 'wrapscan' is
          not set, so you do not need the dummy line. The dummy line is a problem if
          you try to do this on a read-only file.

          Another way to do it is write a function that counts how many times a
          string occurs in a line. Then

          :g/foo/let cnt = cnt + CountInString("foo", getline("."))

          is the idea. (Wrap it in an :execute ...)

          HTH --Benji Fisher
        • Jürgen Krämer
          Hi, ... not exactly, because this only counts the lines containing word , but not all occurences of word . Regards, Jürgen -- Jürgen Krämer
          Message 4 of 7 , Sep 4, 2001
          • 0 Attachment
            Hi,

            Sven Guckes wrote:
            >
            > * T S Aravinda <tsa@...> [010903 04:41]:
            > > How to count the number of occurrences of a word in a file?
            >
            > This is a job for "grep"!
            > :!grep -c word %
            > :r!grep -c word %

            not exactly, because this only counts the lines containing "word",
            but not all occurences of "word".

            Regards,
            Jürgen

            --
            Jürgen Krämer Softwareentwicklung/-support
            Habel GmbH mailto:software@...
            Hinteres Öschle 2 Tel: (0 74 61) 93 53 15
            78604 Rietheim-Weilheim Fax: (0 74 61) 93 53 99
          Your message has been successfully submitted and would be delivered to recipients shortly.