Vim keep window position when switching buffers - vim

A problem I've been having with Vim in general is that when I switch buffers in a window (either :[n]b or MiniBufExpl) the cursor position stays the same, but the window always positions itself so the row the cursor on is in the middle.
This is really annoying me since I visually remember where the top/bottom parts of the window are, not where they would be should the cursor be positioned in the middle of the window.
Is there a setting I can change to preserve a window's position over a buffer?

It's interesting to note that it didn't bother me until I've read your question, lol.
Try this:
if v:version >= 700
au BufLeave * let b:winview = winsaveview()
au BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
endif

That script posted by #dnets always sets the cursor at the top of the screen for me, albeit at the same position in the file.
I changed it to this (copied from http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers)
" Save current view settings on a per-window, per-buffer basis.
function! AutoSaveWinView()
if !exists("w:SavedBufView")
let w:SavedBufView = {}
endif
let w:SavedBufView[bufnr("%")] = winsaveview()
endfunction
" Restore current view settings.
function! AutoRestoreWinView()
let buf = bufnr("%")
if exists("w:SavedBufView") && has_key(w:SavedBufView, buf)
let v = winsaveview()
let atStartOfFile = v.lnum == 1 && v.col == 0
if atStartOfFile && !&diff
call winrestview(w:SavedBufView[buf])
endif
unlet w:SavedBufView[buf]
endif
endfunction
" When switching buffers, preserve window view.
if v:version >= 700
autocmd BufLeave * call AutoSaveWinView()
autocmd BufEnter * call AutoRestoreWinView()
endif
And it now works as I want, screen and cursor position saved.

Related

set name to window and reference to it with command

I have 8 square (equal) windows in my vim screen spanning over 2 large monitors and I want to refer each of them with shortcuts < A-1 >, < A-2 > ... . There is a command in vim N-wincmd-wincmd that allows to to reference to the window by its number, but it is useless for me because other plugins sometimes create windows (like syntastic for syntax checking) and referring by number doesn't exactly matches the correct window. I thought maybe I could reference windows by names, so the question is, how do I set a name to some window, and then make a short cut so the cursor goes to the window with that name when pressing < A - n > where n is the window number?
The following lets you save a static extra window number for each visible window, and then jump to it quickly.
Just call :MarkWins when your layout is clean, and then the mappings <A-1>, <A-2>... will jump to the good window, even if new windows were created after that.
" Mark all visible windows from 1 :
command! MarkWins call s:mark_windows()
" Go to a previously marked window :
command! -nargs=1 GoToMarkedWin call s:go_to_marked_win(<f-args>)
" Mappings (Alt-1, Alt-2...) :
for s:n in range(1,8)
exe printf('noremap <silent> <a-%d> :GoToMarkedWin %d<cr>', s:n, s:n)
endfor
function! s:mark_windows()
let l:old_winnr = winnr()
windo let w:win_mark = winnr()
exe printf('%d wincmd w', l:old_winnr)
endf
function! s:go_to_marked_win(n)
let l:old_winnr = winnr()
while 1
if exists('w:win_mark') && w:win_mark == a:n
return
endif
wincmd w
if winnr() == l:old_winnr | return | endif
endw
endf

Is it possible to just view files content when traverse NERDTree?

