In a vimrc file I want to preload register C with a string I regularly need when coding. The string has a newline in it but no success in getting it to work properly.

Here is an example vimrc snippet of one of my attempts:

" Powershell block comment preload
let @c="<# Start COMMENT `n # End COMMENT #>"

in addition to '`n', I have tried `r`n, <CR>, maybe some others.

How do I represent a newline character in a 'let' statement string in vimrc, and have it work like a newline/CR when called upon to be inserted into the code (essentially inserted as a multi-line string)?

For that matter, in general, how do I represent any special character in a set/let statement string in a vimrc, and have it operate as intended when you ask for the value of that register?

1 Answer 1


You can use \n inside a double quoted string for a new line.

See :help expr-quote for more details of the special sequences available inside a double quoted string. The special sequences inside double quoted strings start with a backslash.

In your specific example, this should do:

" Powershell block comment preload
let @c = "<# Start COMMENT \n # End COMMENT #>"

You might want to adjust spacing before/after the newline, to prevent leading/trailing spaces as necessary.

  • 2
    Who would have thought the vim help key word was 'expr-quote' to get this info...not to mention searching online using keywords like vimrc, newline, statement strings, special characters would yield no results. Commented Mar 11, 2022 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.