How to effectively work with multiple files in Vim
I’ve started using Vim to develop Perl scripts and am finding it very powerful. I can open multiple files at once with:
vi main.pl maintenance.pl
and navigate between them using:
:n
:prev
I can check which files are open with:
:args
However, when I try to add a new file with:
:n test.pl
it replaces my current file list instead of adding to it. When I check with :args, only test.pl is open.
How can I add and remove files in my args list while preserving my existing file list?
To effectively add files to your argument list in Vim without replacing the existing one, you should use :argadd instead of :n. The :n command switches to the next file in the argument list and replaces the current list if you specify a new file. With :argadd, you can append files to your existing argument list, and use :argdelete to remove specific files when needed.
Contents
- Understanding Vim’s Argument List
- Adding Files to the Argument List
- Removing Files from the Argument List
- Working with the Argument List
- Practical Examples and Workflows
- Advanced Tips and Tricks
Understanding Vim’s Argument List
The argument list in Vim is a powerful feature that allows you to specify a set of files and then run commands on those files collectively source. It’s distinct from the buffer list - while the argument list represents files you’re working with as a group, buffers are individual open files in memory.
When you start Vim with multiple files like vi main.pl maintenance.pl, these files are automatically added to the argument list. The argument list serves as the foundation for batch operations like search and replace across multiple files.
Key difference: The argument list is a curated group of files for specific operations, while the buffer list contains all currently loaded files in memory.
Adding Files to the Argument List
To add files to your current argument list without replacing it, use the :argadd command (short form :arga):
:argadd test.pl
This adds test.pl to your existing argument list. You can verify this with :args which will now show both your original files and test.pl.
Adding multiple files at once:
:argadd script1.pl script2.pl script3.pl
Adding files matching a pattern:
:argadd *.pl " Add all .pl files to the argument list
Adding files from the current buffer list:
:argadd % " Add current buffer to argument list
:buffdo argadd % " Add all buffers to argument list
The :argadd command is specifically designed to preserve your existing argument list while adding new files, which is exactly what you need when working with multiple files in your development workflow.
Removing Files from the Argument List
To remove files from your argument list, use :argdelete (short form :argd):
:argdelete test.pl " Remove test.pl from argument list
Removing multiple files:
:argdelete script1.pl script2.pl
Removing files matching a pattern:
:argdelete *.bak " Remove all .bak files from argument list
Remove all files from argument list:
:argdelete * " Clear the entire argument list
Remove current file from argument list:
:argdelete # " Remove current file from argument list
The :argdelete command gives you precise control over which files remain in your argument list, allowing you to focus only on the files relevant to your current task.
Working with the Argument List
Once you have files in your argument list, here are the key commands for working with them:
Navigation:
:next " or :n - go to next file in argument list
:prev " or :N - go to previous file in argument list
:first " or :fir - go to first file in argument list
:last " or :la - go to last file in argument list
Viewing and managing:
:args " Show current argument list
:argdo " Execute command on all files in argument list
Example with :argdo:
:argdo %s/old/new/gc " Interactive find-and-replace across all files in argument list
:argdo w " Save all files in argument list
:argdo update " Save all modified files in argument list
The argument list becomes particularly powerful when combined with :argdo, which lets you execute any Vim command across all files in your argument list.
Practical Examples and Workflows
Perl development workflow:
" Start with your main files
vi main.pl maintenance.pl utils.pl
" Add related test files
:argadd t/*.pl
" Add configuration files
:argadd config/*.pl
" Now you have all relevant files in argument list
:args " Verify your complete list
" Search for a function across all files
:argdo /sub my_function/
" Replace a variable name across all files
:argdo %s/$old_var/$new_var/g
" Clean up - remove test files when done
:argdelete t/*.pl
Batch file processing:
" Add all files matching a pattern
:argadd lib/*.pl
" Process files
:argdo %s/TODO/FIXME/g
" Add more files as you discover them
:argadd bin/*.pl
" Remove files you've finished with
:argdelete lib/old_module.pl
These workflows demonstrate how the argument list can adapt to your development needs, allowing you to dynamically manage which files are included in your current working set.
Advanced Tips and Tricks
Using external commands to populate argument list:
:args `find . -name "*.pl" -type f` " Find all .pl files and add to argument list
:args `grep -l "important_function" *.pl` " Find files containing a function
Working with duplicate files:
:argdedupe " Remove duplicate filenames from argument list
Converting between argument list and quickfix list:
" Convert argument list to quickfix list
:vimgrep /pattern/ #
:copen
" Convert quickfix list to argument list
:cexpr []
:args
Plugin enhancement:
For advanced argument list management, consider the ArgsAndMore plugin which extends Vim’s built-in functionality with features like:
- Filtering the argument list
- Adding files not matching a pattern
- Converting between arguments and quickfix lists
The argument list is a powerful Vim feature that, when used correctly, can significantly improve your productivity when working with multiple files. By understanding the distinction between :n (which switches and replaces) and :argadd (which appends), you can maintain a flexible workflow that adapts to your development needs.
Sources
- Vim Arglist - vimgenius
- Vim 101: Search and Replace on Multiple Files - Medium
- Vim’s useful lists - David Winterbottom
- ArgsAndMore Plugin - vim online
- Manage small groups of related files with Vim’s argument list - Tom McFarlin
- Vim: editing.txt documentation
Conclusion
Mastering Vim’s argument list management transforms how you work with multiple files. Key takeaways include:
- Use
:argaddto append files to your existing argument list instead of:n - Use
:argdeleteto remove specific files when they’re no longer needed - Combine
:argdowith any Vim command for batch operations across multiple files - The argument list is separate from buffers, allowing for more focused file management
- External commands can populate your argument list for dynamic file selection
Start with simple :argadd commands and gradually incorporate :argdo for batch operations. As you become comfortable, explore advanced techniques like pattern-based file management and external command integration. The argument list is one of Vim’s most underutilized features that can dramatically improve your multi-file development workflow.