Reputation: 4470
I have macro lines in my vimrc file like:
let @i = "oif (^[a) {^[a^M}^[k0f)"
Where the "^[" and "^M" are characters which were entered via "Ctrl-V" in the insert mode, so they must be Escape and Newline (I'm using Linux, and Vim is configured to use '\n' newlines, but what this character is exactly in the vimrc file I don't know).
When I search for such a line with grep -B 2 'i = "' ~/.vimrc
I see a completely broken output:
hi Comment cterm=none ctermbg=226 ctermfg=009 guibg=#ffff00 guifg=#ff0000
}0f)"i = "oif () {
While I would want:
hi Comment cterm=none ctermbg=226 ctermfg=009 guibg=#ffff00 guifg=#ff0000
let @i = "oif (^[a) {^[a^M}^[k0f)"
Or at least something similar, with the third line starting with let @i
. But I see }0f)"i = "oif () {
and there is no sign of let @i
.
I tried to investigate a bit and found that it works correctly until the line has the "^M" character.
Could anyone please say how to fix it, or at least explain what is going there?
Upvotes: 1
Views: 96
Reputation: 650
grep
is sending Escape
sequences
(such as ^[a
and ^[k
; see Amadan's comment
below)
and a control character
(^M
) to the terminal and they're messing up with the output, which is
revealed when filtered by other commands like:
grep -B 2 'i = "' ~/.vimrc | cat -v
or
grep -B 2 'i = "' ~/.vimrc | less
This is what cat's man page says about -v
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
and here's also the relevant snipped from less's man page:
Control and binary characters are displayed in standout (reverse video). Each
such character is displayed in caret notation if possible (e.g. ^A for control-
A). Caret notation is used only if inverting the 0100 bit results in a normal
printable character. Otherwise, the character is displayed as a hex number in
angle brackets.
These control sequences are usually used for sending non-printable characters to the terminal, which allow users to customize the terminal output by adding (foreground and background) colors, moving the cursor around, among others.
Invalid escape sequences like ^[a
and ^[k
are ignored so you'll get the
same issue if the macro were defined like this:
let @i = "oif () {^M}0f)"
^M
is called carriage
return and it tells the terminal
to move the cursor to the beginning of the line before printing the rest of the
output (which will override the previous output). That's similar to typing 0
in normal mode, pressing R
and then entering
}0f)"
by yourself on:
let @i = "oif () {^M}0f)"
I hope this answer is helpful.
Upvotes: 2