Save file in vim before searching in FZF

I have this mapping:

nnoremap <silent> <leader><space> :Files<CR>

      

This works great, but when I have an already changed file, I get an error when switching to a new file.

Can I save the file before running fzf? I've tried something like this, but it doesn't work if I don't have a file open. (start vi without file and run fzf)

nnoremap <silent> <leader><space> :w<CR>:Files<CR>

      

+3


source to share


1 answer


Use :update

instead of :write

. This will only be recorded if the changes were actually undone.

You can check for an empty buffer with empty(bufname(''))

, but since there are other corner cases I would rather just turn off the error:

nnoremap <silent> <leader><space> :execute 'silent! update'<Bar>Files<CR>

      



Alternative

Alternatively, you can look at the option 'hidden'

as @romainl mentioned. With this, Vim will not complain if the buffer that has unsaved changes no longer appears in the window and only confronts you when you exit Vim. Many power users have :set hidden

.

+3


source







All Articles