How to effectively work with multiple files in Vim

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

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

    nnoremap f :set nomore:ls:set more:b
    

    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 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 , or to autocomplete,
      • Or just or 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 ' 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'.

提交回复
热议问题