You've picked an unfortunate example, as it's more easily achieved with:
let myvar .= 'bar'
Still, for more complicated edits, you could use the following commands:
Insert the variable into the buffer:
N.B. In the above,
<C-R> denotes a press of Ctrl-R, and
<CR> is a press of Return.
Perform your edits.
Reassign the contents of the line to the variable and delete the line:
:let myvar = getline('.') | d
Steps 1 & 3 can be converted into mappings:
:nnoremap <expr> <leader>ev "o\<C-R>=" . input('Variable: ', 'myvar', 'var') . "\<CR>"
:nnoremap <expr> <leader>av ':let ' . input('Variable: ', 'myvar', 'var') . " = getline('.') <bar> d\<CR>"
These prompt the user for which variable they want to edit/assign to, offering a default of
myvar, and using
variable completion so you don't have to type out the whole variable name.
If you always want to edit
myvar, you can just replace the call to
myvar. If you only want to be asked in the first mapping, then you could save the name of the variable selected in a variable and use that in the second mapping: at that point you'd probably be better off using a mapping that calls a function instead of attempting to cram it into a one-liner.
Creating a more polished, complete solution as described in the question is left as an exercise to the reader. You'd probably want to set up a function to create the new buffer and insert the contents of the variable, and a
BufWriteCmd autocommand to intercept the saving of that buffer (and only that buffer) and instead reassign to the variable.