vim: call function on save
Asked Answered
K

2

8

I am using ghc-mod in vim and would like GhcModCheck (:GhcModCheck) to be called every time I save a file (:w). Could you please tell me how I can achieve that? I guess there's a more general question: how can I invoke a function on save?

Thank you!

Kitty answered 27/7, 2017 at 9:34 Comment(0)
M
8

You can hook into the BufWritePost event. Globally (i.e. for every file):

:autocmd BufWritePost * GhcModCheck

To do this only for Haskell files, you could modify the file pattern:

:autocmd BufWritePost *.hs GhcModCheck

But it's better to leverage Vim's built-in filetype detection and instead put this into ~/.vim/ftplugin/haskell_OnSave.vim:

:autocmd! BufWritePost <buffer> GhcModCheck
Must answered 27/7, 2017 at 9:45 Comment(0)
W
4

There are Customization tips for ghcmod-vim

One of them is

autocmd BufWritePost *.hs GhcModCheckAndLintAsync

Which is also the answer for the generic question about executing a specific command on save.

Waits answered 27/7, 2017 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.