【问题标题】:Setting netrw like NERDTree像 NERDTree 一样设置 netrw
【发布时间】:2011-06-27 18:37:33
【问题描述】:
  1. 我使用nmap <silent> <f2> :NERDTreeToggle<cr> 来切换nerdtree 窗口。我怎样才能对 netrw 做同样的事情?

  2. nerdtree 窗口未显示在缓冲区列表中 (:ls)。 netrw 列在缓冲区列表中。我怎样才能让它不列出来?

  3. :bn 命令有效,但:bp 命令在 netrw 窗口中无效。这是一个错误吗?

【问题讨论】:

标签: vim netrw


【解决方案1】:

“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

【讨论】:

  • 太棒了!谢谢你。
  • 打开文件并切换(关闭)后,第 7 行出现错误:E16: Invalid range: 2wincmd w, Vim 8.0.4。任何想法如何解决?
【解决方案2】:

netrw v150 开始,有:Lexplore,它将在左侧切换netrw 窗口。

【讨论】:

  • 不错!比自定义函数好得多。
  • :Lexplore 默认占用当前缓冲区大小的一半。要仅打开 30 个字符大小的缓冲区,您可以在我的 .vimrc 中映射 Llcommand! Ll Lexplore | vert res 30
【解决方案3】:

我刚刚对Nick's solution 做了一些改进,修复了:

  • 打开 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

【讨论】:

  • 打开文件后,切换会出现错误:line 8: E16: Invalid range: 2wincmd w。事实上,当资源管理器不是活动窗口时,这会在切换时出现错误。 VIM8 可能有问题?
  • 如果您想要基于文件类型的突出显示,请检查gitlab.com/bimlas/vim-high
【解决方案4】:

切换功能

这是我的切换功能版本,基于尼克的回答。现在您可以从任何窗格使用热键,而不仅仅是从 netrw 的窗格。在 Nick 的版本中它会导致错误,我也做了一些代码清理并将其重新映射到 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>

【讨论】:

  • 很好的解决方案!在呈现 NETRW 窗格之前,您是否注意到文件内容的闪烁?你知道可能是什么吗?再次感谢!
  • @betoharres 这可能是由于 Vim 的铃声。这是停用它的方法(将其设置为“视觉”然后告诉它什么都不做)set visualbellset t_vb=
  • 我喜欢你的功能,用了一段时间!但是,您不想重新映射 ,这与 (我经常使用的一对映射)相反。最后,在任何文件类型中重新映射 ,尝试使用 that
【解决方案5】:

其实

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 描述了最佳行为。按回车键,文件在另一个拆分上打开,避免拆分过多。

【讨论】:

  • 是的 - 我现在自己做,并相应地更新了我的答案。
【解决方案6】:
" Toggle Vexplore with Ctrl-E

function! ToggleVExplorer()
      Lexplore
      vertical resize 30
endfunction
map <silent> <C-E> :call ToggleVExplorer()<CR>

简化

【讨论】:

    【解决方案7】:

    作为与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
    
    " ---------------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多