Question

I need to set title for vim window :setl stl=$title.

$ title='It is b.c';
$ vim $file -c "setl stl=${title}"; 

Unfortunately, set stl doesn't support some special character such as space. I have to escape the special chars first.

$ vim $file -c "setl stl=${title// /\ }";//It\ is\ b.c

What I want to know is about:

Is there any convenient method for this problem?Does vim has own escape function for stl value?

Was it helpful?

Solution

Vim does have an escape function. It is called escape(). (Take a look at :h escape()). You can use this in combination with execute (:h exec) to set stl properly.

vim $file -c "exec 'setl stl='.escape('${title}', ' ')"

The vim command for this specific example is

:echo escape('It is b.c', ' ')
It\ is\ b.c

So you can use execute to construct the string properly. Where . is used for string concatenation.

:exec 'setl stl=' . escape('It is b.c', ' ')

Simpler solution from @NikitaKouevda is the following. (:h let-&)

vim $file -c "let &l:stl = '${title}'"

And since vim can read environment variables itself you can simplify it further.

vim $file -c 'let &l:stl=$title'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top