Following the vim manual and also some threads on this site, I can set the color of my cursor to orange in insert mode and red otherwise, with the following lines in my .vimrc file.

" use an orange cursor in insert mode
let &t_SI = "\<Esc>]12;orange\x7"
"use a red cursor otherwise
let &t_EI = "\<Esc>]12;red\x7"
silent !echo -ne "\033]12;red\007"
autocmd VimLeave * silent !echo -ne "\033]112\007"

The problem is that when run vi from an xterm, then when I exit vi, the string -ne is echo-ed in the terminal, which is quite annoying. The source is the echo commands in the last two lines of the above code fragment. If I comment the two lines out the problem doesn't arise. But I need them for the cursor color changes to work in vi and revert back to black when exiting. What causes the -ne on exit, and how can I prevent it? Thanks for any advice


Ditch echo -ne, for life. Use printf.

The Echo command is a portability nightmare; POSIX does not even specify the -e flag and actually says

If the first operand is -n, or if any of the operands contain a <backslash> character, the results are implementation-defined.

So when you issue echo without knowing exactly which Echo will be called — and that will depend on the running shell or if it is execed directly, not only on the system* itself —, you are rolling a die. It may not understand -ne as two flags and print it verbatim: That's what you observed.

Printf, on the other hand, is required to interpret \nnn as the octal character "nnn". In your case, \033 is the escape character and \007 is the bell character; All else is to be taken literally. Therefore,

silent !printf '\%s' '\033]12;red\007'
autocmd VimLeave * silent !printf '\%s' '\033]112\007'
  1. You actually don't even need the format specifier %s in this case, but it is good practice to always put it in (otherwise you may have a surprise if you insert % in the argument, for example).

  2. In Vim the % itself must be escaped, otherwise it gets substituted with the current file's name.

*In my system I could only manage to reproduce your issue by inserting set shell=/bin/dash and set t_te= at the top of the sample ~/.vimrc you provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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