Vim 文件导航

发布于 2024-08-04 17:28:10 字数 613 浏览 4 评论 0 原文

在使用 TextMate 几年后,我非常努力地学习 vim。

我已经开始将一些文件内导航提交到内存中,但我在多个文件之间导航时遇到了困难。

在我的工作流程中,我经常在几个文件之间切换(文件足够多,以至于分割窗格窗口变得太小),这是很常见的。

我目前正在使用 NERDTree,但发现深入目录很麻烦,并且经常使用 CTRL+W h/CTRL+W l 来回跳跃。

我想我会用可以轻松切换的选项卡做得更好,但也许我需要使用不同的工作流程。

我还想要一个“转到文件...”快捷方式,例如 TextMate 中的 CMD+T。我发现 fuzzy_file_finder 但它需要使用 Ruby 绑定构建 vim我所处理的本机安装并非如此。

虽然我可以重建我想要切换到 vim 的主要原因是这样我就可以拥有一个我知道可以轻松跨任何平台工作的编辑器环境。

I'm trying really hard to learn vim after using TextMate for the last few years.

I've started to commit some of the in-file navigation to memory but I'm struggling with navigating between multiple files.

In my workflow it is pretty common that I'm flipping between a handful of files pretty regularly (enough files such that split-pane windows become too small).

I'm currently using NERDTree but find drilling down into directories cumbersome as well as constantly using CTRL+W h/CTRL+W l to hop back and forth.

I think I would do better with tabs I can easily toggle between but maybe I need to use a different workflow.

I'd also like a "Go to File..." shortcut like CMD+T in TextMate. I've found fuzzy_file_finder but it requires vim to be built with Ruby bindings which isn't the case the native installs I've worked on.

While I could rebuild the main reason I want to switch to vim is so I can have one editor environment that I know will easily work across any platform.

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

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

发布评论

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

