How to effectively work with multiple files in Vim

前端 未结 28 2657
甜味超标
甜味超标 2020-11-28 00:18

I\'ve started using Vim to develop Perl scripts and am starting to find it very powerful.

One thing I like is to be able to open multiple files at once with:

<
相关标签:
28条回答
  • 2020-11-28 00:45

    Most of the answers in this thread are using plain vim commands which is of course fine but I thought I would provide an extensive answer using a combination of plugins and functions that I find particularly useful (at least some of these tips came from Gary Bernhardt's file navigation tips):

    1. To toggle between the last two file just press <leader> twice. I recommend assigning <leader> to the spacebar:

      nnoremap <leader><leader> <c-^>
      
    2. For quickly moving around a project the answer is a fuzzy matching solution such as CtrlP. I bind it to <leader>a for quick access.

    3. In the case I want to see a visual representation of the currently open buffers I use the BufExplorer plugin. Simple but effective.

    4. If I want to browse around the file system I would use the command line or an external utility (Quicklsilver, Afred etc.) but to look at the current project structure NERD Tree is a classic. Do not use this though in the place of 2 as your main file finding method. It will really slow you down. I use the binding <leader>ff.

    These should be enough for finding and opening files. From there of course use horizontal and vertical splits. Concerning splits I find these functions particularly useful:

    1. Open new splits in smaller areas when there is not enough room and expand them on navigation. Refer here for comments on what these do exactly:

      set winwidth=84
      set winheight=5
      set winminheight=5
      set winheight=999
      
      nnoremap <C-w>v :111vs<CR>
      nnoremap <C-w>s :rightbelow split<CR>
      set splitright
      
    2. Move from split to split easily:

      nnoremap <C-J> <C-W><C-J>
      nnoremap <C-K> <C-W><C-K>
      nnoremap <C-L> <C-W><C-L>
      nnoremap <C-H> <C-W><C-H>
      
    0 讨论(0)
  • 2020-11-28 00:47

    Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe <filepath> you can add a new tab; and with a regular :q or :wq you close a tab. If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.

    If there are not that many files or you don't have Vim 7 you can also split your screen in multiple files: :sp <filepath>. Then you can switch between splitscreens with Ctrl+W and then an arrow key in the direction you want to move (or instead of arrow keys, w for next and W for previous splitscreen)

    0 讨论(0)
  • 2020-11-28 00:47

    Many answers here! What I use without reinventing the wheel - the most famous plugins (that are not going to die any time soon and are used by many people) to be ultra fast and geeky.

    • ctrlpvim/ctrlp.vim - to find file by name fuzzy search by its location or just its name
    • jlanzarotta/bufexplorer - to browse opened buffers (when you do not remember how many files you opened and modified recently and you do not remember where they are, probably because you searched for them with Ag)
    • rking/ag.vim to search the files with respect to gitignore
    • scrooloose/nerdtree to see the directory structure, lookaround, add/delete/modify files

    EDIT: Recently I have been using dyng/ctrlsf.vim to search with contextual view (like Sublime search) and I switched the engine from ag to ripgrep. The performance is outstanding.

    EDIT2: Along with CtrlSF you can use mg979/vim-visual-multi, make changes to multiple files at once and then at the end save them in one go.

    0 讨论(0)
  • 2020-11-28 00:48

    When I started using VIM I didn't realize that tabs were supposed to be used as different window layouts, and buffer serves the role for multiple file editing / switching between each other. Actually in the beginning tabs are not even there before v7.0 and I just opened one VIM inside a terminal tab (I was using gnome-terminal at the moment), and switch between tabs using alt+numbers, since I thought using commands like :buffers, :bn and :bp were too much for me. When VIM 7.0 was released I find it's easier to manager a lot of files and switched to it, but recently I just realized that buffers should always be the way to go, unless one thing: you need to configure it to make it works right.

    So I tried vim-airline and enabled the visual on-top tab-like buffer bar, but graphic was having problem with my iTerm2, so I tried a couple of others and it seems that MBE works the best for me. I also set shift+h/l as shortcuts, since the original ones (moving to the head/tail of the current page) is not very useful to me.

    map <S-h> :bprev<Return>
    map <S-l> :bnext<Return>

    It seems to be even easier than gt and gT, and :e is easier than :tabnew too. I find :bd is not as convenient as :q though (MBE is having some problem with it) but I can live with all files in buffer I think.

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