Over time, Vim configurations often accumulate unused plugins—slowing startup, consuming disk space, and encreasing maintenance overhead. vim-plug, a lightweight plugin manager, offers PlugClean to safely identify and remove orphaned plugins without disrupting active ones. This guide walks through its secure usage, emphasizing verification, safety guards, and recovery options.
How PlugClean Determines What to Remove
The command scans your runtime configuration (e.g., ~/.vimrc or ~/.config/nvim/init.vim) and compares it against the contents of your plugin directory (typically ~/.vim/plugged or ~/.local/share/nvim/site/plugged). Any plugin present in the filesystem but absent from the Plug declarations is flagged for removal.
By default, :PlugClean runs interactively—it lists candidates and waits for explicit confirmation. The bang variant (:PlugClean!) bypasses prompts and deletes immediately, intended only for scripted or fully verified workflows.
Step-by-Step Safe Cleanup
1. Pre-Cleanup Backup
Before running any cleanup, preserve you're current state:
# Backup config
cp ~/.vimrc ~/.vimrc.bak.$(date -I)
# Optional: archive installed plugins
tar -czf plugged-backup-$(date -I).tar.gz -C ~/.vim plugged
2. Audit Plugin Status
Run :PlugStatus to surface discrepancies:
:PlugStatus
Output includes status indicators like [ok], [not listed], or [out of date]. Plugins marked [not listed] are eligible for PlugClean.
3. Initiate Interactive Cleanup
In normal mode, execute:
:PlugClean
A new buffer appears listing unregistered plugins:
The following plugins are not declared in your config:
- tpope/vim-surround
- jiangmiao/auto-pairs
Delete all? [y/N]:
Type y only after verifying each entry manually.
4. Post-Cleanup Validation
Confirm success with:
:PlugStatus— no[not listed]entries remainls ~/.vim/pluggedorls ~/.local/share/nvim/site/plugged— directory contents match declared plugins
Advanced Usage Patterns
Guarded Automation Function
Add this safeguarded helper to you're config to prevent accidental cleanup on unsaved changes:
function! CleanUnusedPlugins()
if &modified
echohl WarningMsg | echom "vimrc has unsaved changes — aborting cleanup" | echohl None
return
endif
echo "Scanning for orphaned plugins..."
silent! PlugClean
endfunction
nnoremap <leader>xc :call CleanUnusedPlugins()<CR>
Recovery Options
- Reinstall via declaration: Add the missing plugin line back into your config and run
:PlugInstall - Restore from backup: Extract the archived
plugged/directory or copy individual repos back
Troubleshooting Common Scenarios
Vim fails to start after cleanup
This usually indicates a dependency chain break—e.g., Plugin A was removed, but Plugin B relies on its functions. To resolve:
- Start Vim with
vim -u NONEto bypass config - Check error logs or use
:messagesto identify the missing component - Re-declare the required plugin and run
:PlugInstall
Temporarily suspend cleanup for specific plugins
Use the frozen key to exempt plugins from PlugClean:
Plug 'tpope/vim-fugitive', { 'frozen': 1 }
Frozen plugins won’t appear in :PlugClean output—even if omitted from future config edits.
Customize the prompt window layout
Control where the confirmation buffer opens using g:plug_window:
let g:plug_window = 'tabnew'
" or
let g:plug_window = 'vertical botright 50new'
Operational Recommendations
- Run
:PlugStatusbefore every:PlugCleansession - Avoid
:PlugClean!unless used inside version-controlled, tested automation - Track
.vimrcorinit.vimin Git to review plugin additions/removals over time - Pair cleanup with periodic
:PlugUpdateto ensure active plugins stay current