像NERDTree一样设置netrw

时间:2011-02-15 17:11:23

标签: vim netrw

  1. 我使用nmap <silent> <f2> :NERDTreeToggle<cr>来切换nerdtree窗口。我怎么能和netrw一样呢?

  2. nerdtree窗口未显示在缓冲区列表(:ls)中。 netrw列在缓冲区列表中。如何才能将其列入?

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

7 个答案:

答案 0 :(得分:37)

'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

答案 1 :(得分:16)

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

答案 2 :(得分:9)

我刚刚对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

答案 3 :(得分:6)

实际上,

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键,文件在另一个分割中打开,避免分割过多。

答案 4 :(得分:3)

切换功能

这是根据尼克的回答,我的切换功能版本。现在,您不仅可以在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”。

在Explorer中保持焦点

我也喜欢在我的.vimrc中使用它:

" Open file, but keep focus in Explorer
autocmd filetype netrw nmap <c-a> <cr>:wincmd W<cr>

答案 5 :(得分:0)

File System -> User's Desktop

简化

答案 6 :(得分:0)

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

" ---------------------------------------------------------------