[SOLVED] Vimscript: using substitute() on a variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If I paste the output of :ls into a buffer, the command
Code:
:%s/.*\(\".*\"\).*/\1/
reduces that output to just the file paths. Wanting to achieve that result in a variable, I did
Code:
:redir => x|silent :ls|redir END
:let y = substitute(x, ".*\(\".*\"\).*", "\1", "g")
which accomplished absolutely nothing, y is identical to x. I've tried umpteen variations on that substitute command, getting only the same result, or a bunch of error messages. How should I be specifying it?
I was following the examples used in :h substitute() which both use ". But if I do
Code:
:let y = substitute(x, '.*\(".*"\).*', '\1', "g")
I do get output, but only the match to the last line in the variable x. I tried concatenating the output by
Code:
:let y = y . substitute(x, '.*\(".*"\).*', '\1', "g")
but that just appended the same output again to variable y. But the question has changed. How do I get every line of output from that substitute() command?
Last edited by porphyry5; 10-08-2011 at 02:49 PM.
Reason: partial progress
I assume you surely meant :!ls. What I don't get is the regex that you are using. So, you are matching anything .* followed by quotation followed by anything .* followed by quotation and then match anything (.*). You are substituting whatever you are getting between the quotation marks. Just out of curiosity, could you please give me what/ how are you reducing the output from !ls to file paths?
Quote:
Originally Posted by porphyry5
If I paste the output of :ls into a buffer, the command
Code:
:%s/.*\(\".*\"\).*/\1/
reduces that output to just the file paths. Wanting to achieve that result in a variable, I did
Code:
:redir => x|silent :ls|redir END
:let y = substitute(x, ".*\(\".*\"\).*", "\1", "g")
which accomplished absolutely nothing, y is identical to x. I've tried umpteen variations on that substitute command, getting only the same result, or a bunch of error messages. How should I be specifying it?
I was following the examples used in :h substitute() which both use ". But if I do
Code:
:let y = substitute(x, '.*\(".*"\).*', '\1', "g")
I do get output, but only the match to the last line in the variable x. I tried concatenating the output by
Code:
:let y = y . substitute(x, '.*\(".*"\).*', '\1', "g")
but that just appended the same output again to variable y. But the question has changed. How do I get every line of output from that substitute() command?
I assume you surely meant :!ls. What I don't get is the regex that you are using. So, you are matching anything .* followed by quotation followed by anything .* followed by quotation and then match anything (.*). You are substituting whatever you are getting between the quotation marks. Just out of curiosity, could you please give me what/ how are you reducing the output from !ls to file paths?
No, I did not mean :!ls; that accesses the linux ls, which, at least on my system, delivers only the terminating name. I meant vim's ls, which, among other things, returns "the path used to open that file in vim". As I only ever use a single instance of vim, always started the same way in the same working directory, the file paths returned by vim's ls are sufficient for my purpose. The regex, used in the :substitute command, delivers the enquoted file path; used in the substitute() function it returns only the last occurrence of such.
Following is my solution.
The variable x contains
Code:
1 # "~/Session.vim" line 5
2 "~/.vimrc" line 34
3 ".vim/vscripts/makesess.vim" line 4
4 "~/Documents/vimcht" line 62
5 "~/.fluxbox/startup" line 5
6 "~/Documents/Notes" line 2604
7 "~/Documents/bashcht" line 21
8 %a "junk" line 10
By my reading of :h /[], particularly "...Without the "\_" or "\n" the collection does not match an end-of- line...", then
Code:
:let y = ""
:let y = substitute(x, '[.]*\("[.]*"\)[.]*', '\1', "g")
should have done the job. But it delivered
Code:
"junk"
just as it did without the [].
I then tried
Code:
:let y = ""
:let y = substitute(x, '[^\n]*\("[^\n]*"\)[^\n]*', '\1', "g")
and got exactly the same result.
Clearly vim does not use the actual newline character internally, for variables as well as registers. So it treats a variable as a single line. Which led me to this solution
Code:
:let y = substitute(x, '[^"]*\("[^"]\+"\)[^"]*', ':tabe \1\n', 'g')
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.