You can take advantage of the way vim execute command when applied over a range.
Select your lines and do:
:'<,'>call system('mkdir -p '. fnamemodify(fnameescape(getline('.')),':h:p').' && touch '.fnamemodify(fnameescape(getline('.')),':p'))
This will apply the command
call system(...)
over every line you've selected.
The command is calling two external tools, mkdir
and touch
(you may change this if you work in windows).
mkdir -p
will create the directory (and subdirectories if necessary).
The argument is the escaped current line modified to show only the folder path (not the filename)
touch
will create the file
The argument is the escaped current line modified to show the path and the filename
You need to add the shellescape
function so that the filename is correctly escaped for the system call.
The fnamemodify
is handy if you want to modify the filename and path.
Note:
Alternatively to shellescape
, you can use one of the following:
- the
:S
modifier in fnamemodify
- enclose the string with double quotes
"
(thanks @Tommy\ A for the suggestion)
See:
:h v_:
:h system
:h getline()
:h shellescape()
:h fnamemodify()
:h ::S