1

I'm trying to use vimlsp server to help me write VimL, but it is prevented for me due to a parse problem with a literal carriage return (^M) char inside this code:

function! ChompCtrlM(string)
    return substitute(a:string, '^M$', '', '')
endfunction

It turns out I'm not actually even using this, so I'm able to dodge the bullet, but is there a hex escape string literal syntax for VimL? Google yielded nothing.

3
  • It's the actual control character? I don't think Vim should barf on that. It's common to use these (entered with ctrl-V then ctrl-M). Or is it supposed to be three character regex? ^ then M then $??? ;)
    – B Layer
    Nov 9, 2020 at 1:29
  • Yeah, it's the literal control char. It's in my vimrc, ergo vim (neither nvim) barf on it, but vim-lsp certainly doesn't know what to do about it, and to the extent that it halts parsing and effectively bricks it.
    – Steven Lu
    Nov 9, 2020 at 2:09
  • 1
    It might just be ok if I were to write it as "\r"?
    – Steven Lu
    Nov 9, 2020 at 2:10

1 Answer 1

2

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.

Your Answer

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

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