Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

With the following piece of code I get the permissions for an existing file:

augroup Get_file_perm
  autocmd BufWinEnter,FileChangedShell * let w:file_perm=getfperm(expand('%:p'))
augroup END
" Output, e.g.: rw-rw-r--
set statusline=%{w:file_perm}

Unfortunately the getfperm function returns the empty string ("") for the current file path ('%:p') if it doesn't exist. I had expected the file permissions calculated from the umask.

share|improve this question
Doesn't %:p just return the directory the file is currently in? Try just %, instead. Also, what's wrong with (from command mode): :!ls -l % –  Alex Leach Dec 12 '12 at 18:27
With the autochdir option enabled expand('%') expands only to the file's basename. With the :p modifier the absolute path is coerced. :h expand is your friend. –  Tim Friske Dec 12 '12 at 20:16
Also I want to see the file permissions at first glance, without any key stroke. –  Tim Friske Dec 12 '12 at 20:22
add comment

1 Answer

To my knowledge there doesn't exist a Vim function that accomplishes what you're asking for. But I came up with my s:Get_file_perm function whose result I assign to the w:file_perm variable:

" ...
let w:file_perm=<sid>Get_file_perm()
" ...
function! s:Get_file_perm()
  let a=getfperm(expand('%:p'))
  if strlen(a)
    return a
     let b=printf("%o", xor(0777,system("umask")))
     let c=""
     for d in [0, 1, 2]
       let c.=and(b[d], 4) ? "r" : "-"
       let c.=and(b[d], 2) ? "w" : "-"
       let c.=and(b[d], 1) ? "x" : "-"
     return c

The function checks (if) to see if the permissions (getfperm) for the current file path ('%:p') exist (strlen). In the case of a new file ("") the else-block bitwise subtracts (xor) the octal umask from the octal literal 0777 (rwxrwxrwx) – all permission bits set.

For the owner- (0) , group- (1) and permissions for others (2) it iteratively (for) checks for each read- (4), write- (2) and execute (1) bit that it is set (and). If the corresponding bit is set it appends (.=) "r", "w" and "x" to the c variable, respectively. Else "-" is appended to symbolize that the respective operation is not permitted.

share|improve this answer
You're talking to yourself, man.. :-/ –  Alex Leach Dec 12 '12 at 18:44
Its my second I. Sorry bro. –  Tim Friske Dec 12 '12 at 20:10
Keep talking, sometimes that is the best way to solve a problem if nobody else answers the question ;) But on the other hand sometimes it is better just update the question with more information.... –  Johan Dec 20 '12 at 6:11
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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