3 ms·
> Insert the contents of a file: :r <filename> The file name can be a pipe instead. Let’s say you have a Makefile based project and the dependencies aren’t as
by wyoung2 8y ago
> Insert the contents of a file: :r <filename>
The file name can be a pipe instead.
Let’s say you have a Makefile based project and the dependencies aren’t as complete as they ought to be. With your cursor on this line in the Makefile:
SOURCES = \
You can say
:r !ls src/*.c
That will put the complete list of extant C source files into the file at that position, complete with the “src/“ path prefix. Then to format the list properly for “make” syntax, use Shift-V to select the list, hit the right angle bracket (“>”) once to indent it a level, then say:
:s/$/ \/
This trick of replacing the start (“^”) or end (“$”) of a line is very often helpful. Here I’ve used it to ask Vim to put a space and backslash after every line to allow the SOURCES variable definition to continue from one line to the next.
(This will look like “:’<,’>s...” in your editor, with the bit between the colon and s being inserted by Vim; it means “front the start of the selection to the end.”)
Remove the final backslash and it’s done.
The advantage of working this way is that you aren’t manually transcribing things the computer already knows and can report on command.
The possibilities here are vast. For instance, if your C source files are buried under subdirectories of `src`, say this instead:
:r !find src -name \*.c