Reputation: 83
I often use gvim to view a few lines of log, so I could add new-lines to make it easy to read. These are not at all important text so I don't have a need to save to file. The problem is when I shutdown my computer, gvim would prompt with a dialog-box "Save changes to Untitled?", and the computer does not shutdown. I want to get rid of this prompt. Any help, or direction to documentation would be greatly appreciated.
I have reviewed posting on vim/gvim. None of the posting addresses my problem. They all had to do with user closing vim/gvim and needed a prompt; my case is just the opposite. I was looking to use a window close event, but didn't find anything yet.
===== more information =====
I only want to get rid of the dialog window shown below, not any other type of warning. Thanks for the responses I have received so far. I am also trying to get this to work.
Upvotes: 2
Views: 1119
Reputation: 83
Thanks for all the responses, especially the one from @DJMcMayhem, below is what I got to work exactly as I want it. a) if I am editing an existing file, or had given it a file name, I will still get a prompt, and will not lose any edit. b) when I shutdown my computer and I had some scratch buffer, I will not get a prompt "Save changes to Untitled?". This way windows will shutdown.
function! CloseOnShutdown()
if eval('@%')==''
set nomodified
endif
endfunction
autocmd FocusLost * call CloseOnShutdown()
Upvotes: 0
Reputation: 195029
As I commented under your question. If you work in a normal buffer, you should exit it with :q!
to discard the changes you made.
But if you want to quit vim without confirmation, you should work in a "scratch" buffer. Regarding what is a scratch buffer:
https://vi.stackexchange.com/questions/11310/what-is-a-scratch-window
You can create a mapping to set these three options:
:setlocal buftype=nofile
:setlocal bufhidden=hide
:setlocal noswapfile
Or simply way, there are a lot of scratch plugins available, you can choose one. https://github.com/mtth/scratch.vim
https://github.com/ViKomprenas/scratch.vim
https://www.vim.org/scripts/script.php?script_id=664
https://www.reddit.com/r/vim/comments/2odei7/scratchvim_a_scratch_window_that_stays_out_of/
Upvotes: 2
Reputation: 76409
When you invoke gvim
in such a situation, invoke it as gvim '+set bt=nofile' FILE
. This will tell Vim that the buffer is not related to a file and will allow you to exit without saving.
If this is too difficult to remember or too burdensome to type, you can create a shell alias or a shell script in a directory in your PATH
(e.g., ~/bin
) and then invoke that instead. Similar things can be done on Windows, but there the steps to make it work are left as an exercise for the reader.
You can also create a command in your .vimrc
which sets the current buffer to be a scratch buffer that need not be saved by doing something like the following, if that's more to your liking:
command! Scratch setl bt=nofile
Upvotes: 2
Reputation: 7669
After playing around with it a bit, I think this should work:
autocmd ExitPre * q!
One problem with this is that it will make it very easy for you to lose unsaved changes. Here's a slightly more sophisticated version:
function! CloseUnnamed()
if eval('@%') == ''
q!
endif
endfunction
autocmd ExitPre * call CloseUnnamed()
This will only automatically close unnamed buffers. If you have unnamed changes in a named buffer (because you were editing a file) this will still halt shutdown. You could also make it so that you automatically save unnamed buffers when you shutdown:
function! CloseUnnamed()
if eval('@%') == ''
q!
else
wq!
endif
endfunction
autocmd ExitPre * call CloseUnnamed()
But I don't really recommend this. I'd rather review the unsaved changes.
Upvotes: 1