How to effectively work with multiple files in Vim

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

    I use the following, this gives you lots of features that you'd expect to have in other editors such as Sublime Text / Textmate

    1. Use buffers not 'tab pages'. Buffers are the same concept as tabs in almost all other editors.
    2. If you want the same look of having tabs you can use the vim-airline plugin with the following setting in your .vimrc: let g:airline#extensions#tabline#enabled = 1. This automatically displays all the buffers as tab headers when you have no tab pages opened
    3. Use Tim Pope's vim-unimpaired which gives [b and ]b for moving to previous/next buffers respectively (plus a whole host of other goodies)
    4. Have set wildmenu in your .vimrc then when you type :b <file part> + Tab for a buffer you will get a list of possible buffers that you can use left/right arrows to scroll through
    5. Use Tim Pope's vim-obsession plugin to store sessions that play nicely with airline (I had lots of pain with sessions and plugins)
    6. Use Tim Pope's vim-vinegar plugin. This works with the native :Explore but makes it much easier to work with. You just type - to open the explorer, which is the same key as to go up a directory in the explorer. Makes navigating faster (however with fzf I rarely use this)
    7. fzf (which can be installed as a vim plugin) is also a really powerful fuzzy finder that you can use for searching for files (and buffers too). fzf also plays very nicely with fd (a faster version of find)
    8. Use Ripgrep with vim-ripgrep to search through your code base and then you can use :cdo on the results to do search and replace
    0 讨论(0)
  • 2020-11-28 00:30

    To add to the args list:

    :argadd
    

    To delete from the args list:

    :argdelete
    

    In your example, you could use :argedit test.pl to add test.pl to the args list and edit the file in one step.

    :help args gives much more detail and advanced usage

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

    If using only vim built-in commands, the best one that I ever saw to switch among multiple buffers is this:

    nnoremap <Leader>f :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>
    

    It perfectly combines both :ls and :b commands -- listing all opened buffers and waiting for you to input the command to switch buffer.

    Given above mapping in vimrc, once you type <Leader>f,

    • All opened buffers are displayed
    • You can:
      • Type 23 to go to buffer 23,
      • Type # to go to the alternative/MRU buffer,
      • Type partial name of file, then type <Tab>, or <C-i> to autocomplete,
      • Or just <CR> or <Esc> to stay on current buffer

    A snapshot of output for the above key mapping is:

    :set nomore|:ls|:set more
      1  h    "script.py"    line 1
      2 #h  + "file1.txt"    line 6  -- '#' for alternative buffer
      3 %a    "README.md"    line 17 -- '%' for current buffer
      4       "file3.txt"    line 0  -- line 0 for hasn't switched to
      5     + "/etc/passwd"  line 42 -- '+' for modified
    :b '<Cursor> here'
    

    In the above snapshot:

    • Second column: %a for current, h for hidden, # for previous, empty for hasn't been switched to.
    • Third column: + for modified.

    Also, I strongly suggest set hidden. See :help 'hidden'.

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

    My way to effectively work with multiple files is to use tmux.

    It allows you to split windows vertically and horizontally, as in:

    enter image description here

    I have it working this way on both my mac and linux machines and I find it better than the native window pane switching mechanism that's provided (on Macs). I find the switching easier and only with tmux have I been able to get the 'new page at the same current directory' working on my mac (despite the fact that there seems to be options to open new panes in the same directory) which is a surprisingly critical piece. An instant new pane at the current location is amazingly useful. A method that does new panes with the same key combos for both OS's is critical for me and a bonus for all for future personal compatibility. Aside from multiple tmux panes, I've also tried using multiple tabs, e.g. enter image description here and multiple new windows, e.g. enter image description here and ultimately I've found that multiple tmux panes to be the most useful for me. I am very 'visual' and like to keep my various contexts right in front of me, connected together as panes.

    tmux also support horizontal and vertical panes which the older screen didn't (though mac's iterm2 seems to support it, but again, the current directory setting didn't work for me). tmux 1.8

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

    Some answers in this thread suggest using tabs and others suggest using buffer to accomplish the same thing. Tabs and Buffers are different. I strongly suggest you read this article "Vim Tab madness - Buffers vs Tabs".

    Here's a nice summary I pulled from the article:

    Summary:

    • A buffer is the in-memory text of a file.
    • A window is a viewport on a buffer.
    • A tab page is a collection of windows.
    0 讨论(0)
  • 2020-11-28 00:33

    have a try following maps for convenience editing multiple files

    " split windows

    nmap <leader>sh :leftabove vnew<CR>

    nmap <leader>sl :rightbelow vnew<CR>

    nmap <leader>sk :leftabove new<CR>

    nmap <leader>sj :rightbelow new<CR>

    " moving around

    nmap <C-j> <C-w>j

    nmap <C-k> <C-w>k

    nmap <C-l> <C-w>l

    nmap <C-h> <C-w>h

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