Vim中如何将位置4的Buffer移动到位置2?
Got it, let's break down how to rearrange your buffer list so the 4th buffer shifts to position 2. I'll cover both native Vim approaches (no plugins required) and a convenient plugin that streamlines this task.
Native Vim Method (No Plugins)
Vim doesn’t include a direct "move buffer" command by default, but you can tweak the buffer order using session files or manual buffer management:
Option 1: Edit the Session File
- Save your current session:
Run:mksession! buffer_order.vim— the!overwrites the file if it exists. This file stores all your open buffers and their current order. - Modify the session file:
Open it with:edit buffer_order.vim. Look for lines starting withbaddorbuffer— these lines define the sequence of your buffers. - Rearrange the buffer entries:
Find the line for your 4th buffer, cut it, and paste it into the spot where the 2nd buffer is listed. Save and exit with:wq. - Reload the session:
Run:source buffer_order.vimto apply the new order. Verify with:lsto confirm the 4th buffer is now in position 2.
Option 2: Manual Buffer Reopening
- First, enable hidden buffers with
:set hidden(so you can switch buffers without saving unsaved changes). - Save any work in the 4th buffer, then close it with
:bdelete 4. - Switch to the 1st buffer using
:b1, then reopen the 4th buffer by its name or path. It will now occupy position 2 in the buffer list.
Plugin Method (Frequent Use Recommendation)
If you rearrange buffers often, the vim-bufmove plugin makes this trivial:
Install the plugin:
For Packer.nvim, add this to your config:use 'matze/vim-bufmove'For Vundle, add:
Plugin 'matze/vim-bufmove'Run your plugin installer (e.g.,
:PackerSyncor:PluginInstall) to complete setup.Move the buffer:
- Switch to the 4th buffer with
:b4. - Use the default shortcut
<A-Up>(Alt + Up arrow) twice to shift it up two positions to slot 2. Alternatively, run the command:BufMoveUptwice. - Check the updated order with
:ls— you’re done!
- Switch to the 4th buffer with
The plugin also supports moving buffers down with <A-Down> or :BufMoveDown for quick adjustments.
内容的提问来源于stack exchange,提问作者Cirel Ramos

