我曾经
nmap <silent> <f2> :NERDTreeToggle<cr>
切换 nerdtree 窗口。我怎样才能对 netrw 做同样的事情?nerdtree 窗口未显示在缓冲区列表(
:ls
)中。netrw 列在缓冲区列表中。我怎样才能让它不上市?:bn
命令有效,但:bp
命令在 netrw 窗口中不起作用。这是一个错误吗?
7 回答
'Vexplore' 命令打开一个垂直目录浏览器。您可以在此基础上添加以下代码到您的 .vimrc 文件中,以使用 Ctrl-E 切换垂直浏览器(例如):
" 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>
上面的代码总是试图打开屏幕左侧的资源管理器窗口;我在打开多个拆分垂直窗口的情况下使用它。
[可选] 您可能希望在 .vimrc 中添加以下行以改善浏览体验:
" 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
从netrw
v150 开始,有:Lexplore
,它将netrw
在左侧切换一个窗口。
我刚刚对尼克的解决方案做了一些改进,修复了:
- 打开 100% 高窗口(独立于窗口拆分)
:Lexplore
左侧打开,:Lexplore!
右侧打开- 列出当前文件的目录(甚至在远程目录上)
将这些行放在 .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
表现得像 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
切换功能
这是我的切换功能版本,基于尼克的回答。现在您可以从任何窗格中使用热键,而不仅仅是从 netrw 的窗格中。在尼克的版本中,它会导致错误,我也做了一些代码清理并将其重新映射到 Ctrl-O,因为默认情况下使用 Ctrl-E 向下滚动一行。
" 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>
变量“t:expl_buf_num”对于当前选项卡是全局的,因此每个选项卡可以有一个资源管理器。如果您希望能够在每个窗口中打开资源管理器,可以将其更改为“w:expl_buf_num”。
在资源管理器中保持焦点
我也喜欢在我的 .vimrc 中有这个:
" Open file, but keep focus in Explorer
autocmd filetype netrw nmap <c-a> <cr>:wincmd W<cr>
实际上,
let g:netrw_browse_split = 4
let g:netrw_altv = 1
最适合我。
*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.
我认为选项 4 描述了最佳行为。通过按 enter,文件在另一个拆分上打开,避免拆分过多。
" Toggle Vexplore with Ctrl-E
function! ToggleVExplorer()
Lexplore
vertical resize 30
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>
简化
作为与Nick's类似且更简单的方法,您可以在 .vimrc 中使用 F9 使其可切换(并且非常类似于 NERDTree):
" ---------------------------------------------------------------
" 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
" ---------------------------------------------------------------