vim,使用 vanilla Vim 快速切换文件(无插件) [英] vim, switching between files rapidly using vanilla Vim (no plugins)

查看:21
本文介绍了vim,使用 vanilla Vim 快速切换文件(无插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道将自己限制在 vanilla Vim(不使用插件)会限制编辑器的功能,但是由于我经常在不同的机器之间切换,所以到处移动我的环境通常会很麻烦.我只想留在原版 Vim.

I understand that limiting myself to vanilla Vim (not using plugins) limits the power of the editor, but as I switch between different machines frequently, it is often too much trouble to move my environment around everywhere. I want to just stay in vanilla Vim.

让我望而却步的是在文件之间快速切换的能力.我(至少相信)对 bufferswindowstabs 以及 netrw(VexEx 等).

Something that holds me back is the ability to quickly switch between files. I (believe at least) have a good understanding of buffers, windows, tabs, as well as netrw (Vex, Ex, etc).

但是在诸如 Sublime Text 之类的编辑器中,我只需键入 ctrl-p,我立即在文件中.

But in an editor such as Sublime Text, I can just type ctrl-p and instantly I am at the file.

我知道我可以下拉到 shell,但我想知道是否还有其他隐藏的"秘密可以根据文件名在 Vim 中的文件之间快速切换.

I know that I can drop down to the shell, but I wonder if there are any other "hidden" secrets to rapidly switching between files in Vim based off more than just the filename.

推荐答案

与 ST2 的 Ctrl+P 最接近的等价物(closest",而不是exact")是一个名为的插件,准备好... CtrlP.还有其他类似的插件,如 Command-TFuzzyFinder.

The closest equivalent ("closest", not "exact") to ST2's Ctrl+P is a plugin called, get ready… CtrlP. There are other similar plugins like Command-T or FuzzyFinder.

我使用 CtrlP 并且我喜欢它,但我全心全意地支持您决定无插件".这不是最简单的方法,但从长远来看会有所回报.

I use CtrlP and I love it but I wholeheartedly support your decision to go "plugin-free". It's not the easiest way to go but it will pay off in the long run.

打开文件

打开文件的最基本方法是:e/path/to/filename.值得庆幸的是,您可以获得制表符补全和通配符:经典的 * 和特殊的 **,它代表任何子目录".

The most basic way to open a file is :e /path/to/filename. Thankfully, you get tab-completion and wildcards: the classic * and a special one, **, which stands for "any subdirectory".

结合所有这些,您可以:

Combining all of that, you can do:

:e **/*foo<Tab>

从工作目录下名称中包含 foo 的所有文件中进行选择,或者:

to choose from all the files containing foo in their name under the working directory or:

:e **/*foo/*bar<Tab>

从名称中包含 bar 的所有文件中进行选择,该文件位于名称中包含 foo 的任何子目录下,位于工作目录下的任何位置.

to choose from all the files containing bar in their name under any subdirectory containing foo in its name, anywhere under the working directory.

当然,这也适用于 :tabe[dit]:sp[lit]:vs[plit].

Of course, that works for :tabe[dit], :sp[lit] and :vs[plit], too.

不过,这些命令仅限于一个文件.使用 :next 打开多个文件:

Those commands are limited to one file, though. Use :next to open multiple files:

:next **/*.js

看看:help arglist.

在缓冲区之间跳转

:b[uffer] 是基本的缓冲区切换命令:

:b[uffer] is the basic buffer-switching command:

:b4         " switch to buffer number 4
:bn         " switch to next buffer in the buffer list
:bp         " switch to previous buffer in the buffer list
:bf         " switch to first buffer in the buffer list
:bl         " switch to last buffer in the buffer list
:b foo<Tab> " switch by buffer name with tab-completion
:b#         " switch to the alternate file

请注意,其中许多命令及其相关命令都接受计数.

Note that many of these commands and their relatives accept a count.

:ls 命令显示加载的缓冲区列表.不过,它有点特殊":缓冲区在创建时会被分配一个编号,因此如果删除缓冲区,您可以获得一个类似于 1 2 5 的列表.这有点尴尬,是的,这使得按其编号切换到缓冲区有点太麻烦了.更喜欢按部分名称切换,:b foo 或循环,:bn :bp.

The :ls command shows you a list of loaded buffers. It is a bit "special", though: buffers are assigned a number when they are created so you can have a list that looks like 1 2 5 if you delete buffers. This is a bit awkward, yes, and that makes switching to a buffer by its number a bit too troublesome. Prefer switching by partial name, :b foo<Tab> or cycling, :bn :bp.

无论如何,这是一个很酷的映射,它列出了所有加载的缓冲区并为您填充提示,等待您输入缓冲区的编号并按 :

Anyway, here is a cool mapping that lists all loaded buffers and populates the prompt for you, waiting for you to type the number of a buffer and press <enter>:

nnoremap gb :ls<CR>:b<Space>

通过这种映射,切换到另一个缓冲区就像:

With this mapping, switching to another buffer is as simple as:

gb
(quickly scanning the list)
3<CR>

或:

gb
(quickly scanning the list)
foo<tab><CR>

这个想法来自这张图片 取自 Bairui 的收藏Vim 信息图表:

The idea comes from this image taken from Bairui's collection of Vim infographics:

Vim 也有 <C-^>(或某些键盘上的 <C-6>)——相当于 :b# 的普通模式——在当前缓冲区和前一个缓冲区之间跳转.如果您经常在两个缓冲区之间交替使用,请使用它.

Vim also has <C-^> (or <C-6> on some keyboards)—the normal mode equivalent of :b#—to jump between the current buffer and the previous one. Use it if you often alternate between two buffers.

:help buffers 中阅读所有关于缓冲区的信息.

Read all about buffers in :help buffers.

转到声明

在文件中,您可以使用 gdgD.

Within a file, you can use gd or gD.

在项目中,Vim 的标签"功能是您的朋友,但您需要一个外部代码索引器,如 ctags 或 cscope.最基本的命令是 :tag foo ,光标位于方法名称上.这两个工具都很好地集成到 Vim 中:参见 :help tags:help ctags:help cscope.

Within a project, Vim's "tags" feature is your friend but you'll need an external code indexer like ctags or cscope. The most basic commands are :tag foo and <C-]> with the cursor on a method name. Both tools are well integrated into Vim: see :help tags, :help ctags and :help cscope.

