2

:cdo print command (intended to print every line in the quickfix list), shows generic file information instead of line output for some quickfix entries. Why is this?

The following example is taken from Practical Vim tip 109 "Call grep Without Leaving Vim". In the code samples, the source text files are in the code/grep directory.)

In the example, the quickfix list is populated with three entries across two text files:

:code/grep
:grep Waldo *
:cli
 1 department-store.txt:1: Waldo is beside the boot counter.
 2 goldrush.txt:6: Waldo is studying his clipboard.
 3 goldrush.txt:9: The penny farthing is 10 paces ahead of Waldo.

However, :cdo print outputs unexpected text "goldrush.txt" 9 lines --11%-- for the first entry instead of Waldo is beside the boot counter.:

(1 of 3): Waldo is beside the boot counter.
"goldrush.txt" 9 lines --11%--
(2 of 3): Waldo is studying his clipboard.
Waldo is studying his clipboard.
(3 of 3): The penny farthing is 10 paces ahead of Waldo.
The penny farthing is 10 paces ahead of Waldo.

Observations

  1. The behavior of :cdo print is different from that of sister commands :{tab,arg,buf}do print, whose output includes all applicable lines, for instance :bufdo print:

    Waldo is beside the boot counter.
    A man has found gold.
    
  2. Redirecting :cdo print to a register shows the expected output.

    :redir @a
    :cdo print
    :redir END
    

    A subsequent :echo @a reveals:

    "department-store.txt" 5 lines --20%-- ((2) of 2)
    (1 of 3): Waldo is beside the boot counter.
    Waldo is beside the boot counter.
    
    "goldrush.txt" 9 lines --11%-- (2 of 2)
    (2 of 3): Waldo is studying his clipboard.
    Waldo is studying his clipboard.
    (3 of 3): The penny farthing is 10 paces ahead of Waldo.
    The penny farthing is 10 paces ahead of Waldo.
    

    Compared to this, the direct output of :cdo print appears to be truncated.


Vim version: 8.2.800

4

:help :cdo explains that the command actually is comprised of several subcommands:

It works like doing this: :cfirst :{cmd} :cnext :{cmd} etc.

The "truncation" you observe are the file open messages of any new files from the quickfix list; these compete with the :print command for the (limited) output area and thus partially overwrite each other. If you don't see the same behavior with :argdo et al., this looks like an inconsistency in its implementation. You may want to open an issue at the bug tracker, or directly discuss this on the vim_dev mailing list. The :cdo command is a later addition; something might have been missed in its implementation, as it mainly is invoked for the side effects of the supplied {command}, and not for the output itself as with :print.

Update: This inconsistency has been fixed in version 8.2.0836. Thanks Yeggapan for picking this up and supplying a patch!

| improve this answer | |
  • 4
    I have created pull request github.com/vim/vim/pull/6155 to address this. – Yegappan Lakshmanan May 28 at 16:21
  • Thanks Ingo for the explanation and Yegappan for the PR. Ingo—though the docs state that :cdo {cmd} is equivalent to :cfirst {cmd}, :cnext, :{cmd}, etc, they also point out that only valid quickfix entries are used by :cdo. :cfirst, however, jumps to the first entry, regardless of whether it is valid or not. The way the docs are worded today, this seems a tad misleading, as the two are not completely equivalent. Would it help to adjust the docs? – mxxk May 28 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.