Keep netrw view after closed just like nerdtree in VIM - vim

Here's my netrw settings, I toggle netrw with <F2>.
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 = 20
set autochdir
" Toggle Vexplore with <F2>
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 <F2> :call ToggleVExplorer()<CR>
This is the first view I open netrw:
Then I do some operations in netrw just like this.
But when toggle happened, the operations I did disappear.
Can anyone who knows about netrw help me solve this problem? I have been confused for two months. SOS!
I hope when I open netrw again, I can see the view when I leave netrw. I know that if I don't close netrw it will not clean my operation when switch to other buffers, but sometimes I have to close it temporarily due to valuable screen space.

Related

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

How to open or close NERDTree and Tagbar with <leader>\?

I want <leader>\ to open or close NERDTree and Tagbar, under the following conditions:
Only close both if NERDTree and Tagbar are both opened
Open both if NERDTree and Tagbar are closed OR if one is already opened
So far, in VIMRC, I have:
nmap <leader>\ :NERDTreeToggle<CR> :TagbarToggle<CR>
Which doesn't exactly work, since if one is opened, and the other closed. It would open the one that was closed and close the one that was opened.
How can this be achieved?
You need to use a function that checks whether the plugin windows are open or not and then acts accordingly. This should work and will also jump back to the window that you started in:
function! ToggleNERDTreeAndTagbar()
let w:jumpbacktohere = 1
" Detect which plugins are open
if exists('t:NERDTreeBufName')
let nerdtree_open = bufwinnr(t:NERDTreeBufName) != -1
else
let nerdtree_open = 0
endif
let tagbar_open = bufwinnr('__Tagbar__') != -1
" Perform the appropriate action
if nerdtree_open && tagbar_open
NERDTreeClose
TagbarClose
elseif nerdtree_open
TagbarOpen
elseif tagbar_open
NERDTree
else
NERDTree
TagbarOpen
endif
" Jump back to the original window
for window in range(1, winnr('$'))
execute window . 'wincmd w'
if exists('w:jumpbacktohere')
unlet w:jumpbacktohere
break
endif
endfor
endfunction
nnoremap <leader>\ :call ToggleNERDTreeAndTagbar()<CR>
hmm... this works for me in vimrc
The toggle option checks if the window already exists, so no custom function needed (#JanLarres or one of the contributors) must have added it to TagBar :D
" NERDTree
map <leader>n :NERDTreeToggle<CR>
" TagBar
map <leader>t :TagbarToggle<CR>

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
" ---------------------------------------------------------------

Vim keep window position when switching buffers

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.

Resources