Vim - writing to filename in vimscript variable

vim

Solution

Vimscript evaluation rules

Vimscript is evaluated exactly like the Ex commands typed in the `:` command-line. There were no variables in `ex`, so there's no way to specify them. When typing a command interactively, you'd probably use `<C-R>=` to insert variable contents:

:sleep <C-R>=timetowait<CR>m<CR>

... but in a script, `:execute` must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables:

execute 'sleep' timetowait . 'm'

Like `:execute` above, the `:echo[msg` command is particular in that it takes a variable argument, whereas most commands (like `:write`) do not, and treat the argument(s) literally.

Your particular problem

As above, your issue is best resolved via `execute`:

:execute 'write' foo

However, note that if `foo` contains any regular filename, it still needs to be escaped for the `:write` function, which understands some special notation (e.g. `%` stands for the current buffer name), and likes to have spaces escaped:

:execute 'write' fnameescape(foo)

Problem

I'm doing something like ``` :let foo="bar" :echom foo bar :w foo "foo" [New File] 0 lines, 0 characters written ``` I am expecting/hoping to write a file named "bar", not a file named "foo". Assuming that I have a string stored in a variable "foo", how can I write the current buffer to a file with the name being that string? As an aside, can someone explain what `:w foo` and `:echom foo` are doing different with regards to `foo`?

Original source