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.
Related
I have the following problem, or lets say idea, with vim. When I am writing latex documents I want automatically open a file ~/.vim/latex_hints, where I collected some hints, shortcuts, workarounds,..., in vsplit on the right side. The hint file should be loaded read only and automatically closing when I close the latex document.
After a few experiments I added the following commands to my vimrc:
function Handletexfile()
setlocal cc=80
setlocal wrap
setlocal textwidth=80
belowright vsplit +setl\ ro\ nomodifiable ~/.vim/latex_hints
endfunction
autocmd BufRead,BufNewFile *.tex call Handletexfile()
and
function Handletexfileexit()
let tablist = []
call extend(tablist, tabpagebuflist(tabpagenr()))
for b in tablist
echo b . " ". bufname(b)
if bufname(b) =~ "vim/.*_hints"
echo "Close buffer..". b
execute "bdelete! ".b
endif
endfor
endfunction
autocmd BufWinLeave *.* call Handletexfileexit()
When I open a tex file, my hint file is displayed on the right side as read only and not modifiable. But when I close using :q or :wq the buffers open in the current tab are listed and the one matching to the hint file is selected by the if statement. But I get the following output
1 abstract.tex
2 ~/.vim/latex_hints
Close buffer..2
and my vim crashes with an segfault.
The first part of your requirement translates pretty straightforward into Vimscript:
autocmd BufWinEnter <buffer> belowright vsplit +setl\ ro ~/.vim/latex_hints
Put this into ~/.vim/after/ftplugin/tex.vim, or prepend :autocmd FileType tex to the above command.
The latter part is more complex; on BufWinLeave, you'd have to check all other windows for the opened cheat file with bufwinnr(), go to it (:wincmd w), and :close it.
As an alternative use the preview window with your hint file. The preview window gives you some advantages:
It is small and out of the way
Can close it easily via <c-w>z from any other window
Can jump to the preview window from any window easily via <c-w>P (jump back via <c-w>p)
You can do this via the :pedit command. For example:
autocmd BufWinEnter <buffer> pedit +setl\ ro ~/.vim/latex_hints
Personally I feel like it would be better to create command or mapping to open your hints files as you may eventually out grow the file as time goes on. I would also set the 'bufhidden' to wipe and un-list the buffer with 'nobuflisted'.
You may also want to look into getting a nice snippet plugin.
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
I would like to modify vim's updatetime for files with a specific extension. I've tried accomplishing this by adding the following line to my ~/.vimrc:
autocmd BufRead,BufNewFile *.t set updatetime=60000
This doesn't seem to work. When I open up a file with a .t extension and type :set ut? I see the default updatetime=4000.
The 'updatetime' setting is a global setting, it is not meant to have different values for different buffers. Why do you want a different value?
You can work around this with autocmds, as you've attempted. However, the BufRead,BufNewFile events will only fire when a buffer is loaded; it won't update the setting as you switch buffers. The correct way is to define two autocmds on BufEnter; a general one to reset the setting, and (following that, so that it is executed after the first!) one that matches your file patterns and manipulates the setting:
autocmd BufEnter * set updatetime=4000
autocmd BufEnter *.t set updatetime=60000
I'd like to have the tagbar VIM plugin launch when I open certain filetypes, so I added the following to my .vimrc:
if has("gui_running")
autocmd BufEnter *.hs nested TagbarOpen
However, this isn't working as I expected. It opens a side window, but the side window displays nothing and my cursor is trapped within it. I cannot switch windows with a click or with the CTRL-W movement commands.
However, when I run TagbarOpen manually, it works just fine.
Anyone else tried this, or is the above the wrong command to issue?
Interesting, that's a side effect of a convenience functionality that I hadn't anticipated. What happens is this: If TagbarOpen is called while the window is already open, Tagbar makes the cursor jump to its window instead of just doing nothing (for convenience like I said). So every time you try to leave the window by switching to the Haskell window, the autocommand causes it to jump right back. I've pushed a change that removes this functionality -- it probably wasn't that useful to begin with. So if you give the development version on GitHub a try (https://github.com/majutsushi/tagbar), it should work.
That the window is empty has a different reason: Haskell is not supported by Exuberant Ctags by default. But someone wrote a nice alternative for Haskell that works with Tagbar here: https://github.com/bitc/lushtags.
I actually have this exact configuration set up my vimrc for php files. Tagbar opens with function / variables loaded, cursor stays in php source file when Vim loads:
autocmd FileType php call SetPHPOptions()
function! SetPHPOptions()
setlocal shiftwidth=4 tabstop=4 softtabstop=4 expandtab makeprg=php-xdebug\ %
:call tagbar#autoopen(0)
endfunction
substitute 'php' for 'hs' or any other file type you want. List of filetypes at:
$VIMRUNTIME/filetype.vim
Have it running on MacVim (snapshot 72, Vim 7.4), and latest build of tagbar from https://github.com/majutsushi/tagbar
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)