When I do :update
, I'd like to print a message if the buffer was not written (because it wasn't changed).
I get a message upon save, but would like a message if save didn't occur, also.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityWhen I do :update
, I'd like to print a message if the buffer was not written (because it wasn't changed).
I get a message upon save, but would like a message if save didn't occur, also.
Here is an improved version to support as many of write/update
features as possible. Even if it'd be of a lesser use for this case, it's a good habit to be more precise while creating new commands.
Also try never define a mapping as a prefix of another one (<leader>w
vs. <leader>wq
), as this may lead to surprising delays.
command! -bar -bang -range=% -nargs=* -complete=file Update
\ if &modified
\ | execute printf('%s %d,%dwrite%s %s', <q-mods>, <line1>, <line2>,
\ <q-bang>, <q-args>)
\ | else
\ | echo 'Buffer is not modified'
\ | endif
nnoremap <leader>w :Update<CR>
nnoremap <leader>q :Update<Bar>quit<CR>
With help from this answer, I came up with:
function! s:UpdateVerbose()
if ! getbufinfo(bufnr())[0].changed
echomsg "Save unnecessary: buffer is unchanged"
else
write
endif
endfunction
com! UpdateVerbose call s:UpdateVerbose()
" <leader>w to update file (only if it has changed) - a lot faster than :w<Enter>:
nnoremap <Leader>w :UpdateVerbose<CR>
nnoremap <Leader>wq :UpdateVerbose<CR>:q<CR>