Search code examples
vimadminstatus

How to check if vim window has admin rights and how to add it to the statusline?


I noted that:
Vim with admin rights can only open files from program with admin rights.
Vim without admin rights can only open files from programs without admin rights.

This makes it that I often have two windows of more of vim open with different rights.
I often don't know if the one I use has admin rights or not.

Is it possible to check if a vim window has admin rights or not?

I would like to add the admin status to the statusline, something like this:

function! CheckAdminMode()
  if &admin | return 'Admin' | else | return '' | endif
endfunction

set statusline+=%*\ %{&admin}

Is this possible?


Solution

  • If you know a shell command you can use system(). E.g. for most home linux setups

    if system('echo $EUID') == 0 " Implicit type cast, string->int for left operand
        return 'Root'
    else
        return ''
    endif
    

    ($EUID is normally a shell special variable not present in exported environment thus using if $EUID == 0 is not possible) or, faster,

    py import os
    return (pyeval('os.geteuid()') == 0) ? 'Root' : ''
    

    . As you are asking for admin and not root I assume you are not on linux, thus I can’t help you with the command to check. You can always use indirect check: test whether you have right to write to some directory writeable only for administrators:

    " filewritable() reports 2 for writable directories which is also true
    return filewritable('C:\Windows\System32') ? 'Admin' : ''
    

    .