Re: Is it possible to run formatter before saving?

Benno Schulenberg <[email protected]> Fri, 30 Apr 2021 16:47:37 +0200
Newsgroups gmane.editors.nano.general
Message-ID <[email protected]>
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.

Benno
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEv9AJBh5TUFKtDfIVDSjU0qCs6IQFAmCMGJAACgkQDSjU0qCs
6ITKgQ/+OnsYBCuMGI/xtW1xPgOE3k15f6rQKgRtiD7Rt7MmbSmx3gIpkQPD73mB
zICT+cBljZJWDOjHe6lleAB12Zk/ryriLgW1oPf/ubXLLTxOM3Vbq8bjyKEn2jDv
RcQg9C4d29ShPKvLme9NVUUGyxV7EX6tRhKZYK3ZI1+beyZGQwguqwPZPlW34pN3
q7xa4aeqPG0M36raR/8MBX57bvdtyUU9CnSSAVLu8/7g12WfNqqs7H++uWoEv001
FV7hOfCCYV04cy6mYcRhgr3J+CKyfeSOFDzM9k7Ve9sbHnGGksEtGPW6kgyPH9Zn
dMIgV//AUEOIDVFnUA/RChyxqzhp4NKVGcem5/RPhweGR88O29cdsv7meUo1M9Ul
24UchjjVcE6yY1sAfih4SdiQMzQI29t3dl0HQqQrOZEfYEQz7dirc0obCSzG/vaV
eyX7sWrfLj/Kg/SJNrZgTpQuNybaOcFwpPYDUu+a6inUhEFjOgxx47ld5RZubxAN
F6/fHww6AODgo0TrDTCuvKe4KboS7lSuguluNow4PaWPdiWQAsLRmLw5TubZj1u+
zsydOfFiod0bniunK88sg4dB6xfqthtaWS38HtByXZbAYQIi5wb60K/IDpuqUWin
3ZizZQS4NJL+jUVhScwyNM2MSi17hVtrtShYu1vMY8fH3b7Beds=
=/Wg1
-----END PGP SIGNATURE-----