Setting of 'formatprg' in ftplugin/awk.vim prevents reformatting

12 views
Skip to first unread message

Gary Johnson

unread,
Aug 13, 2025, 6:36:12 PMAug 13
to vim...@googlegroups.com
I recently discovered the g:awk_is_gawk variable and set it to 1 in
my vimrc since I use gawk rather than awk almost exclusively.
Sometime later, I tried reformatting a comment in a file of gawk
code using gqj and nothing happened. Also, the following was left
on the command line.

:.,.+1!gawk -f- -o/dev/stdout

I found the source of the problem to be this line in
ftplugin/awk.vim:

setlocal formatprg=gawk\ -f-\ -o/dev/stdout

which is executed if g:awk_is_gawk exists and the OS is Unix-like.
(The line was added at commit d58a3bf7d on Sept. 28, 2020.)

I presume that the purpose of this is to allow the user to check for
errors in their gawk code, but it seems wrong to me to hijack the
formatting command for that purpose, mainly because doing so
prevents the user from actually reformatting their code.

I think a better solution would be to create a user-defined command
to do this.

I have worked around the problem for now by putting this line in my
after/ftplugin/awk.vim:

setlocal formatprg<

Regards,
Gary

Christian Brabandt

unread,
Aug 14, 2025, 2:49:22 AMAug 14
to vim...@googlegroups.com

On Mi, 13 Aug 2025, Gary Johnson wrote:

> I recently discovered the g:awk_is_gawk variable and set it to 1 in
> my vimrc since I use gawk rather than awk almost exclusively.
> Sometime later, I tried reformatting a comment in a file of gawk
> code using gqj and nothing happened. Also, the following was left
> on the command line.
>
> :.,.+1!gawk -f- -o/dev/stdout
>
> I found the source of the problem to be this line in
> ftplugin/awk.vim:
>
> setlocal formatprg=gawk\ -f-\ -o/dev/stdout
>
> which is executed if g:awk_is_gawk exists and the OS is Unix-like.
> (The line was added at commit d58a3bf7d on Sept. 28, 2020.)
>
> I presume that the purpose of this is to allow the user to check for
> errors in their gawk code, but it seems wrong to me to hijack the
> formatting command for that purpose, mainly because doing so
> prevents the user from actually reformatting their code.

Not sure I understand, what exactly the problem is. According to my
understanding of the gawk man page, gawk -o should do pretty-printing,
so isn't the above correct for re-formatting?

Thanks,
Christian
--
"Tourists -- have some fun with New York's hard-boiled cabbies. When you get
to your destination, say to your driver, "Pay? I was hitchhiking."
-- David Letterman

Maxim Kim

unread,
Aug 14, 2025, 3:37:39 AMAug 14
to vim_dev
The issue is that gq (with internal formatter) was used to format a comment, now we have formatprg set which in this case formats the code, not comments.

The usual substitute is gw -- it should still work. 

Maxim Kim

unread,
Aug 14, 2025, 3:48:05 AMAug 14
to vim_dev
>  I presume that the purpose of this is to allow the user to check for
errors in their gawk code, but it seems wrong to me to hijack the
formatting command for that purpose, mainly because doing so
prevents the user from actually reformatting their code.

The purpose is to format awk code as far as I know. gq was designed to be used with external formatters, to always (almost) use vim's internal formatter, use gw

:help gw

I have just tried to use formatprg you showed and it works to format awk code I grabbed from internet:

Gary Johnson

unread,
Aug 14, 2025, 3:51:40 AMAug 14
to vim...@googlegroups.com
On 2025-08-14, Christian Brabandt wrote:
> On Mi, 13 Aug 2025, Gary Johnson wrote:
>
> > I recently discovered the g:awk_is_gawk variable and set it to 1 in
> > my vimrc since I use gawk rather than awk almost exclusively.
> > Sometime later, I tried reformatting a comment in a file of gawk
> > code using gqj and nothing happened. Also, the following was left
> > on the command line.
> >
> > :.,.+1!gawk -f- -o/dev/stdout
> >
> > I found the source of the problem to be this line in
> > ftplugin/awk.vim:
> >
> > setlocal formatprg=gawk\ -f-\ -o/dev/stdout
> >
> > which is executed if g:awk_is_gawk exists and the OS is Unix-like.
> > (The line was added at commit d58a3bf7d on Sept. 28, 2020.)
> >
> > I presume that the purpose of this is to allow the user to check for
> > errors in their gawk code, but it seems wrong to me to hijack the
> > formatting command for that purpose, mainly because doing so
> > prevents the user from actually reformatting their code.
>
> Not sure I understand, what exactly the problem is. According to my
> understanding of the gawk man page, gawk -o should do pretty-printing,
> so isn't the above correct for re-formatting?

The problem is that gawk -o doesn't reformat the text within
comments as the default internal format function does. That's what
I expect the gq command to do. There is no other way to access that
internal function than to use the gq command with an empty
'formatprg', whereas there are other means to execute
a pretty-printer on a buffer.

Regards,
Gary

Gary Johnson

unread,
Aug 14, 2025, 4:16:38 AMAug 14
to vim_dev
On 2025-08-14, Maxim Kim wrote:
> >  I presume that the purpose of this is to allow the user to check for
> errors in their gawk code, but it seems wrong to me to hijack the
> formatting command for that purpose, mainly because doing so
> prevents the user from actually reformatting their code.
>
> The purpose is to format awk code as far as I know. gq was designed to be used
> with external formatters, to always (almost) use vim's internal formatter, use
> gw
>
> :help gw
>
> I have just tried to use formatprg you showed and it works to format awk code I
> grabbed from internet:
>
> https://asciinema.org/a/QUuoVqgTi25qYIRpUeBucml64
>
> On Thursday, August 14, 2025 at 5:37:39 PM UTC+10 Maxim Kim wrote:
>
> The issue is that gq (with internal formatter) was used to format a
> comment, now we have formatprg set which in this case formats the code, not
> comments.
>
> The usual substitute is gw -- it should still work. 

gw does allow text to be reformatted without using 'formatprg'.
Thanks for pointing that out. It's not a replacement for gq,
though, since it leaves the cursor where it was so you can't repeat
commands such as gwj on subsequent lines without also moving the
cursor.

I could train my fingers to use gw instead of gq, but repeating a gq
command by using . is just too convenient to forgo.

I can see now why someone might want to set 'formatprg' to use gawk
-o, but having tried it, I don't particularly like gawk's idea of
"pretty", so I'll keep my workaround. Thanks for the explanations.

Regards,
Gary

Reply all
Reply to author
Forward
0 new messages