You can normally use double quoted strings for this purpose. They accept a range of backslash escape sequences to produce special characters without having to input the special character literals into your buffer.
The character that shows as ^M
is the carriage return and you can enter it as "\r"
. You can also use key names in double quoted backspace sequences. The key notation for carriage return is <CR>
, so you can produce it in a string with "\<CR>"
. Both "\r"
and "\<CR>"
are equivalent and produce the same string.
See :help expr-quote
for a full list of backslash escape sequences that are recognized in a double quoted string.
In your specific case, you could rewrite your function as:
function! ChompCtrlM(string)
return substitute(a:string, "\r$", '', '')
endfunction
This will hopefully avoid the conflict with vimlsp that you found with the special character literal.
ctrl-V
thenctrl-M
). Or is it supposed to be three character regex?^
thenM
then$
??? ;)"\r"
?