I am using neovim to work on a Swift project, so I have set up an autocmd
to run swiftformat
when I write Swift files as shown.
autocmd BufWritePost * if count(['swift'],&filetype) | silent execute "!swiftformat %" | redraw!
The command works as expected, but neovim is not reloading the file.
Even running :checktime
to cause a reload does nothing.
If I add an | edit
at the end of the command, the file is reloaded, but the screen moves to be centered at my cursor, which I don’t want.
If I change the autocmd
to
autocmd BufWritePost * if count(['swift'],&filetype) | silent execute "!swiftformat %" | redraw!
\ | silent execute "!sleep 2 && touch %" | redraw!
then the file is reloaded after write, but my cursor locks up for two seconds on write, which is clearly undesirable.
If I change the autocmd
to
autocmd BufWritePost * if count(['swift'],&filetype) | silent execute "!swiftformat %" | redraw!
\ | silent execute "!sleep 1 && touch %" | redraw!
then the cursor locks up for just one second on write, but the file does not reload.
My only explanation for this behavior is that neovim is checking if the file was modified by checking timestamps, and since the timestamp of nvim’s write and swiftformat’s/touch’s write are so close together that it doesn’t realize the buffer was modified.
I tried removing the sleep
but touch
ing the file with a timestamp in the future to trick nvim, but this was unsuccessful too.
How do I work around these issues? Is there some way I can express that the file really has changed, even if the timestamp has not?
I have included my entire init.vim
in case that would be useful.
" Specify a directory for plugins
call plug#begin('~/.config/nvim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'keith/swift.vim'
" Initialize plugin system
call plug#end()
" Swift indentation
autocmd FileType swift set tabstop=4
autocmd FileType swift set softtabstop=0
autocmd FileType swift set expandtab
autocmd FileType swift set shiftwidth=4
autocmd FileType swift set smarttab
" Automatically run swiftformat on write
autocmd BufWritePost * if count(['swift'],&filetype) | silent execute "!swiftformat %" | redraw!
" coc config
let g:coc_global_extensions = [
\ 'coc-snippets',
\ 'coc-pairs',
\ 'coc-sourcekit',
\ 'coc-json',
\ ]
" \ 'coc-prettier',
" Make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"