On 03/09/2021, Marc Chantreux <m...@unistra.fr> wrote:
>> 'abc' in FILE, from within vi.
>
> * % means 'the current file' in vi commands so you can write

That's helpful; thank you!

> * | is the command separator
> * grep has a -c flag to count occurrences
>
> so you can write:
>
>     :w|grep -c abc %

That doesn't really fit the bill:

1. This error message is produced: 'The grep command is unknown'

2. grep only counts the number of lines.  If the 'abc' reappears in
the same line, grep won't catch that.  My version will, however
crufty, though it's arguably better/shorter/more portable to use your
%:

  :!cat % | tr '\n' ' ' | awk '{print gsub(/abc/, "")}'

And yes, I did also try
  :%!tr '\n' ' ' | awk '{print gsub(/abc/, "")}'
but the problem with that is, it replaces the buffer with the number
of occurrences.  So cat it is unless someone can point to something
better.

> you can also write the content of the buffer to a pipe (my prefered
> solution here):
>
>     :w !grep -c

That produces a grep usage prompt here, along with this message:
'grep -c: exited with status 2.'
Are you sure you've tested this and that you're using just plain (n)vi?
Also, again, grep -c, which I did know about, is insufficient for
counting every occurrence.  I suspect there's prolly s/th better than
my clumsy kludge, but grep -c is not de wey.

>> Sadly, :E doesn't actually work in vim.  It says
>> > E464: Ambiguous use of user-defined command
>
>> I don't know what works in vim. vim prolly has a dozen
>> chrome-electroplated ways to do the same thing, but I don't know them.
>
> :h windows
>
> also:
>
> :h :new
> :h :enew
> :h gf
> :h :bu
> :h :ls
> :h :bw
>
> the following code isfrom my ~/.vimrc so i can:
>  * navigate into buffers with <left> and <right> arrows
>  * come back to the first buffer with <down>
>  * have a fzf menu of the current buffers using <up>
>
> set hidden
> command! -nargs=0 BU redir! > ~/.v.json | silent echo
> json_encode(getbufinfo()) | redir END
>   \| silent exec "!vim_BU ~/.v.json ~/.v"
>   \|so ~/.v
>   \|redraw!
> nnoremap <up>     <esc>:BU<cr>
> nnoremap <down>  :bfirst!<cr>
> nnoremap <left>  :bnext!<cr>
> nnoremap <right> :bprevious!<cr>
>
> the code of vim_BU (i can probably get grid of perl and use jq instead):
>
> vim_BU () {
>       perl -MEnv -MJSON -w0 -nE'
>               map +( printf "%3s %s%s %s\n"
>                       , $$_{bufnr}
>                       , ( $$_{hidden}       ? "h"       : "a" )
>                       , ( $$_{changed}      ? "+"       : " " )
>                       , ( length $$_{name}  ? $$_{name} : "NONAME" )
>               ), @{ decode_json $_ }
>       ' $1 | fzf | awk '{print "bu "$1}' > $2
> }

Wow, that's... helpful(?), I think, but truth be told, I've tested
none of this, because I'm slightly overwhelmed by what to me looks
like far greater complexity than :E, ^W etc. -- and even than my
occurrence-counting one-liner.

Maybe I'm just not truly ready for vim (though I do use it at times).

> regards
> marc

None of the above is intended as throwing anything back in your face
however, and I thank you very much for your reply.

Kind regards,
Ian

PS: Regarding this:
> get grid[sic] of perl and use jq
Presumably that's <https://stedolan.github.io/jq/>?
Honestly, I hadn't even heard of jq.
It's not in base; I do see it's in ports, but it's not installed on
any OpenBSD machines I've ever had access to.  I see jq is acceptably
licensed, but so long as perl is in base and jq isn't... it makes more
sense for me to learn perl than jq.  Because I'm not great with perl
either, but still.

Reply via email to