Opening a directory in vim

前端 未结 5 2056
清酒与你
清酒与你 2021-02-12 12:22

I\'m a mac user giving vim a serious try. Most of the GUI editors I\'m used to allow me to open a directory as a \"project\" by executing a command like:

相关标签:
5条回答
  • 2021-02-12 12:54

    Try adding the following to your .vimrc

    let g:netrw_liststyle=3
    let g:netrw_keepdir=0
    

    This will make the directory browsing use a tree style for showing the files (you can expand a directory by putting the cursor on a directory and hitting enter) and make the current working directory also be the one you are browsing.

    You might also be interested in the NERDTree plugin that provides a directory browser that is more advanced than the built in one. It has an option

    let g:NERDTreeChDirMode=2
    

    to make the current directory match the root of the displayed tree or

    let g:NERDTreeChDirMode=1
    

    to change the directory whenever you use a command (:e or :NERDTree) to browse a new directory.

    0 讨论(0)
  • 2021-02-12 12:55

    Braindead:

     (cd /path/to/dir && vim file)
    

    Less so:

     vim /path/to/dir/file +':cd %:h'
    

    You can always map :cd %:h to a convenient key or put in an autocommand (I wouldn't actually recommend the latter, but there is no arguing about taste)


    Oh and for directories instead of files:

    :cd %
    

    is quite enough

    0 讨论(0)
  • 2021-02-12 13:10
    $ cd ~/my/working/directory
    $ vim .
    
    0 讨论(0)
  • 2021-02-12 13:12

    Thanks to @sehe's suggestions, I came up with this. Not sure if it's the best solution, but it seems to work.

    #!/bin/bash
    
    if [ "$#" -eq 1 ];then # is there a path argument?
      if test -d $1;then # open directory in vim
        vim $1 +':cd %'
      else # open file in vim
        vim $1 +':cd %:h'
      fi
    else # no path argument, just open vim
      vim 
    fi
    
    0 讨论(0)
  • 2021-02-12 13:12

    Would this help?

    set autochdir
    

    I found it http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

    0 讨论(0)
提交回复
热议问题