Op 30-04-2021 om 00:00 schreef Stepan Bujnak:
> I'm writing a lot of Go code. When editing a Go file in nano, I would like to run
> `goimports -w` which automatically formats the code right before saving. I can do
> that by binding it to a key combination, but I'm interested whether it would be
> possible to run the formatter automatically after pressing ^S or ^X and before
> saving the file.
Your question is a bit confusing: ^S saves the file, but you want to run
the formatter /after/ that but before saving the file???
Anyway, how about creating a little script that first runs /bin/nano on
all arguments, and then checks each argument in turn and runs whatever
on it when it is a Go file? Then alias nano to this script and presto.
If instead you want ^S to format the buffer before saving it to disk,
then you need to rebind things. Say 'bind M-5 savefile main', and
'bind ^S "^[f^[5" main' (where ^[ is a literal escape), which causes
^S to act as if you pressed M-f followed by M-5.
Yes, this is exactly what I wanted and it works like a charm. Thank you.
Benno