I can traverse NERDTree, but to see file content I press go, and once opened file's buffer stays open until I explicitely close it. That makes viewing files too uncomfortable.
when I traverse NERDTree nodes I'd like to see the highlighted file content in a temporary viewer buffer and I'd like to explicitely select some of traversed files for editing, say by pressing e.
When I close NERDTree buffer, the temporary viewer buffer shall close as well, and there should only be opened buffers for those explicitely selected files, not for all viewed files.
Is that possible?
Looks like that could be a nice feature request for NERDTree :)
Meanwhile, you could put in your ~/.vimrc something like the following:
let g:nerd_preview_enabled = 0
let g:preview_last_buffer = 0
function! NerdTreePreview()
" Only on nerdtree window
if (&ft ==# 'nerdtree')
" Get filename
let l:filename = substitute(getline("."), "^\\s\\+\\|\\s\\+$","","g")
" Preview if it is not a folder
let l:lastchar = strpart(l:filename, strlen(l:filename) - 1, 1)
if (l:lastchar != "/" && strpart(l:filename, 0 ,2) != "..")
let l:store_buffer_to_close = 1
if (bufnr(l:filename) > 0)
" Don't close if the buffer is already open
let l:store_buffer_to_close = 0
endif
" Do preview
execute "normal go"
" Close previews buffer
if (g:preview_last_buffer > 0)
execute "bwipeout " . g:preview_last_buffer
let g:preview_last_buffer = 0
endif
" Set last buffer to close it later
if (l:store_buffer_to_close)
let g:preview_last_buffer = bufnr(l:filename)
endif
endif
elseif (g:preview_last_buffer > 0)
" Close last previewed buffer
let g:preview_last_buffer = 0
endif
endfunction
function! NerdPreviewToggle()
if (g:nerd_preview_enabled)
let g:nerd_preview_enabled = 0
augroup nerdpreview
autocmd!
augroup END
else
let g:nerd_preview_enabled = 1
augroup nerdpreview
autocmd!
autocmd CursorMoved * nested call NerdTreePreview()
augroup END
endif
endfunction
This is probably quite naive and nasty code, but with some tweaking could do what you intend to do.
Edited, changes in version 2:
Added nested on the autocommand so syntax highlight works
Not enabled by default, execute :call NerdPreviewToggle() to enable/disable
I built on DavidEG's answer by taking tabs into account, because I ran into multiple nerdtree tab edge-cases.
function! PreviewNERDTreeFile()
if !exists('t:previous_preview_buffer') | let t:previous_preview_buffer = 0 | endif
let filename = substitute(getline('.'), '^\s*\|\s*$', '','g')
let should_close_buffer_next_time = 1
if (bufnr(filename) > 0) | let should_close_buffer_next_time = 0 | endif
normal go
if t:previous_preview_buffer > 0
exe 'bwipeout ' . t:previous_preview_buffer
let t:previous_preview_buffer = 0
endif
if should_close_buffer_next_time
let t:previous_preview_buffer = bufnr(filename)
endif
endfunction
Here's a NERDTree extension that uses the last active buffer as a preview window, and either hijacks that window or splits it with the original content depending on if you open with o, s, i, gs, gi etc.
https://github.com/numEricL/nerdtree-live-preview

Automatically quit Vim if NERDTree and TagList are the last and only buffers

Basically, my .vimrc starts TagList and NERDTree when Vim is launched, as splits on the left and on the right of the normal file buffer.
I want to close Vim when, closing the last buffer/tab, TagList and NERDTree splits are the only remained. I'm already using vim-nerdtree-tabs and it works great when NERDTree is the only and last buffer open.
I'm aware that such topic has been discussed here on StackOverflow but I cannot find anything related to both NERDTree and TagList.
Thanks
let Tlist_Exit_OnlyWindow = 1
will close Tag_list window if it's the last window, look at http://vim-taglist.sourceforge.net/manual.html for more infomation about Tlist_Exit_OnlyWindow, I'm not sure if you are looking for this, if not, please delete my answer.
Something like... (untested)
fun! NoExcitingBuffersLeft()
if tabpagenr("$") == 1 && winnr("$") == 2
let window1 = bufname(winbufnr(1))
let window2 = bufname(winbufnr(2))
if (window1 == t:NERDTreeBufName || window1 == "__Tag_List__") &&
(window2 == t:NERDTreeBufName || window2 == "__Tag_List__")
quit
endif
endif
endfun
then tie that function to an autocommand...
au WinEnter * call NoExcitingBuffersLeft()<cr>
I don't use either of those plugins, so you may need to adjust the t:NERDTreeBufName and __Tag_List__.
Improving on Conner's idea, I've made a functional solution here.
" If only 2 windows left, NERDTree and Tag_List, close vim or current tab
fun! NoExcitingBuffersLeft()
if winnr("$") == 3
let w1 = bufname(winbufnr(1))
let w2 = bufname(winbufnr(2))
let w3 = bufname(winbufnr(3))
if (exists(":NERDTree")) && (w1 == "__Tag_List__" || w2 == "__Tag_List__" || w3 == "__Tag_List__")
if tabpagenr("$") == 1
exec 'qa'
else
exec 'tabclose'
endif
endif
endif
endfun
autocmd BufWinLeave * call NoExcitingBuffersLeft()
Need vim 7.0+ for the BufWinLeave event.
Closes the tab if more than one tab is open, otherwise quits vim.
This way, the auto-command is tied to when you close the last window that's not NERDTree or Tag_List, rather than upon entering one of the two windows.
This is nice extendable solution. To validate against other plugins/window types just add them to the regex check.
function! s:CloseAddons()
for w in range(1, winnr('$'))
if bufname(winbufnr(w)) !~# '__Tagbar\|NERD_tree_\|coc-explorer'
\ && getbufvar(winbufnr(w), "&buftype") !=? "quickfix"
return
endif
endfor
if tabpagenr('$') ==? 1
execute 'quitall'
else
execute 'tabclose'
endif
endfunction

Setting netrw like NERDTree

I used nmap <silent> <f2> :NERDTreeToggle<cr> to toggle nerdtree window. How can I do the same with netrw?
nerdtree window is not shown in the buffer list(:ls). netrw is listed in the buffer list. How can I make it not listed?
:bn command works but :bp command does not work in the netrw window. Is this a bug?
The 'Vexplore' command opens a vertical directory browser. You can build on this by adding the following code to your .vimrc file to toggle the vertical browser with Ctrl-E (for example):
" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
if exists("t:expl_buf_num")
let expl_win_num = bufwinnr(t:expl_buf_num)
if expl_win_num != -1
let cur_win_nr = winnr()
exec expl_win_num . 'wincmd w'
close
exec cur_win_nr . 'wincmd w'
unlet t:expl_buf_num
else
unlet t:expl_buf_num
endif
else
exec '1wincmd w'
Vexplore
let t:expl_buf_num = bufnr("%")
endif
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>
The code above tries to open the Explorer window on the left hand side of the screen at all times; I use it with multiple split vertical windows open.
[OPTIONAL] You might like to add the following lines to your .vimrc to improve the browsing experience:
" Hit enter in the file browser to open the selected
" file with :vsplit to the right of the browser.
let g:netrw_browse_split = 4
let g:netrw_altv = 1
" Change directory to the current buffer when opening files.
set autochdir
Starting with netrw v150, there's :Lexplore, which will toggle a netrw window on the left-hand side.
I just did some improvements on Nick's solution which fixes:
opens 100% high window (independent from window splits)
:Lexplore opens it on left side, :Lexplore! on the right
listing the directory of the current file (even on remote directories)
Put these lines to the end of your .vimrc:
com! -nargs=* -bar -bang -complete=dir Lexplore call netrw#Lexplore(<q-args>, <bang>0)
fun! Lexplore(dir, right)
if exists("t:netrw_lexbufnr")
" close down netrw explorer window
let lexwinnr = bufwinnr(t:netrw_lexbufnr)
if lexwinnr != -1
let curwin = winnr()
exe lexwinnr."wincmd w"
close
exe curwin."wincmd w"
endif
unlet t:netrw_lexbufnr
else
" open netrw explorer window in the dir of current file
" (even on remote files)
let path = substitute(exists("b:netrw_curdir")? b:netrw_curdir : expand("%:p"), '^\(.*[/\\]\)[^/\\]*$','\1','e')
exe (a:right? "botright" : "topleft")." vertical ".((g:netrw_winsize > 0)? (g:netrw_winsize*winwidth(0))/100 : -g:netrw_winsize) . " new"
if a:dir != ""
exe "Explore ".a:dir
else
exe "Explore ".path
endif
setlocal winfixwidth
let t:netrw_lexbufnr = bufnr("%")
endif
endfun
Suggested options to behave like NERDTree:
" absolute width of netrw window
let g:netrw_winsize = -28
" do not display info on the top of window
let g:netrw_banner = 0
" tree-view
let g:netrw_liststyle = 3
" sort is affecting only: directories on the top, files below
let g:netrw_sort_sequence = '[\/]$,*'
" use the previous window to open file
let g:netrw_browse_split = 4
Toggle function
Here is my version of toggle function, based on Nick's answer. Now you can use hotkey from any pane, not only from netrw's pane. In Nick's version it causes an error, also I did some code cleanup and remap it to Ctrl-O, because Ctrl-E is used by default to scroll down by one line.
" Toggle Vexplore with Ctrl-O
function! ToggleVExplorer()
if exists("t:expl_buf_num")
let expl_win_num = bufwinnr(t:expl_buf_num)
let cur_win_num = winnr()
if expl_win_num != -1
while expl_win_num != cur_win_num
exec "wincmd w"
let cur_win_num = winnr()
endwhile
close
endif
unlet t:expl_buf_num
else
Vexplore
let t:expl_buf_num = bufnr("%")
endif
endfunction
map <silent> <C-O> :call ToggleVExplorer()<CR>
Variable "t:expl_buf_num" is global for current tab, so you can have one Explorer per tab. You can change it to "w:expl_buf_num" if you want to be able to open Explorer in every window.
Keep focus in Explorer
Also I like to have this at my .vimrc:
" Open file, but keep focus in Explorer
autocmd filetype netrw nmap <c-a> <cr>:wincmd W<cr>
Actually,
let g:netrw_browse_split = 4
let g:netrw_altv = 1
works best for me.
*g:netrw_browse_split* when browsing, <cr> will open the file by:
=0: re-using the same window
=1: horizontally splitting the window first
=2: vertically splitting the window first
=3: open file in new tab
=4: act like "P" (ie. open previous window)
Note that |g:netrw_preview| may be used
to get vertical splitting instead of
horizontal splitting.
I think the best behavior is described by option 4. By pressing enter, file is opened on the other split, avoiding an overpopulation of splits.
" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
Lexplore
vertical resize 30
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>
Simplify
As a similar and simpler aprroach to Nick's, you could make it toggleable (and very NERDTree-like) with F9 with this in your .vimrc:
" ---------------------------------------------------------------
" File Explorer start
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 15
" Toggle Vexplore with F9
map <silent> <F9> :Lexplore<CR>
" File Explorer end
" ---------------------------------------------------------------

Switch to last-active tab in VIM

In Vim, is there a way to quickly toggle between the current tab and the last-active tab? Sort of the way '' toggles between the current line and the last-active line. Plugins / keyboard mappings / voodoo all acceptable.
Put this in your .vimrc:
if !exists('g:lasttab')
let g:lasttab = 1
endif
nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()
Then, in normal mode, type \tl to swap to the tab you viewed last.
Fix the potential issue when a tab is closed:
" Switch to last-active tab
if !exists('g:Lasttab')
let g:Lasttab = 1
let g:Lasttab_backup = 1
endif
autocmd! TabLeave * let g:Lasttab_backup = g:Lasttab | let g:Lasttab = tabpagenr()
autocmd! TabClosed * let g:Lasttab = g:Lasttab_backup
nmap <silent> <Leader>` :exe "tabn " . g:Lasttab<cr>
I use buffers and not tabs, but I am able to switch between the current and latest used buffer using :b#
Basics of using buffers are:
:e filename to open file in new buffer
:bn to go to next buffer
:bp to go to previous buffer
:bd to close current buffer
here's a solution in lua for folks that uses neovim also make sure to change <A-S-b> to your favorite keybinding.
-- switching to last active tab
vim.api.nvim_create_autocmd("TabLeave", {
pattern = "*",
callback = function()
vim.api.nvim_set_keymap('n', '<A-S-b>', '<cmd>tabn ' .. vim.api.nvim_tabpage_get_number(0) .. '<CR>', { noremap = true, silent = true })
end
})

Resources