在vim中打开目录

发布于 2024-11-05 15:39:03 字数 347 浏览 2 评论 0原文

我是一名 mac 用户,正在认真尝试 vim。我习惯的大多数 GUI 编辑器都允许我通过执行以下命令来将目录作为“项目”打开:

编辑 ~/www/example.com/

vim 等效的 vim ~/www/example.com/ 将显示目录中的文件列表,我可以打开它们。但它没有将 vim 的工作目录设置为该路径,我必须运行 :cd . 来设置工作目录。

有没有某种方法,也许使用 shell 脚本,打开 vim 并将其工作目录设置为给定路径?

我实际上正在使用 MacVim,如果这有什么区别的话。

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:

edit ~/www/example.com/

The vim equivalent vim ~/www/example.com/ will show me a list of files in the directory, and I can open them. But it does not set vim's working directory to that path, I have to run :cd . to set the working directory.

Is there some way, perhaps with a shell script, to open vim and have it's working directory set to a given path?

I'm actually using MacVim, if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心的位置 2024-11-12 15:39:03
$ cd ~/my/working/directory
$ vim .
$ cd ~/my/working/directory
$ vim .
々眼睛长脚气 2024-11-12 15:39:03

感谢@sehe的建议,我想出了这个。不确定这是否是最好的解决方案,但它似乎有效。

#!/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

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
谎言月老 2024-11-12 15:39:03

Braindead:

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

不那么重要:

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

你总是可以将 :cd %:h 映射到一个方便的键或放入一个自动命令(我实际上不会推荐后者,但对于品味是没有争议的)


哦,还有对于目录而不是文件:

:cd %

就足够了

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

ペ泪落弦音 2024-11-12 15:39:03

尝试将以下内容添加到您的 .vimrc

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

这将使目录浏览使用树形样式来显示文件(您可以通过将光标放在目录上并按 Enter 来展开目录)并使当前工作目录也成为您的目录正在浏览。

您可能还对 NERDTree 插件感兴趣,该插件提供了比内置浏览器更高级的目录浏览器。它有一个选项

let g:NERDTreeChDirMode=2

可以使当前目录与显示的树的根匹配,或者

let g:NERDTreeChDirMode=1

在您使用命令(:e:NERDTree)浏览树时更改目录。新目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文