Vim Buffers, Windows & Tabs

Managing buffers, windows, and tabs.

Buffer Basics

List buffers
:ls                                      " List all buffers
:buffers                                 " Same as :ls
:files                                   " Same as :ls
Buffer indicators
" %  = current buffer
" #  = alternate buffer
" a  = active (loaded and visible)
" h  = hidden (loaded but not visible)
" +  = modified
" -  = readonly
" =  = readonly

Navigate Buffers

Switch buffers
:bnext                                   " Next buffer
:bprev                                   " Previous buffer
:bfirst                                  " First buffer
:blast                                   " Last buffer
By number
:b3                                      " Go to buffer 3
:buffer 3                                " Same
By name
:b file.adoc                             " Go to buffer by name
:b adoc<Tab>                             " Tab complete
Alternate buffer
:b#                                      " Switch to alternate buffer
Ctrl-^                                   " Same (faster)

Open Files to Buffers

Edit file
:e file.adoc                             " Open file in buffer
:e .                                     " Open file explorer
:e **/*pattern*<Tab>                     " Find and open
Multiple files
:args *.adoc                             " Open all adoc files
:argadd file.adoc                        " Add to args
:argdo %s/old/new/g                      " Run on all args

Close Buffers

Delete buffer
:bd                                      " Delete current buffer
:bd 3                                    " Delete buffer 3
:bd file.adoc                            " Delete by name
:bd!                                     " Force delete (discard changes)
Delete multiple
:bd 1 2 3                                " Delete buffers 1, 2, 3
:1,3bd                                   " Delete range
:%bd                                     " Delete all buffers
:%bd|e#                                  " Delete all except current
Wipe buffer
:bw                                      " Wipe buffer completely
:bw!                                     " Force wipe

Hidden Buffers

Allow hidden
:set hidden                              " Allow unsaved hidden buffers
Without hidden
:set nohidden                            " Must save before switching

Buffer Windows

Open buffer in split
:sb 3                                    " Split and show buffer 3
:sb file.adoc                            " Split by name
:vert sb 3                               " Vertical split
Open all buffers
:ball                                    " Open all buffers in windows
:vert ball                               " Vertical splits
:tab ball                                " Open all in tabs
Search in buffer
:bufdo /pattern                          " Search in all buffers
:bufdo %s/old/new/g                      " Replace in all buffers
:bufdo update                            " Save all buffers

Unlisted Buffers

Show unlisted
:ls!                                     " Show all including unlisted
Unlisted types
" Help files
" Quickfix
" Netrw

Buffer Shortcuts

Common mappings
nnoremap <leader>bn :bnext<CR>
nnoremap <leader>bp :bprev<CR>
nnoremap <leader>bd :bd<CR>
nnoremap <leader>bl :ls<CR>
nnoremap <Tab> :bnext<CR>
nnoremap <S-Tab> :bprev<CR>

Buffer with FZF

FZF buffer picker
:Buffers                                 " FZF buffer list (if fzf.vim)
Custom mapping
nnoremap <leader>b :Buffers<CR>

Create Splits

Horizontal split
:split                                   " Split current window
:sp                                      " Short form
:sp file.adoc                            " Split and open file
Ctrl-w s                                 " Split current window
Vertical split
:vsplit                                  " Vertical split
:vs                                      " Short form
:vs file.adoc                            " Vertical split with file
Ctrl-w v                                 " Vertical split current
New window
:new                                     " New horizontal split (empty)
:vnew                                    " New vertical split (empty)

Navigate Windows

Move between windows
Ctrl-w h                                 " Move to left window
Ctrl-w j                                 " Move to window below
Ctrl-w k                                 " Move to window above
Ctrl-w l                                 " Move to right window
Cycle windows
Ctrl-w w                                 " Next window
Ctrl-w W                                 " Previous window
Ctrl-w p                                 " Previous (last accessed) window
Jump to window
Ctrl-w t                                 " Top-left window
Ctrl-w b                                 " Bottom-right window

Close Windows

Close current
:q                                       " Close window (and vim if last)
:close                                   " Close window
Ctrl-w c                                 " Close window
Ctrl-w q                                 " Close window (quit)
Close others
:only                                    " Close all other windows
Ctrl-w o                                 " Same

Resize Windows

Height
Ctrl-w +                                 " Increase height
Ctrl-w -                                 " Decrease height
Ctrl-w _                                 " Maximize height
:resize 10                               " Set height to 10
:res +5                                  " Increase by 5
:res -5                                  " Decrease by 5
Width
Ctrl-w >                                 " Increase width
Ctrl-w <                                 " Decrease width
Ctrl-w |                                 " Maximize width
:vertical resize 80                      " Set width to 80
Equal size
Ctrl-w =                                 " Make all equal size

Move Windows

Rotate
Ctrl-w r                                 " Rotate windows down/right
Ctrl-w R                                 " Rotate windows up/left
Move to position
Ctrl-w H                                 " Move to far left (vertical)
Ctrl-w J                                 " Move to bottom (horizontal)
Ctrl-w K                                 " Move to top (horizontal)
Ctrl-w L                                 " Move to far right (vertical)
Exchange
Ctrl-w x                                 " Exchange with next window

Window to Tab

Move to tab
Ctrl-w T                                 " Move window to new tab

Split Behavior

Where splits open
:set splitbelow                          " Horizontal splits below
:set splitright                          " Vertical splits right

Preview Window

Open preview
:pedit file.adoc                         " Open in preview window
:ped file.adoc                           " Short form
Ctrl-w }                                 " Preview tag under cursor
Close preview
:pclose                                  " Close preview window
Ctrl-w z                                 " Same

Diff Windows

Diff mode
:diffthis                                " Current window in diff mode
:diffoff                                 " Turn off diff mode
:diffoff!                                " Turn off in all windows
:diffupdate                              " Refresh diff
Navigate diff
]c                                       " Next change
[c                                       " Previous change
do                                       " Get change from other (obtain)
dp                                       " Put change to other

Window Commands

Run in all windows
:windo %s/old/new/g                      " Substitute in all windows
:windo set number                        " Set option in all windows

Window Shortcuts

Suggested mappings
nnoremap <leader>wh :split<CR>
nnoremap <leader>wv :vsplit<CR>
nnoremap <leader>wc :close<CR>
nnoremap <leader>wo :only<CR>
nnoremap <leader>w= <C-w>=

" Easier navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" Resize with arrows
nnoremap <C-Up> :resize +2<CR>
nnoremap <C-Down> :resize -2<CR>
nnoremap <C-Left> :vertical resize -2<CR>
nnoremap <C-Right> :vertical resize +2<CR>

Create Tabs

New tab
:tabnew                                  " New empty tab
:tabnew file.adoc                        " New tab with file
:tabe file.adoc                          " Short form
Tab from split
Ctrl-w T                                 " Move current window to new tab
Open in tab
:tab split                               " Current buffer in new tab
:tab help vim                            " Help in new tab
:tab terminal                            " Terminal in new tab

Navigate Tabs

Next/previous
gt                                       " Next tab
gT                                       " Previous tab
:tabnext                                 " Next tab
:tabprev                                 " Previous tab
By number
1gt                                      " Go to tab 1
2gt                                      " Go to tab 2
:tabnext 3                               " Go to tab 3
First/last
:tabfirst                                " First tab
:tablast                                 " Last tab

Close Tabs

Close current
:tabclose                                " Close current tab
:tabc                                    " Short form
Close others
:tabonly                                 " Close all other tabs
:tabo                                    " Short form

Move Tabs

Reorder tabs
:tabmove 0                               " Move to first position
:tabmove                                 " Move to last position
:tabmove +1                              " Move right
:tabmove -1                              " Move left

List Tabs

Show tabs
:tabs                                    " List all tabs and windows

Tab Line

Show tab line
:set showtabline=0                       " Never show
:set showtabline=1                       " Show if >1 tab (default)
:set showtabline=2                       " Always show

Tab Commands

Run in all tabs
:tabdo %s/old/new/g                      " Substitute in all tabs
:tabdo update                            " Save all tabs

Open Files in Tabs

Multiple files
:args *.adoc | tab all                   " Open all adoc in tabs
From command line
nvim -p file1.adoc file2.adoc file3.adoc

Tab Shortcuts

Suggested mappings
nnoremap <leader>tn :tabnew<CR>
nnoremap <leader>tc :tabclose<CR>
nnoremap <leader>to :tabonly<CR>
nnoremap <leader>tm :tabmove<Space>

" Quick tab switching
nnoremap <leader>1 1gt
nnoremap <leader>2 2gt
nnoremap <leader>3 3gt
nnoremap <leader>4 4gt
nnoremap <leader>5 5gt

" Alt+number for tabs (terminal emulator)
nnoremap <A-1> 1gt
nnoremap <A-2> 2gt
nnoremap <A-3> 3gt

Tabs vs Buffers

When to use
" Buffers: Different files in same project
" Windows: Viewing multiple files simultaneously
" Tabs: Different contexts/workspaces

" Workflow:
" - Many buffers
" - Few windows per tab
" - Few tabs (different tasks)