Assigning multiple lines of text to a variable using the HERE document syntax

41 views
Skip to first unread message

Yegappan Lakshmanan

unread,
May 14, 2019, 12:22:54 PM5/14/19
to vim_dev
Hi,

When converting test29 into a new style test, I came across several
instances where multiple lines of text need to be stored in a variable.
I used the following method to do it:

new
insert
some text
some text
some text
.

let text = getline(1, '$')

You can also use a list literal to store the lines. But it is not readable.

It will be useful to support a HERE document syntax for assigning multiple
lines of text to a variable. Something along the lines of (similar to the shell
here document syntax):

let text <<HERE
some text
some text
HERE

Regards,
Yegappan

Bram Moolenaar

unread,
May 14, 2019, 3:46:12 PM5/14/19
to vim...@googlegroups.com, Yegappan Lakshmanan
Yeah, the list syntax is OK, but when the text contains quotes it gets a
bit messy.

We already use this for Python, Lua, etc.

--
BLACK KNIGHT: None shall pass.
ARTHUR: I have no quarrel with you, brave Sir knight, but I must cross
this bridge.
BLACK KNIGHT: Then you shall die.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Yegappan Lakshmanan

unread,
May 18, 2019, 1:33:57 AM5/18/19
to Bram Moolenaar, vim_dev
Hi Bram,

On Tue, May 14, 2019 at 12:46 PM Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>
> Yegappan wrote:
>
> > When converting test29 into a new style test, I came across several
> > instances where multiple lines of text need to be stored in a variable.
> > I used the following method to do it:
> >
> > new
> > insert
> > some text
> > some text
> > some text
> > .
> >
> > let text = getline(1, '$')
> >
> > You can also use a list literal to store the lines. But it is not readable.
> >
> > It will be useful to support a HERE document syntax for assigning multiple
> > lines of text to a variable. Something along the lines of (similar to the shell
> > here document syntax):
> >
> > let text <<HERE
> > some text
> > some text
> > HERE
>
> Yeah, the list syntax is OK, but when the text contains quotes it gets a
> bit messy.
>
> We already use this for Python, Lua, etc.
>

I have created the PR #4386 that implements the support for this.

https://github.com/vim/vim/pull/4386

- Yegappan

Bram Moolenaar

unread,
May 18, 2019, 7:05:51 AM5/18/19
to vim...@googlegroups.com, Yegappan Lakshmanan
Thanks!

I'm not sure why you added the feature to trim tabs. Why not all white
space? I suppose it's for things like this:

func GetColors()
let colors =<< END
blue
red
green
END
return colors
endfunc

This looks better:

func GetColors()
let colors =<< trim END
blue
red
green
END
return colors
endfunc

The "-" is cryptic, is this coming from another language? I would
prefer something easily understandable, like "trim".

I would think that when "trim" is used, then the indent of the first
line is removed, and the same amount is removed from following lines.
And the marker must have less indent. This allows for:

func GetNested()
let nested =<< trim END
header1
item1
item2
header2
item3
END
return nested
endfunc

Then three space of indent before the item lines is preserved.


--
"You're fired." (1980)
"You're laid off." (1985)
"You're downsized." (1990)
"You're rightsized." (1992)
(Scott Adams - The Dilbert principle)

Yegappan Lakshmanan

unread,
May 18, 2019, 9:56:54 AM5/18/19
to Bram Moolenaar, vim_dev
Hi Bram,
This is to be consistent with the here document syntax used in
shell scripts like bash:

https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents
http://tldp.org/LDP/abs/html/here-docs.html

>
> Why not all white space? I suppose it's for things like this:
>

Yes. This is for indenting the here document lines.

>
> func GetColors()
> let colors =<< END
> blue
> red
> green
> END
> return colors
> endfunc
>
> This looks better:
>
> func GetColors()
> let colors =<< trim END
> blue
> red
> green
> END
> return colors
> endfunc
>
> The "-" is cryptic, is this coming from another language? I would
> prefer something easily understandable, like "trim".
>

I wanted to keep it consistent with the shell script syntax. So I used "-".
I can change it to "trim".

>
> I would think that when "trim" is used, then the indent of the first
> line is removed, and the same amount is removed from following lines.
> And the marker must have less indent. This allows for:
>

I will make this change to remove the same amount of indentation from
all the lines in the here document as the first line.

- Yegappan

Bram Moolenaar

unread,
May 18, 2019, 12:52:32 PM5/18/19
to vim...@googlegroups.com, Yegappan Lakshmanan
Ah, that's where it comes from. It's based on the assumption that the
script is indented with tabs, and that the input does not start with any
tabs. I think both assumptions are not always valid. Or even rarily
valid if you look at Vim scripts.

> > Why not all white space? I suppose it's for things like this:
>
> Yes. This is for indenting the here document lines.
>
> >
> > func GetColors()
> > let colors =<< END
> > blue
> > red
> > green
> > END
> > return colors
> > endfunc
> >
> > This looks better:
> >
> > func GetColors()
> > let colors =<< trim END
> > blue
> > red
> > green
> > END
> > return colors
> > endfunc
> >
> > The "-" is cryptic, is this coming from another language? I would
> > prefer something easily understandable, like "trim".
>
> I wanted to keep it consistent with the shell script syntax. So I used "-".
> I can change it to "trim".

If we use "trim" then it's still similar to what bash does, but at the
same time we make clear that it's not exactly the same.

> > I would think that when "trim" is used, then the indent of the first
> > line is removed, and the same amount is removed from following lines.
> > And the marker must have less indent. This allows for:
>
> I will make this change to remove the same amount of indentation from
> all the lines in the here document as the first line.

Yeah, I think this works better, especially that it allows for indent in
the literal lines. Except for the first one.

--
Engineers will go without food and hygiene for days to solve a problem.
(Other times just because they forgot.)

Yegappan Lakshmanan

unread,
May 18, 2019, 3:19:23 PM5/18/19
to Bram Moolenaar, vim_dev
Hi Bram,

On Sat, May 18, 2019 at 9:52 AM Bram Moolenaar <Br...@moolenaar.net> wrote:
>
> Yegappan wrote:
>
> > > > > >
> > > > > > It will be useful to support a HERE document syntax for assigning multiple
> > > > > > lines of text to a variable. Something along the lines of (similar to the shell
> > > > > > here document syntax):
> > > > > >
> > > > > > let text <<HERE
> > > > > > some text
> > > > > > some text
> > > > > > HERE
> > > > >
> > > > > Yeah, the list syntax is OK, but when the text contains quotes it gets a
> > > > > bit messy.
> > > > >
> > > > > We already use this for Python, Lua, etc.
> > > > >
> > > >
> > > > I have created the PR #4386 that implements the support for this.
> > > > https://github.com/vim/vim/pull/4386
> > >
> > > Thanks!
> > >
> > > I'm not sure why you added the feature to trim tabs.
> > >
> >
> > This is to be consistent with the here document syntax used in
> > shell scripts like bash:
> >
> > https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Documents
> > http://tldp.org/LDP/abs/html/here-docs.html
>
> Ah, that's where it comes from. It's based on the assumption that the
> script is indented with tabs, and that the input does not start with any
> tabs. I think both assumptions are not always valid. Or even rarely
I have updated the PR to use "trim" instead of "-" and also to remove
all the leading indentation matching the indentation used in the 'let' line.

- Yegappan
Reply all
Reply to author
Forward
0 new messages