Skip to main content

Files and Buffers

Complete guide to file operations and buffer management in Vim.

File Operations

Opening Files

:e filename         " Edit file
:edit filename " Edit file (same as :e)
:e . " Open file browser in current directory
:e %:h " Open file browser in directory of current file
:e **/*pattern* " Open file matching pattern (with wildcards)

File Completion

:e <Tab>           " File completion
:e <Ctrl+d> " Show possible completions
:e part<Tab> " Complete filename starting with 'part'

Saving Files

:w                 " Write (save) current file
:w filename " Write to specific filename
:w! " Force write (overwrite readonly)
:wa " Write all buffers
:wall " Write all buffers (same as :wa)

Saving and Quitting

:wq                " Write and quit
:x " Write and quit (only if changes)
ZZ " Write and quit (normal mode)
:wqa " Write all buffers and quit
:xa " Write all buffers and quit

Quitting

:q                 " Quit (fails if unsaved changes)
:q! " Quit without saving
:qa " Quit all buffers
:qa! " Quit all buffers without saving
ZQ " Quit without saving (normal mode)

File Information

:f                 " Show current filename and status
:file " Show current filename and status
Ctrl+g " Show file information
1 Ctrl+g " Show full path
2 Ctrl+g " Show buffer number

Buffer Management

Buffer Basics

  • Buffer: A file loaded into memory
  • Window: A viewport onto a buffer
  • Tab: A collection of windows

Buffer Navigation

:ls                " List all buffers
:buffers " List all buffers (same as :ls)
:files " List all buffers (same as :ls)

Buffer Status Indicators

%     " Current buffer
# " Alternate buffer (last edited)
a " Active buffer (visible)
h " Hidden buffer (not visible)
+ " Modified buffer
- " Readonly buffer
= " Readonly buffer (same as -)
x " Error reading buffer

Switching Buffers

:b number          " Switch to buffer number
:b filename " Switch to buffer by filename
:b partial " Switch to buffer by partial name
:bn " Next buffer
:bnext " Next buffer (same as :bn)
:bp " Previous buffer
:bprev " Previous buffer (same as :bp)
:bf " First buffer
:bl " Last buffer

Buffer Shortcuts

Ctrl+^             " Switch to alternate buffer
Ctrl+6 " Switch to alternate buffer (same as Ctrl+^)

Buffer Deletion

:bd                " Delete current buffer
:bdelete " Delete current buffer (same as :bd)
:bd number " Delete buffer by number
:bd filename " Delete buffer by filename
:bw " Wipe buffer (more thorough than :bd)
:bwipeout " Wipe buffer (same as :bw)

Buffer Operations

:ball              " Open all buffers in windows
:sball " Open all buffers in split windows
:unhide " Open all buffers in windows (same as :ball)

Advanced Buffer Management

Buffer Ranges

:1,3bd             " Delete buffers 1-3
:2,$bd " Delete buffers 2 to last
:%bd " Delete all buffers

Buffer Patterns

:bd *.txt          " Delete all .txt buffers
:bd pattern " Delete buffers matching pattern

Buffer Modification

:set modified      " Mark buffer as modified
:set nomodified " Mark buffer as not modified

Working with Multiple Files

Opening Multiple Files

vim file1 file2 file3    " Open multiple files
vim *.txt " Open all .txt files
vim -o file1 file2 " Open files in horizontal splits
vim -O file1 file2 " Open files in vertical splits

Argument List

:args              " Show argument list
:args *.txt " Set argument list to all .txt files
:argadd file " Add file to argument list
:argdelete file " Remove file from argument list

Argument Navigation

:n                 " Next file in argument list
:next " Next file (same as :n)
:N " Previous file in argument list
:prev " Previous file (same as :N)
:first " First file in argument list
:last " Last file in argument list

Argument Operations

:argdo command     " Execute command on all files in argument list
:argdo %s/old/new/g " Replace in all argument files
:argdo update " Save all argument files

File Browsing

Built-in File Browser (netrw)

:e .               " Open file browser
:Explore " Open file browser (same as :e .)
:Sexplore " Open file browser in split
:Vexplore " Open file browser in vertical split

File Browser Navigation

<Enter>            " Open file/directory
- " Go up one directory
D " Delete file/directory
R " Rename file/directory
% " Create new file
d " Create new directory

File Browser Settings

:let g:netrw_liststyle = 3    " Tree view
:let g:netrw_winsize = 25 " Window size
:let g:netrw_browse_split = 4 " Open in previous window

Session Management

Session Basics

:mksession session.vim    " Create session file
:source session.vim " Load session file
vim -S session.vim " Start vim with session

Session Options

:set sessionoptions=blank,buffers,curdir,folds,help,options,tabpages,winsize,winpos

View Management

:mkview            " Save view (cursor position, folds, etc.)
:loadview " Load view

Backup and Recovery

Backup Settings

:set backup        " Enable backup files
:set backupdir=~/.vim/backup " Set backup directory
:set backupext=.bak " Set backup extension

Swap Files

:set swapfile      " Enable swap files
:set directory=~/.vim/swap " Set swap directory
:recover " Recover from swap file

Undo Files

:set undofile      " Enable persistent undo
:set undodir=~/.vim/undo " Set undo directory

File Encoding and Format

Encoding

:set encoding=utf-8       " Set internal encoding
:set fileencoding=utf-8 " Set file encoding
:set fileencodings=utf-8,latin1 " Set encoding detection

File Format

:set fileformat=unix      " Set to Unix format (LF)
:set fileformat=dos " Set to DOS format (CRLF)
:set fileformat=mac " Set to Mac format (CR)

Viewing File Info

:set                      " Show all options
:set encoding? " Show encoding
:set fileformat? " Show file format

Practical File Operations

Common File Workflows

:e ~/.vimrc        " Edit Vim configuration
:so % " Source current file
:w !sudo tee % " Save file as root

File Comparisons

:diffsplit file2   " Compare with another file
:diffget " Get changes from other file
:diffput " Put changes to other file

File Permissions

:!chmod +x %       " Make current file executable
:!ls -l % " Show file permissions

Buffer and File Tips

Efficiency Tips

  1. Use buffer numbers for quick switching
  2. Use partial names for buffer switching
  3. Use argument list for working with multiple files
  4. Set up sessions for project work
  5. Use file browser for exploring directories

Common Patterns

:ls | :b 3         " List buffers then switch to buffer 3
:argdo %s/old/new/g | update " Replace in all files and save
:bufdo %s/old/new/g | update " Replace in all buffers and save

Workspace Management

:mksession! project.vim    " Save session (overwrite)
:qa " Quit all
vim -S project.vim " Resume session

Troubleshooting Files and Buffers

Common Issues

  1. Can't save file - Check permissions or use :w !sudo tee %
  2. Too many buffers - Use :bd to delete unused buffers
  3. Lost changes - Check for swap files with :recover
  4. Encoding issues - Check :set encoding? and :set fileencoding?

Recovery Options

:recover           " Recover from swap file
:e! " Reload file (lose changes)
:earlier 1f " Go to earlier file state

Buffer Cleanup

:%bd|e#            " Delete all buffers except current
:bufdo bd " Delete all buffers

Advanced File Operations

Remote File Editing

:e scp://server/path/file    " Edit remote file via SCP
:e ftp://server/path/file " Edit remote file via FTP

File Searching

:find filename     " Find and open file in path
:sf filename " Find and split file
:vert sf filename " Find and vertical split file

File Path Manipulation

:cd %:h            " Change to directory of current file
:pwd " Show current directory
:lcd %:h " Change local directory

This comprehensive guide covers all aspects of file operations and buffer management in Vim. Master these techniques to efficiently work with multiple files and manage your editing sessions.