How to effectively work with multiple files in Vim

前端 未结 28 2655
甜味超标
甜味超标 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:42

    I use the command line and git a lot, so I have this alias in my bashrc:

    alias gvim="gvim --servername \$(git rev-parse --show-toplevel || echo 'default') --remote-tab"
    

    This will open each new file in a new tab on an existing window and will create one window for each git repository. So if you open two files from repo A, and 3 files from repo B, you will end up with two windows, one for repo A with two tabs and one for repo B with three tabs.

    If the file you are opening is not contained in a git repo it will go to a default window.

    To jump between tabs I use these mappings:

    nmap <C-p> :tabprevious<CR>
    nmap <C-n> :tabnext<CR>
    

    To open multiple files at once you should combine this with one of the other solutions.

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

    I made a very simple video showing the workflow that I use. Basically I use the Ctrl-P Vim plugin, and I mapped the buffer navigation to the Enter key.

    In this way I can press Enter in normal mode, look at the list of open files (that shows up in a small new window at the bottom of the screen), select the file I want to edit and press Enter again. To quickly search through multiple open files, just type part of the file name, select the file and press Enter.

    I don't have many files open in the video, but it becomes incredibly helpful when you start having a lot of them.

    Since the plugin sorts the buffers using a MRU ordering, you can just press Enter twice and jump to the most recent file you were editing.

    After the plugin is installed, the only configuration you need is:

    nmap <CR> :CtrlPBuffer<CR>
    

    Of course you can map it to a different key, but I find the mapping to enter to be very handy.

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

    I use buffer commands - :bn (next buffer), :bp (previous buffer) :buffers (list open buffers) :b<n> (open buffer n) :bd (delete buffer). :e <filename> will just open into a new buffer.

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

    I think you may be using the wrong command for looking at the list of files that you have open.

    Try doing an :ls to see the list of files that you have open and you'll see:

       1 %a   "./checkin.pl"            line 1
      2 #    "./grabakamailogs.pl"     line 1
      3      "./grabwmlogs.pl"         line 0
      etc.
    

    You can then bounce through the files by referring to them by the numbers listed, e.g. :3b

    or you can split your screen by entering the number but using sb instead of just b.

    As an aside % refers to the file currently visible and # refers to the alternate file.

    You can easily toggle between these two files by pressing Ctrl Shift 6

    Edit: like :ls you can use :reg to see the current contents of your registers including the 0-9 registers that contain what you've deleted. This is especially useful if you want to reuse some text that you've previously deleted.

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

    I use multiple buffers that are set hidden in my ~/.vimrc file.

    The mini-buffer explorer script is nice too to get a nice compact listing of your buffers. Then :b1 or :b2... to go to the appropriate buffer or use the mini-buffer explorer and tab through the buffers.

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

    Adding another answer as this is not covered by any of the answer

    To change all buffers to tab view.

     :tab sball
    

    will open all the buffers to tab view. Then we can use any tab related commands

    gt or :tabn           "    go to next tab
    gT or :tabp or :tabN  "    go to previous tab
    

    details at :help tab-page-commands.

    We can instruct vim to open ,as tab view, multiple files by vim -p file1 file2. alias vim='vim -p' will be useful.
    The same thing can also be achieved by having following autocommand in ~/.vimrc

     au VimEnter * if !&diff | tab all | tabfirst | endif
    

    Anyway to answer the question: To add to arg list: arga file,

    To delete from arg list: argd pattern

    More at :help arglist

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