vim auto save current file when focus is lost - vim

How to make vim automatically save only current buffer when focus is lost, not all files as described here http://vim.wikia.com/wiki/Auto_save_files_when_focus_is_lost

Based on your comments I can say that you have wrong question: you want not to “save only current buffer when focus is lost” (focus tends to have a meaning of “currently focused window”), but to “write buffer when switching to another one: when it is no longer in your focus of attention”. To complete this you may use
augroup AutoWrite
autocmd! BufLeave * :update
augroup END
, maybe combined with
set autowrite
and
set autowriteall
(sets of situations where first and second two variants are triggered intersect, but neither is a superset of another one).

Instead of :wa (write all), use :w.

Also you can try this
set updatetime=1000
autocmd CursorHoldI * silent w
just put it in your .vimrc

Related

How to correctly reload .vimrc file so it will be equal to restart of vim?

I use MacVim and in my .vimrc file I have map ,V :source $MYVIMRC<CR> binding that allows me to apply the newest version of .vimrc in a case it was recently modified.
However I noticed that strange things can happen, relaunch can slow down vim and some plugins can start to conflict after pressing ,V, when everything works fine if I just close and relaunch MacVim from the scratch.
I'd be very thankful if you could give me a hint on the reason of this behavior as I'd like to have a possibility to update .vimrc file that will completely clear internal vim state and grab new configuration file
The only viable way to re-apply your config to a pristine Vim is actually to restart it.
But the most likely cause of slow downs is the overuse/misuse of autocommands.
Autocommands are added without checking for existing ones. One consequence is that they tend to pile-up if you don't manage them properly and every individual autocommand corresponding to a specific event is executed when that event is triggered, leading to dreadful slow downs.
Here are the two ways you are supposed to use autocommands in your vimrc:
Method #1
" anywhere
augroup nameofthegroup
autocmd!
autocmd EventName pattern commandtoexecute
autocmd AnotherEventName anotherpattern anothercommandtoexecute
augroup END
Method #2
" near the top of your vimrc
augroup nameofthegroup
autocmd!
augroup END
" anywhere
autocmd nameofthegroup EventName pattern commandtoexecute
autocmd nameofthegroup AnotherEventName anotherpattern anothercommandtoexecute
The idea is to create a group of autocommands that clears itself whenever it is invoked and thus prevents them from piling-up.

Is it possible to set different textwidths for go code and comment in vim?

For example, textwidth=100 for go code and textwidth=80 for go comment.
With my OnSyntaxChange plugin, you can trigger settings changes based on the syntax element you're currently in.
call OnSyntaxChange#Install('GoComment', '^Comment$', 1, 'a')
autocmd User SyntaxGoCommentEnterA setlocal textwidth=80
autocmd User SyntaxGoCommentLeaveA setlocal textwidth=100
Put this into ~/.vim/after/ftplugin/go.vim. (This requires that you have :filetype plugin on. Alternatively, you could define an :autocmd FileType go ... (for each line) directly in your ~/.vimrc, but this tends to become unwieldy once you have many customizations.
Alternative
A smaller, non-plugin variant would be a custom gq mapping that changes 'textwidth' temporarily, performs the reformatting, and then restores the original value. But that only works for manually triggered reformatting of paragraphs.

How to activate word-wrap on all unfocused windows in vim?

I'm programming in vim and I'm having one little problem. My vim is multiple times splitted in both vertical and horizontal directions, and I want to activate word-wrap like set wrap when i switch from one window to another with ctrl-w/W, also i want to unwrap like set nowrap on focused one. I have no idea how to do this and I was never good at writing scripts for vim. Any ideas ?
I'm not sure of what you want to archieve, but maybe you could add
autocmd WinEnter * set nowrap
autocmd WinLeave * set wrap
to your .vimrc. This will trigger a set nowrap on a window you enter and a set wrap on a window you leave. From the tests I ran, WinEnter and WinLeave are triggered when you switch between several split windows.
Alternatively, you can replace the * by a *.txt if you just want these command to be issued only on buffers containing a txt file for instance.

How to set the default to unfolded when you open a file?

In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
set foldlevel=99
should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.
You can put this in your .vimrc:
au BufRead * normal zR
It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.
set nofoldenable
Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc
In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:
autocmd BufWinEnter * silent! :%foldopen!
That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)
Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.
But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.
You could map it to keys to enable it.
For example,
nmap ,f :set foldmethod=syntax<CR>
Then while in normal mode hit the ",f" key combination
You can open unfolded file when you put set nofoldenable into your .vimrc file.
autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.

Vim 'autowriteall' option only for specific buffers

The 'autowriteall' option makes Vim save all buffers when quitting. I want to be able to use this option only for one specific buffer (a temp file which will be discarded soon) but not for the other buffers.
How do I get Vim to automatically save changes only for one specific buffer?
It's not quite perfect, but one option would be to use the VimLeavePre autocmd:
:autocmd VimLeavePre <buffer> w
However, you'll have to quit with :q! or :qa! for this to work, otherwise it'll never get as far as initiating the autocmd.
:help autocmd
:help VimLeavePre
:help autocmd-buffer-local
You're going to have to use a combination of autocommands. The immediately obvious relevant ones are:
BufHidden
BufLeave
BufUnload
BufDelete
This will cover hiding buffers, leaving them for other buffers or windows, closing Vim, and deleting buffers. (I think BufDelete is redundant given BufUnload but I'm not totally sure I've considered all cases). Note that VimLeavePre will only work if the buffer you're trying to save is the active one, so it's not what you want.
The template autocommand is going to be
:autocommand {event} {filename} w
Or, if you don't have an easy filename pattern to match or it might not have one at all (in which case the w command will need a filename argument) you can use buffer-local autocommands. These would probably have to be set somehow when creating the buffer, like if it's one spawned by some script to show some certain information. For information on this, see:
:help autocmd-buffer-local
You can get information about the multitude of autocommand events from
:help autocommand-events
maybe what you want is:
setlocal autowriteall
setlocal only enables a function for the specified buffer.
autowriteall is autowrite + save on quit, enew, e and others (h autowriteall)

Resources