vim, switching between files rapidly using vanilla Vim (no plugins)

后端 未结 7 2004
南旧
南旧 2020-11-30 16:14

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 m

相关标签:
7条回答
  • 2020-11-30 16:50

    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.

    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.


    Opening files

    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>
    

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

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

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

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

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

    :next **/*.js
    

    and take a look at :help arglist.


    Jumping between buffers

    :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.

    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>
    

    or:

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

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

    Flying vs cycling

    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.

    Read all about buffers in :help buffers.


    Go to declaration

    Within a file, you can use gd or gD.

    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.

    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.


    Deploying your config

    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.


    EDIT

    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.


    Juggling with files

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

    :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>
    

    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.


    Juggling with buffers

    set wildcharm=<C-z>
    nnoremap <leader>b :buffer <C-z><S-Tab>
    nnoremap <leader>B :sbuffer <C-z><S-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>
    

    Those mappings speak for themselves.


    Juggling with tags

    nnoremap <leader>j :tjump /
    

    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.


    MORE EDIT

    A couple of additional tips/tricks…


    Wildmenu options

    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 tells Vim how you want the "wildmenu" to behave:

    set wildmode=list:full
    

    wildignore filters out all the cruft:

    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 allows you to search for foo and find Foo:

    set wildignorecase
    

    File marks

    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
    

    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.

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