就其价值而言,我广泛使用标签导航在项目中移动(使用 CtrlP 的 :CtrlPTag:CtrlPBufTag 命令,大多数情况下,但内置的也是),我最喜欢的通用"缓冲区切换方法是按名称.

For what it's worth, I use tag navigation extensively to move within a project (using CtrlP's :CtrlPTag and :CtrlPBufTag commands, mostly, but the buit-in ones too) and my favorite "generic" buffer switching method is by name.

部署您的配置

许多 Vim 用户将他们的配置置于版本控制之下,这使得在新机器上安装您自己的配置非常变得非常快速和容易.考虑一下.

A lot of Vim users put their config under version control which makes it very quick and easy to install your own config on a new machine. Think about it.

编辑

几个月前,我不得不在一台装有过时 Vim 的远程机器上工作.我本可以安装一个合适的 Vim 并克隆我自己心爱的配置,但这次我决定轻装上阵,以锐化锯".我很快构建了一个极简的 .vimrc 并重新审视了几个被遗忘的原生特性.在那场演出之后,我认为 CtrlP 不是必需的并摆脱了它:本机功能和自定义映射并不那么吸引人,但它们可以在没有太多依赖的情况下完成工作.

A few months ago, I had to work on a remote machine with an outdated Vim. I could have installed a proper Vim and cloned my own beloved config but I decided to travel light, this time, in order to "sharpen the saw". I quickly built a minimalist .vimrc and revisited a couple of half forgotten native features. After that gig, I decided CtrlP wasn't that necessary and got rid of it: native features and custom mappings are not as sexy but they get the job done without much dependencies.

处理文件

set path=.,**
nnoremap <leader>f :find *
nnoremap <leader>s :sfind *
nnoremap <leader>v :vert sfind *
nnoremap <leader>t :tabfind *

只要您设置路径正确,