评论(17

葬花如无物 2024-08-11 17:28:11

:Sex:Vex:Tex:Ex 都是用于 ex(探索)文件的有用命令如果您想使用与 :e 不同的内容

(其中 S/V/T 是 Split/Vertical/Tab 的缩写),请在您的系统上使用

:Sex,:Vex, :Tex and :Ex are all useful commands for ex(ploring) the files on your system if you want to use something different from :e

(where S/V/T are short for Split/Vertical/Tab)

莫言歌 2024-08-11 17:28:11

到目前为止,我发现的文件导航最好最快的插件是 fzf.vim。您可以非常快速地模糊搜索所有文件、打开的缓冲区甚至文件内容。

由于我们有一个非常大的代码库,我指定了几个我最常使用的目录作为 fzf 搜索的目录。 Fzf 甚至拥有丰富的 git 集成。因此您只能搜索跟踪的文件。

这是文件搜索的样子:
输入图像描述这里

是内容搜索的样子:
输入图像描述这里

By far the best and fastest plugin I found for file navigation is fzf.vim. You can very quickly fuzzy search all your files, the open buffers and even the files contents.

Since we have a very large codebase at work I specified a couple of directories I use most as the directories fzf searches. Fzf even has a rich git integration. So you can search only tracked files.

This is how the file search looks:
enter image description here

and his is how the content search looks:
enter image description here

月光色 2024-08-11 17:28:11

如果您还没有找到它们,您可能需要查看:

  • 原始的 fuzzy finder 插件 - IIRC 没有 Ruby 绑定问题
  • Project 插件 - 类似于 NERDTree
  • 缓冲区资源管理器插件 - 显示打开的缓冲区列表

另外请记住,您可以在您的.vimrc 使它们不那么麻烦。我确实经常使用分割窗口;我发现以下几点可以让与他们打交道变得更加容易:

" set your own personal modifier key to something handy
let mapleader = "," 

" use ,v to make a new vertical split, ,s for horiz, ,x to close a split
noremap <leader>v <c-w>v<c-w>l
noremap <leader>s <c-w>s<c-w>j
noremap <leader>x <c-w>c

" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h

If you haven't found them already, you might want to check out:

  • the original fuzzy finder plugin -- which IIRC doesn't have the Ruby binding issues
  • the Project plugin -- similar to NERDTree
  • the buffer explorer plugin -- shows a list of open buffers

Also bear in mind that you can remap key shortcuts in your .vimrc to make them less cumbersome. I do use split windows a lot; I've found the following make dealing with them much easier:

" set your own personal modifier key to something handy
let mapleader = "," 

" use ,v to make a new vertical split, ,s for horiz, ,x to close a split
noremap <leader>v <c-w>v<c-w>l
noremap <leader>s <c-w>s<c-w>j
noremap <leader>x <c-w>c

" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h
狼亦尘 2024-08-11 17:28:11

令人惊讶的是,任何答案中都没有提到 :find 命令。我通常处理需要深入了解目录层次结构的 Rails 项目。因此,对于位于 app/assets/stylesheets/ 中的文件 application.css,我只需输入 :find applica,然后按 < strong>tab 自动完成,回车 打开。

It is surprising to see that :find command has not been mentioned in any of the answers. I usually work with rails projects where I need to drill down deep into the directory hierarchy. So for a file application.css, located in app/assets/stylesheets/ all I have to type is :find applica and then press tab to autocomplete and enter to open.

ι不睡觉的鱼゛ 2024-08-11 17:28:11

我想我用标签会做得更好
可以很容易地切换,但也许我
需要使用不同的工作流程。

你知道 Vim 的选项卡吗?不确定您是指 Vim 自己的选项卡,还是渴望 TextMate 的选项卡。目前还不清楚你使用的是哪个 Vim 版本,但至少从 7.0 开始它就有了选项卡(我最近看到的所有地方似乎都安装了这些选项卡),而且它们非常棒。

:tabe 打开一个新的(“选项卡编辑”的缩写,因此例如 :tabe 表示空或 :tabe path/to/file 表示在新选项卡中打开文件,)您可以使用 gt/gT 在相邻选项卡之间移动,并且还有一堆其他与选项卡相关的导航命令,您可能会这样来自 Vim 的期望 (:help tabs)

我的大型项目工作流程往往涉及一堆选项卡,每个选项卡有 1 到 3 个窗口,具体取决于我需要的上下文类型。如果我有一个双宽终端窗口,效果会更好,因为我可以使用 :vs 垂直分割,这样一个选项卡就可以轻松地同时显示 4 个文件,每个文件都有足够的空间。

最后提示:我有时会使用“switch buffer”命令(:sb ),这有点像 TM 的模糊查找,因为它工作得很好,但仅适用于已经打开的缓冲区。当我同时打开十几个或更多文件时仍然很棒。 (注意,我需要在 vimrc 中 :set switchbuf=usetab 才能跨选项卡工作,但一旦设置就很好了。)

I think I would do better with tabs I
can easily toggle between, but maybe I
need to use a different workflow.

Are you aware of Vim's tabs? Not sure if you were referring to Vim's own tabs there, or pining for TextMate's. It's unclear what Vim version you're using but it's had tabs since at least 7.0 (which seems to be installed everywhere I look lately), and they're awesome.

:tabe opens a new one (short for "tab edit", so e.g. :tabe for empty or :tabe path/to/file for opening a file in a new tab,) you can move between adjacent tabs with gt/gT, and there's a bunch of other tab-related nav commands too, as you might expect from Vim (:help tabs)

My workflow for large projects tends to involve a bunch of tabs, each with between 1 and 3 windows depending on what sort of context I need. This works even better if I have a doublewide terminal window since I can vertically split with :vs, so then a single tab can easily show me 4 files at once with plenty of room for each.

Final tip: I sometimes use the "switch buffer" command (:sb <partial_buf_name>) which is sorta-kinda like TM's fuzzy finding, in that it works pretty well but only for already-open buffers. Still great for when I have a dozen or more files open at once. (N.B. I needed to :set switchbuf=usetab in my vimrc for this to work across tabs, but once that's set it's great.)

深爱成瘾 2024-08-11 17:28:11

我发现 LustyExplorer 是迄今为止最好的。

您可以通过仅键入文件名的一部分来打开文件。

您可以从当前文件的目录、当前pwd 或缓冲区列表中打开文件。

如果从缓冲区列表中打开文件,则不必浏览目录树,但不幸的是,对于其他两种模式,您仍然必须浏览目录树。好处是你不必从第一个角色开始。

如果您有:

dir_a/
    ...
dir_b/
    file.js
    file.html

并且您想打开 dir_b/file.html 您通常可以通过输入 bh 找到它

I find LustyExplorer the best so far.

You can open a file by typing only a part of its name.

You can open a file from the directory of the current file, or the current pwd, or from the buffer list.

If you open a file from the buffer list, you don't have to navigate through the directory tree, though unfortunately for the other two modes, you still have to navigate through the tree. The good thing is you don't have to start from the first character.

If you have:

dir_a/
    ...
dir_b/
    file.js
    file.html

and you want to open dir_b/file.html you can generally find it by typing b<Tab>h

反目相谮 2024-08-11 17:28:11

如果您想要的文件已经加载到缓冲区中(如果您经常在几个文件之间切换,则可能会出现这种情况),那么您可以使用 :b[uffer] 命令快速切换到缓冲区。

:b 可以将缓冲区编号作为参数,或者(更有用)与路径/文件名的部分匹配的字符串。如果有多个匹配项,您可以使用 Tab 循环浏览它们。

如果你想分割窗口并在新窗口中打开缓冲区,请使用 :sb name

如果你想在新选项卡中打开缓冲区,请使用 :tab b name >

您还可以使用 :ls 命令查看当前加载的缓冲区列表。

If the file that you want is already loaded into a buffer (which is likely if you are flipping between a handful of files regularly) then you can quickly switch to the buffer with the :b[uffer] command.

:b can either take a buffer number as a parameter, or (more usefully) a string that matches part of the path/filename. If there are multiple matches you can use tab to cycle through them.

If you want to split the window and open the buffer in the new window then use :sb name

If you want to open the buffer in a new tab then use :tab b name

You can also use the :ls command to see a list of currently loaded buffers.

紫瑟鸿黎 2024-08-11 17:28:11

查看插件 ctrlp 作为插件 Command-T。它比这更好,因为它是一个“原生”Vim 脚本,不需要使用 ruby​​(或其他支持)编译 Vim,同时运行速度非常快且不引人注目。

Check out the plugin ctrlp as an alternative to the plugin Command-T. It's better than this in that it is a 'native' Vim script and doesnt need Vim to be compiled with ruby (or other support) while it runs very fast and unobtrusive.

山有枢 2024-08-11 17:28:11

我经常这样做:

:e ./

在正常模式下查看当前文件夹的内容,然后导航到任何其他文件夹或文件。

I often do:

:e ./

in NORMAL MODE to see the content of the current folder and then navigate to any other folder or file.

美胚控场 2024-08-11 17:28:11

NerdTree 最适合在 vim 中进行文件导航。

下面的链接是对 nerdtree 的很好的介绍。

http://www.catonmat.net/blog/vim-plugins-nerdtree-维姆/

NerdTree is best for file navigation in vim.

The link below is a good introduction to nerdtree.

http://www.catonmat.net/blog/vim-plugins-nerdtree-vim/

坚持沉默 2024-08-11 17:28:11

我使用这个函数来查找子目录中的文件,但我没有编写它。它来自 vim wiki

function! Find(name) 
    let l:list=system("find . -name '".a:name."' | grep -v \".svn/\" | perl -ne 'print \"$.\\t$_\"'") 
    let l:num=strlen(substitute(l:list, "[^\n]", "", "g")) 
    if l:num 0 
            echo "Not a number" 
            return 
        endif 
        if l:inputl:num 
            echo "Out of range" 
            return 
        endif 
        let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*") 
    else 
      let l:line=l:list 
    endif 
    let l:line=substitute(l:line, "^[^\t]*\t./", "", "") 
    execute ":e ".l:line 
endfunction 

command! -nargs=1 Find :call Find("") 

我喜欢的一件事是它支持通配符 (*)。它在多场比赛中也表现良好。

I use this function for finding files in subdirectories, I didn't write it though. It's from the vim wiki:

function! Find(name) 
    let l:list=system("find . -name '".a:name."' | grep -v \".svn/\" | perl -ne 'print \"$.\\t$_\"'") 
    let l:num=strlen(substitute(l:list, "[^\n]", "", "g")) 
    if l:num 0 
            echo "Not a number" 
            return 
        endif 
        if l:inputl:num 
            echo "Out of range" 
            return 
        endif 
        let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*") 
    else 
      let l:line=l:list 
    endif 
    let l:line=substitute(l:line, "^[^\t]*\t./", "", "") 
    execute ":e ".l:line 
endfunction 

command! -nargs=1 Find :call Find("") 

One thing I like is it has support for wildcards (*). It's also well behaved for multiple matches.

爱她像谁 2024-08-11 17:28:11

除了 wildmenu 答案之外,我还使用 BufExplorer< /a> 插件,以及以下映射来快速跳转缓冲区:

nmap <A-1> :b 1<CR>
nmap <A-2> :b 2<CR>
nmap <A-3> :b 3<CR>
...
nmap <A-0> :b 10<CR>
nmap <A-=> \bs

或者您可以使用 :ls 命令替换缓冲区浏览器,它基本上显示相同的内容。

最后一个映射 Alt-= 以显示缓冲区资源管理器。

我不太使用 Windows,因为我通常只有一两个窗口。

我还修改了 statusline 以包含缓冲区编号 %2n,以便我始终知道哪个缓冲区正在被编辑。请参阅 :h 状态行

In addition to the wildmenu answers, I use BufExplorer plugin, and the following mappings to quickly jump buffers:

nmap <A-1> :b 1<CR>
nmap <A-2> :b 2<CR>
nmap <A-3> :b 3<CR>
...
nmap <A-0> :b 10<CR>
nmap <A-=> \bs

Or you can just replace the buffer explorer with the :ls command, which basically display the same thing.

The last one maps the Alt-= to show the buffer explorer.

I don't use Windows that much, as I generally have one or two windows only.

I also modified the statusline to include the buffer number %2n, so that the I always know which buffer is being edited. See :h statusline

水溶 2024-08-11 17:28:11

你知道 MacVim 应用程序吗?它针对 OS X 进行了 VIM 优化,其功能之一是支持选项卡(CMD+T 打开新选项卡),它有抽屉等...(请阅读:http://zerokspot.com/weblog/2008/08/03/macvim-as-textmate-replacement/

尝试一下!

问候
米哈尔

do you know MacVim application? It is VIM polished for OS X, and one of its feature is support for tabs (CMD+T to open new tab), it have drawer, etc... (read this: http://zerokspot.com/weblog/2008/08/03/macvim-as-textmate-replacement/)

Try it!

Regards
Michal

惟欲睡 2024-08-11 17:28:11

这是我放在 .vimrc 中的一些相当琐碎的东西。它将从任何子目录加载文件。

" load from an unspecified subdirectory 
function! LoadFromSubdirectory(filespec) 
  let filename = glob("`find . -name '" . a:filespec . "' -print`") 
  exe ':e '.filename 
endfunction 
:command -nargs=1 Er :call LoadFromSubdirectory(<f-args>) 

所以我可以说:

:Er Main.java

它将搜索子目录,找到 Main.java 并加载它。对于同名的多个文件来说,它的表现不佳,但它快速且简单。

我使用的另一个工具是 VTreeExplorer,它提供了基于树的文件/目录窗口,并且不需要 VIM 的任何特殊构建选项(即它是一个普通插件)。

Here's something fairly trivial that I stick in my .vimrc. It will load a file from any subdirectory.

" load from an unspecified subdirectory 
function! LoadFromSubdirectory(filespec) 
  let filename = glob("`find . -name '" . a:filespec . "' -print`") 
  exe ':e '.filename 
endfunction 
:command -nargs=1 Er :call LoadFromSubdirectory(<f-args>) 

So I can just say:

:Er Main.java

and it will search subdirectories, find Main.java and load it. It's not well behaved for multiple files of the same name, but it's quick and simple.

The other tool I use is VTreeExplorer, which provides a tree-based window of files/directories, and doesn't require any special build options from VIM (i.e. it's a vanilla plugin).

酷遇一生 2024-08-11 17:28:11

我查找文件的工作流程是 wildmenu、autocwd 和 :e

.vimrc 中的相关部分:

set wildmenu
set wildignore=*.dll,*.o,*.pyc,*.bak,*.exe,*.jpg,*.jpeg,*.png,*.gif
set wildmode=list:full
set autochdir
let g:netrw_list_hide='^\.,.\(pyc\|pyo\|o\)

以及如何使用它:

:e myf^D

给出当前目录中以 myf 开头的所有文件。您还可以 通过它们。使用 return 打开:)

^D 将始终为您提供所有匹配项。因为 autocmd 总是转到当前文件的文件夹,所以很容易使用。如果您在 foo/bar.py 中并且想要转到 foo/baz.py,您只需执行 :e baz.py 并且你在那儿。这也适用于缓冲区(:b foo^D 列出以 foo 开头的所有缓冲区)

以及如何使用它:


给出当前目录中以 myf 开头的所有文件。您还可以 通过它们。使用 return 打开:)

