Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I make open files as readonly and nomodifiable, if they're in a certain folder hierarchy, but do NOT contain a certain extension?

For example, if I open files on /server/(production|uat)/* I want to be prompted whether to edit the file, or just view it. An exception might be needed as well--such as if the file contains the prefix temp.

share|improve this question
You could replace your vim executable (at 'which vim') with a bash script that executes vim -R -M instead of vim if your conditions are met – user247679 Aug 28 '13 at 21:01

Put this into your .vimrc file:

autocmd BufReadPre,BufNewFile /Taboo/Dir/*  setlocal readonly nomodifiable
autocmd! BufReadPre,BufNewFile /Taboo/Dir/*/temp*.txt   setlocal noreadonly modifiable
share|improve this answer

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.