I want to build an include
path from a environment variable on vim
. I want to keep only the instances which ends as /share/bin
on this variable, and change them to include
. I want to do this on .vimrc
, for this reason I used the vim substitute()
function.
As a test, I will use this variable:
export TEST=/afs/test/share/bin:/afs/test2/share/bin:/afs/test/bin:/afs/test3/share/bin:/cvmfs/x86_64-slc6-gcc47-opt/share:/cvmfs/x86_64-slc6-gcc47-opt/bin
The desired result for this example is:
TEST=/afs/test/include /afs/test2/include /afs/test3/include
While trying to achieve this result, I got stuck with the following two questions:
Question1
As I was having trouble to use the vim substitute()
method (explained below), I opened a file, say lala
, with the same text from the $TEST
environment variable:
/afs/test/share/bin:/afs/test2/share/bin:/afs/test/bin:/afs/test3/share/bin:/cvmfs/x86_64-slc6-gcc47-opt/share:/cvmfs/x86_64-slc6-gcc47-opt/bin
And used the command s/share\/bin:/include /g
to get:
/afs/test/include /afs/test2/include /afs/test/bin:/afs/test3/include /cvmfs/x86_64-slc6-gcc47-opt/share:/cvmfs/x86_64-slc6-gcc47-opt/bin
afterwards, I used the command %s#\(/.*include\)\(.*\)#\1#g
, resulting:
/afs/test/include /afs/test2/include /afs/test/bin:/afs/test3/include
But when I do on vim
:echo substitute(substitute($TEST,"share/bin:","include ","g"),"\(/.*include\)\(.*\)","\1","g")
I get:
/afs/test/include /afs/test2/include /afs/test/bin:/afs/test3/include /cvmfs/x86_64-slc6-gcc47-opt/share:/cvmfs/x86_64-slc6-gcc47-opt/bin
so, my first question is:
Why does not substitute()
give the same result when using substitute
?
Question2
And finally, I want to remove all instances similar to /afs/test/bin:
from:
/afs/test/include /afs/test2/include /afs/test/bin:/afs/test3/include
, that is, that do not have include on it. I was trying:
%s#\(/.\{-}include\)\@<=\(/| \)\{-}.\{-}:# #
But it is matching /afs/test2/include /afs/test/bin:
and giving as result:
/afs/test/include /afs/test3/include
How can I remove those text instances that did not have /share/bin?
"\(/.*include\)
which won't work as you seem to expect. This will be a literal ( character rather than using the\(
search feature. Either use single quotes or escape the backslash to keep it in the string. I'm not going to try deciphering the rest since the list functions in the current answer are so much nicer. :-)