^D 将始终为您提供所有匹配项。因为 autocmd 总是转到当前文件的文件夹,所以很容易使用。如果您在 foo/bar.py 中并且想要转到 foo/baz.py,您只需执行 :e baz.py 并且你在那儿。这也适用于缓冲区(:b foo^D 列出以 foo 开头的所有缓冲区)

My workflow for finding files is the wildmenu, autocwd and :e.

Relevant parts in the .vimrc:

set wildmenu
set wildignore=*.dll,*.o,*.pyc,*.bak,*.exe,*.jpg,*.jpeg,*.png,*.gif
set wildmode=list:full
set autochdir
let g:netrw_list_hide='^\.,.\(pyc\|pyo\|o\)

And how to use it:

:e myf^D

That gives all the files in the current directory that start with myf. You can also <Tab> through them. Use return to open :)

^D will always give you all the matches. Because autocmd always goes to the folder of the current file it's easy to work with. If you are in foo/bar.py and you want to go to foo/baz.py you only do :e baz.py and you're there. That also works with buffers (:b foo^D lists all buffers starting with foo)

And how to use it:


That gives all the files in the current directory that start with myf. You can also <Tab> through them. Use return to open :)

^D will always give you all the matches. Because autocmd always goes to the folder of the current file it's easy to work with. If you are in foo/bar.py and you want to go to foo/baz.py you only do :e baz.py and you're there. That also works with buffers (:b foo^D lists all buffers starting with foo)