:find 就是一个非常棒的命令.根据我的设置,,ffoo 将递归查找当前目录下所有包含 foo 的文件.它快速、直观且轻便.当然,我受益于与 :edit 和朋友相同的补全和通配符.

:find is a truly great command as soon as you set path correctly. With my settings, ,ffoo<Tab> will find all the files containing foo under the current directory, recursively. It's quick, intuitive and lightweight. Of course, I benefit from the same completion and wildcards as with :edit and friends.

为了使过程更快,以下映射允许我跳过项目的整个部分并在当前文件的目录下递归查找文件:

To make the process even quicker, the following mappings allow me to skip entire parts of the project and find files recursively under the directory of the current file:

nnoremap <leader>F :find <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>S :sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>V :vert sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>T :tabfind <C-R>=expand('%:h').'/*'<CR>

警告!path 选项非常强大.上面的值—.,**对我有用,但我使用的语言没有标准库.适当的值完全取决于的需求.

WARNING! The path option is extremely powerful. The value above—.,**—works for me but the languages I use don't have a standard library. The proper value depends entirely on your needs.

处理缓冲区

set wildcharm=<C-z>
nnoremap <leader>b :buffer <C-z><S-Tab>
nnoremap <leader>B :sbuffer <C-z><S-Tab>

上面的映射列出了带有空提示的wildmenu"中的可用缓冲区,允许我使用 导航菜单或键入几个字母和 <再次 Tab> 以缩小列表范围.与上面的文件映射一样,这个过程很快,几乎没有摩擦.

The mappings above list the available buffers in the "wildmenu" with an empty prompt, allowing me to either navigate the menu with <Tab> or type a few letters and <Tab> again to narrow down the list. Like with the file mappings above, the process is quick and almost friction-less.

nnoremap <PageUp>   :bprevious<CR>
nnoremap <PageDown> :bnext<CR>

那些映射不言自明.

处理标签

nnoremap <leader>j :tjump /

此映射使用正则表达式搜索而不是全词搜索,因此我可以使用 ,jba 来查找标签 foobarbaz().

This mapping uses regex search instead of whole word search so I can do ,jba<Tab> to find tag foobarbaz().

是的,模糊匹配很容易让人上瘾,但没有它您也可以同样高效.而且只需要一小部分成本.

Yes, fuzzy matching is addictive but you can be just as productive without it. And for a fraction of the cost.

更多编辑

一些额外的提示/技巧......

A couple of additional tips/tricks…

通配菜单选项

使用 set wildmenu 启用的wildmenu"使文件/缓冲区导航更容易.它的行为受一系列值得研究的选项控制:

The "wildmenu", enabled with set wildmenu, makes file/buffer navigation easier. Its behavior is governed by a bunch of options that are worth investigating:

wildmode 告诉 Vim 你希望wildmenu"如何表现:

wildmode tells Vim how you want the "wildmenu" to behave:

set wildmode=list:full

wildignore 过滤掉所有的垃圾:

set wildignore=*.swp,*.bak
set wildignore+=*.pyc,*.class,*.sln,*.Master,*.csproj,*.csproj.user,*.cache,*.dll,*.pdb,*.min.*
set wildignore+=*/.git/**/*,*/.hg/**/*,*/.svn/**/*
set wildignore+=tags
set wildignore+=*.tar.*

wildignorecase 允许你搜索 foo 并找到 Foo:

wildignorecase allows you to search for foo and find Foo:

set wildignorecase

<小时>

文件标记

augroup VIMRC
  autocmd!

  autocmd BufLeave *.css  normal! mC
  autocmd BufLeave *.html normal! mH
  autocmd BufLeave *.js   normal! mJ
  autocmd BufLeave *.php  normal! mP
augroup END

我最近在别人的 ~/.vimrc 中发现了这个 gem.每当您离开缓冲区时,它都会在光标的确切位置创建一个文件标记,以便无论您身在何处,'J 都会跳转到您编辑的最新 JavaScript 缓冲区.太棒了.

I recently found this gem in someone else's ~/.vimrc. It creates a file mark at the exact position of the cursor whenever you leave a buffer so that, wherever you are, 'J jumps to the latest JavaScript buffer you edited. Awesome.

这篇关于vim,使用 vanilla Vim 快速切换文件(无插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