When I have a new temporary buffer/window and I just do :q, it quits the window but do not clean the buffer. When I quit Vim, it will always popup and tell me there is No write since last change for buffer [No Name].
I have the option for hidden and bufhidden
set hidden
set bufhidden=wipe
It closes the window without warning, but still popup when closing the whole Vim program.
I tried to add an autocmd:
au BufLeave * bw
It works when I quit a window, but will clean the buffer when I want to just open a new window/tab (as it does not distinguish switch window and close window). I also tried BufWinLeave and WinLeave, I did not achieve what I need.
I came up with something like:
function! OnBufHidden()
if expand("<afile>") == ""
execute ":bw! " . expand("<abuf>")
endif
endf
set hidden
autocmd BufHidden * call OnBufHidden()
It should work but it does not. The execute is executed because I tried with some echo inside, but not sure why bw! is not executed.
You can create a scratch buffer in a vertical scratch window with this command:
command! SC vnew | setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile
With :set bufhidden=wipe, the buffer's contents are lost without confirmation, but only if it is hidden (e.g. via :hide) first; :set hidden doesn't do this, it just enables hiding. If you use :q and the buffer is still visible, you'll get the confirmation.
To get what you want, also :setlocal buftype=nofile (as in #romainl's answer). Then, you'll never get a confirmation.
Related
I've let tagbar and NERDtree autoload when vim is opening any file.
The window layout from left to right is:
NERDTree----My source code----TagBar
The problem is, each time vim is up, the NERDtree on the left gets the focus(keyboard). I wish to make the middle window(My source code) having the focus, so I can start coding immediately. Or else I have to C-w l to switch windows each time.
How to set this in ~/.vimrc?
Thanks a lot.
The plugins probably use :autocmd VimEnter to open their sidebars. You can define a similar autocmd, but it has to run after the plugins'.
Put the following into ~/.vim/after/plugin/middleWindow.vim:
autocmd VimEnter * 2wincmd w
This goes to the second available window.
It's a bit tricky, because I don't know when in your start-up sequence, all three buffers are loaded. If you use Vim8, you can do the wincmd on a timer. This works for me:
call timer_start(100, { -> execute( "wincmd l") })
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.
I've tried the following in my .vimrc:
:au FocusLost * silent! wa
autocmd BufLeave,FocusLost silent! wall
And also tried:
How can I make Vim autosave files when it loses focus?
but can't get it to work, when I do a Ctrl+Z or switch to another tab in Terminal (mac) it still doesn't auto save.
BufLeave is triggered when you go to another buffer. Neither <C-z> nor switching to another Terminal.app tab will trigger this because you are using CLI Vim which doesn't care at all about the terminal emulator's GUI or environment and… you are not leaving your buffer.
The same is true for FocusLost (more or less, the doc says that it's GUI only but can work in some terminals without telling which one).
So, these setting will probably work in MacVim but definetly not in CLI Vim.
Actually, since Vim is not aware of your terminal emulator's tabs or about it being put in the background, I doubt you can achieve your goal in CLI Vim.
I happen to have autocmd FocusLost * :wa in my ~/.vimrc but I've put it in an if has("gui_running") conditional and also inoremap <Esc> <Esc>:w<CR> to save on ESC. Hope it helps.
On OS X and Vim CLI, I use this plugin http://www.vim.org/scripts/script.php?script_id=4521
AutoSave - automatically save changes to disk without having to use :w
(or any binding to it) every time a buffer has been modified.
AutoSave is disabled by default, run :AutoSaveToggle to enable/disable
AutoSave. If you want plugin to be always enabled it can be done with
g:auto_save option (place 'let g:auto_save = 1' in your .vimrc).
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.
Though I'm no Vim expert, I've been scratching an itch by working on a rough Vim equivalent of TextMate's ⌘R functionality to run Ruby code from a buffer and display the output.
The script currently just opens a new window (split) with :new and puts the output there. If you run it multiple times, it opens multiple windows. Ideally, I'd like it to reuse the same window within each tab page, much like :help does.
I've looked but haven't found a way to achieve this. Any pointers?
You can create a scratch buffer with a name, so that on subsequent calls you can check to see if that buffer is already open (and if so reuse it) or you need a new one. Something like this:
function! Output()
let winnr = bufwinnr('^_output$')
if ( winnr >= 0 )
execute winnr . 'wincmd w'
execute 'normal ggdG'
else
new _output
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
endif
silent! r! ls
endfunction
I guess you could do it manually.
For Example:
:e test1.txt (or use any existing buffer)
:vs (or :new or :sp)
:b <tab> (keep pressing tab until test1.txt comes up. or use the buff no)
You may want to use the quickfix window so you can also jump to errors. If you get a ruby compiler vim plugin, you can run :make to run your code. You should see output and errors in the quickfix (:copen).