弄潮 2024-08-11 17:28:10

浏览文件系统的一个简单方法是使用命令:

:Sex

这不是我编造的:)

An easy way to browse the file system is the command:

:Sex

I'm not making this up :)

自找没趣 2024-08-11 17:28:10

我不认为通过普通的旧 :e 深入到子目录会很麻烦,因为给定了一个不错的制表符完成配置。

查看 'wildmenu' 选项,让 Vim 在命令行上方的模型行中显示完成列表(文件名)。您可以更改 'wildmode' 选项来进一步配置 Vim 的制表符补全类型。

我个人使用 :set wildmode=full

我的工作流程是这样的:

  1. :cd 进入我的项目的顶级目录。
  2. 打开文件foo/bar/baz

    • 最简单的场景:输入 :e fbb

    • 如果其中一个目录中有多个以 b 开头的文件,您可能需要执行 < ;right> 或键盘上的另一个 在它们之间跳转(或者再输入几个字母来消除歧义)。

    • 最坏的情况是存在共享名称的文件和目录,并且您需要深入了解该目录。在本例中,按制表符补全目录名称,然后输入 * 进行向下钻取。

  3. 打开 2 或 3 个窗口并根据需要在所有窗口中打开文件。
  4. 一旦文件在缓冲区中打开,就不要终止缓冲区。当您打开新文件时,使其在后台保持打开状态。只需 :e 在同一窗口中创建一个新文件。
  5. 然后,使用 :b 循环浏览已在后台打开的缓冲区。如果您输入 :b foo ,它将仅匹配当前打开的与 foo 匹配的文件。

