I don't think you said whether a "destructive" approach was acceptable or not. That is, the change is not superficial in this solution (although you could "undo" out of it). In case a non-destructive option is not forthcoming I offer this...
If you're in a Unix-like environment1 (Linux, Mac, Windows+Cygwin) then you can send output from an appropriate shell command through a Vim filter like so...
:g/\* <<[^>]\+>>/ exe '.!head -1 "' . substitute(getline("."), "^\* <<\\([^>]\\+\\)>>", "\\1.txt", "") . '"'
Every line with the pattern you indicated will get processed so the proper filename is generated and then that is sent through a command (head -1
in this case) to get the first line. Thanks to the filter :.!
the result will replace the current line.
You can either run this manually (probably want to map it to a shortcut, though) or you can put it in an autocommand if there's a particular file type(s) that you always want to apply it to.
I'll break it down in a bit more detail later.
1If you're on straight Windows and know some Windows console command that extracts the first line of a file this can be made to work there, too.
Update: Got to thinking how the above could be made non-destructive. Christian Brabandt mentioned use of conceal
in a comment and that's where my thinking is, too.
The change to the above is to append the first line of text rather than have it replace the original line. The original part is hidden with conceal.
set conceallevel=3
syn match Conceal /^\* <<[^>]*>><</hs=s+2,he=e-2 conceal
" would prefer to do this with a single global but for now...
g/\* <<[^>]\+>>/ s/$/\='<<' . system('head -1 "' . substitute(getline("."), "^\* <<\\([^>]\\+\\)>>", "\\1.txt", "") . '"')
g/\* <<[^>]\+>>/ norm! gJA>>
(BTW, upon rereading the question I think the idea is to have the first line of text just replace the text between << >>
rather than replace the entire line. This part assumes that's correct.)
To reverse the effect just use a global command to delete everything from the second "<<" to the end of the line. I can show those commands but I'm waiting to hear from OP if this is even what they have in mind.