I have a function in my bashrc that checks if a file is writable with [ -w "$1" ]
. If it is, I open it with vim normally. If it's not, I open with sudo via sudo env HOME="$HOME" vim -u ~/.vimrc "$1"
to make sure vim sources my .vimrc
and colorscheme correctly. I want to print a warning message on the message line (whatever that line is called) when I do this, so I know I've had to use sudo. I wrote a vimrc function to display the message how I want:
"sudo warning label:
function! Sudowarn ()
hi TempColor ctermfg=255 ctermbg=88
echohl TempColor
echo "Automatically sudo'd to have write access!"
echohl None
endfunction
My problem is that I don't know how to call this function when the file loads only when I've sudo'd. I know vim knows about the $USER
variable ("root" when sudo'd, "jeremysprofile" when not), and I know autocmd
has a User option but I don't know how to use these to solve my problem.
If someone can at least tell me what I should be doing to solve my problem instead of asking internet questions, it would be greatly appreciated.
SUDO_USER
environment variable, as well as some others. Tryenv | grep SUDO
. You can access all environment variables from Vim with$VARNAME
. I'm sure I answered this question (or something very similar) before in more detail, but I can't find it right now :-($USER
works as I want, i.e.,:echom $USER
gives "root" when I've used sudo and my username otherwise. I just don't know how to use that in an autocmd =(if
, right?autocmd BufRead * if $USER == "root" | call Sudowarn() | endif
(Arguably better to put thatif
in theSudowarn()
function though) Or am I misunderstanding what you mean?BufReadPost
) the file contents don't appear until after I've hit enter; the messages seems to block that.cmdheight
settings. if you want to post an answer, I'll accept it, otherwise I'll post an answer tomorrow.