How do I paste multiline clipboard text as a comma-separated list in Insert mode?
Answer
<C-r>=substitute(getreg('+'), '\n\+', ', ', 'g')<CR>
Explanation
When you paste from the system clipboard into code or config, multiline text often needs to be flattened first. The expression register lets you compute the pasted value at insertion time, so you can transform clipboard content without leaving Insert mode or creating temporary edits.
How it works
<C-r>=substitute(getreg('+'), '\n\+', ', ', 'g')<CR>
<C-r>=opens the expression register in Insert mode.getreg('+')reads the system clipboard register.substitute(..., '\n\+', ', ', 'g')replaces one-or-more newlines with,.<CR>evaluates the expression and inserts the transformed result.
This is useful when converting copied rows into a comma-separated inline list (for SQL IN (...), log filters, feature-flag lists, or shell arguments).
Example
Clipboard content:
api
billing
auth
After running the command in Insert mode:
api, billing, auth
Tips
- Swap
', 'for' | 'or' 'depending on your target format. - Use
getreg('"')instead ofgetreg('+')if you prefer Vim's unnamed register. - For safer shell usage, wrap each item with quotes in the expression.