can I do command substitutions in vimscript?

18 views
Skip to first unread message

ninjaaron

unread,
Nov 10, 2011, 5:24:13 PM11/10/11
to v...@vim.org
In bash, I can do something like this:

foo=`cat bar`

... and then the variable $foo will be the content of the file "bar."

I want to use the cat command to assign a value in my vimrc. I'm sure this
is possible, but I can't figure it out.

--
View this message in context: http://vim.1045645.n5.nabble.com/can-I-do-command-substitutions-in-vimscript-tp4982601p4982601.html
Sent from the Vim - General mailing list archive at Nabble.com.

Taylor Hedberg

unread,
Nov 10, 2011, 5:37:58 PM11/10/11
to vim...@googlegroups.com, v...@vim.org
I'm not 100% sure what you're trying to accomplish, but if you want to
actually invoke cat to dump the contents of a file "bar" into a Vim
variable "foo", you can do:

let foo = system('cat bar')

where the string passed to system() can be any arbitrary shell command.

Marc Weber

unread,
Nov 10, 2011, 5:47:24 PM11/10/11
to vim_use
Excerpts from ninjaaron's message of Thu Nov 10 23:24:13 +0100 2011:

> In bash, I can do something like this:
> foo=`cat bar`

let foo=system('cat bar')
or let foo = join(readfile('bar'),"\n") # lookup binary options

> I want to use the cat command to assign a value in my vimrc. I'm sure this
> is possible, but I can't figure it out.

Why do you want to use cat? ..

Marc Weber

ninjaaron

unread,
Nov 10, 2011, 8:35:49 PM11/10/11
to v...@vim.org
Thanks guys. I've been writing some scripts to change themes for my window
manager and my terminals together on the fly, and I made some vim
highlighting schemes to go along with them. With this command, I can echo
some text from my scripts to a file, and then have .vimrc read the file and
select the appropriate color scheme.

--
View this message in context: http://vim.1045645.n5.nabble.com/can-I-do-command-substitutions-in-vimscript-tp4982601p4982982.html

Reply all
Reply to author
Forward
0 new messages