我还使用这些映射来更轻松地打开新窗口并在它们之间跳转,因为这是我经常做的事情。

" Window movements; I do this often enough to warrant using up M-arrows on this"
nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>

" Open window below instead of above"
nnoremap <C-W>N :let sb=&sb<BAR>set sb<BAR>new<BAR>let &sb=sb<CR>

" Vertical equivalent of C-w-n and C-w-N"
nnoremap <C-w>v :vnew<CR>
nnoremap <C-w>V :let spr=&spr<BAR>set nospr<BAR>vnew<BAR>let &spr=spr<CR>

" I open new windows to warrant using up C-M-arrows on this"
nmap <C-M-Up> <C-w>n
nmap <C-M-Down> <C-w>N
nmap <C-M-Right> <C-w>v
nmap <C-M-Left> <C-w>V

我只需要几秒钟的时间就可以打开 Vim,设置一些窗口并在其中打开一些文件。就我个人而言,我从未发现任何第三方文件浏览脚本非常有用。

I don't find drilling down into subdirectories via plain old :e to be that cumbersome given a decent configuration for tab-completion.

Look into the 'wildmenu' option to have Vim show a list of completions (filenames) in the modeline above the commandline. You can change the 'wildmode' option to further configure the kind of tab-completion Vim will do.

