请选择 进入手机版 | 继续访问电脑版

技术控

    今日:227| 主题:57401
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Vim: you don't need NERDtree or (maybe) netrw

[复制链接]
蜜彩儿 发表于 2016-10-18 21:40:48
224 3
Table of contents

     
       

    •       
    • netrw - the unloved directory browser      
    • Changing the directory view in netrw      
    • Changing how files are opened      
    • Set the width of the directory explorer      
    • You may not need netrw     
       
     

Vim: you don't need NERDtree or (maybe) netrw

Vim: you don't need NERDtree or (maybe) netrw-1-技术控-horizontal,available,default,contents,achieved

  netrw - the unloved directory browser

   The netrw plugin normally ships with vim and is the default filebrowser. It gets a bad rap and ships with all kinds of features likes remote editing over SSH and FTP, and anecdotally has many bugs . Most developers just jump straight for the  NERDtree plugin  but in my opinion the default netrw plugin does most of what people use NERDtree for. Moreover, for opening files and traversing codebases there are other native vim options available.
  Invoking netrw

  Invoking netrw can be achieved in three ways
  
       
  • :Explore - opens netrw in the current window   
  • :Sexplore - opens netrw in a horizontal split   
  • :Vexplore - opens netrw in a horizontal split  
   You can also snigger by typing :Sex to invoke a horizontal split.
  Changing the directory view in netrw

  The directory listing view can be modified to show more or less information on files and directories, change the sorting order and hiding certain files.
   With the directory browser open hit i to cycle through the view types. There are four different view types: thin, long, wide and tree. A preferred view type can be made permanent by setting it in a .vimrc file.
  1. let g:netrw_liststyle = 3
复制代码
  

Vim: you don't need NERDtree or (maybe) netrw

Vim: you don't need NERDtree or (maybe) netrw-2-技术控-horizontal,available,default,contents,achieved

  The tree list view in netrw.
  Removing the banner

   The directory banner is mostly useless. To remove it temporarily press I . To remove it permanently add the following to your .vimrc .
  1. let g:netrw_banner = 0
复制代码
Changing how files are opened

   By default files will be opened in the same window as the netrw directory browser. To change this behaviour the netrw_browse_split option may be set. The options are as follows
  
       
  • 1 - open files in a new horizontal split   
  • 2 - open files in a new vertical split   
  • 3 - open files in a new tab  
   To make the selection permanent add the following to your .vimrc .
  1. let g:netrw_browse_split = 1
复制代码
Set the width of the directory explorer

   The width of the directory explorer can be fixed with the netrw_browse_split option. The following sets the width to 25% of the page.
  1. let g:netrw_winsize = 25
复制代码
NERDtree like setup

   If NERDtree is your thing netrw can give you a similar experience with the following settings
  1. let g:netrw_banner = 0
  2. let g:netrw_liststyle = 3
  3. let g:netrw_browse_split = 4
  4. let g:netrw_altv = 1
  5. let g:netrw_winsize = 25
  6. let g:netrw_banner = 0
  7. augroup ProjectDrawer
  8.   autocmd!
  9.   autocmd VimEnter * :Vexplore
  10. augroup END
复制代码
  

Vim: you don't need NERDtree or (maybe) netrw

Vim: you don't need NERDtree or (maybe) netrw-3-技术控-horizontal,available,default,contents,achieved

  A nerdtree like setup with netrw.
  My settings

  I keep things minimal and am happy with most of the defaults. In fact for now I just remove the banner. I told you I like minimal :-).
  1. let g:netrw_banner = 0
复制代码
vim-vinegar

   It is worth mentioning vim-vinegar , a plugin that looks to enhance netrw and is popular with users looking to avoid installing NERDtree.
  Well, vinegar.vim enhances netrw, partially in an attempt to mitigate the need for more disruptive “project drawer” style plugins.
  In trying to use less vim plugins so I’m not a user but horses for courses etc.
  You may not need netrw

   Vim also supports arbitrary commands to be run following ! . For a quick directory listing the following works.
  1. :! ls -lF
复制代码
  For a more complex command other commands like ack , grep or find can be used.
   There are a number of ways to open files in vim and if that is what you use netrw for. Using file within vim can open files and supports tab completion.
  1. :file path/to/file.txt
复制代码
To open a file in a vertical split use the following. This also supports tab completion.
  1. :vs path/to/file.txt
复制代码
To open a file in a horizontal split use the following. This also supports tab completion.
  1. :sp path/to/file.txt
复制代码
To open a file in a new tab use the following. This also supports tab completion.
  1. let g:netrw_banner = 00
复制代码
  For exploring codebases I also findusing ctags is a better than grepping or trying to guess where methods are defined. First you need to generate a tags file using the ctags command.
  1. let g:netrw_banner = 01
复制代码
  Once this file is created vim can take you to a function or method definition automatically with CTRL-[ . It is like magic and much more intelligent than random file browsing.
  Conclusion

   The directory browser that ships with vim is not particularly intuitive and ships with a wealth of features I will most likely never use. I get the sense that many developers just blindly install a shiny plugin without understanding what netrw can do. Sure, netrw is not perfect but less dependencies in my life and striving for simplicity is a good thing.
  Further reading

  
       
  • Why does NERDtree exist? What’s wrong with netrw?   
  • netrw documentation   
  • vim-vinegar   
  • nerdtree   
  • Vim and ctags   
  • Make netrw really behave like Nerdtree  
   Have an update or suggestion for this article? You can edit it here and send me a pull request.
  Tags



上一篇:Introducing VMware Workspace ONE Essentials
下一篇:iOS App组件化开发实践之需要思考的问题
槑92 发表于 2016-10-22 02:55:26
先顶再看!
回复 支持 反对

使用道具 举报

刘红艳 发表于 2016-11-7 11:01:10
轻轻的手滑
回复 支持 反对

使用道具 举报

AliciaMl 发表于 2016-11-21 17:56:14
土豪,我们做朋友吧。
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表