Personally I use :set wildmode=full.

My workflow is like this:

  1. :cd into the toplevel directory of my project.
  2. To open file foo/bar/baz:

    • Simplest scenario: type :e f<tab>b<tab>b<tab><enter>.

    • If there are more than one file starting with b in one of those directories you might have to do a <left> or <right> or another <tab> on the keyboard to jump between them (or type a few more letters to disambiguate).

    • Worst-case scenario there are files and directories that share a name and you need to drill down into the directory. In this case tab-complete the directory name and then type *<tab> to drill down.

  3. Open 2 or 3 windows and open files in all of them as needed.
  4. Once a file is open in a buffer, don't kill the buffer. Leave it open in the background when you open new files. Just :e a new file in the same window.
  5. Then, use :b <tab> to cycle through buffers that are already open in the background. If you type :b foo<tab> it will match only against currently-open files that match foo.

I also use these mappings to make it easier to open new windows and to jump between them because it's something I do so often.

" Window movements; I do this often enough to warrant using up M-arrows on this"
nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>

" Open window below instead of above"
nnoremap <C-W>N :let sb=&sb<BAR>set sb<BAR>new<BAR>let &sb=sb<CR>

" Vertical equivalent of C-w-n and C-w-N"
nnoremap <C-w>v :vnew<CR>
nnoremap <C-w>V :let spr=&spr<BAR>set nospr<BAR>vnew<BAR>let &spr=spr<CR>

" I open new windows to warrant using up C-M-arrows on this"
nmap <C-M-Up> <C-w>n
nmap <C-M-Down> <C-w>N
nmap <C-M-Right> <C-w>v
nmap <C-M-Left> <C-w>V

It takes me a matter of seconds to open Vim, set up some windows and open a few files in them. Personally I have never found any of the third-party file-browsing scripts to be very useful.

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