Is it possible to start vim automatically with root permission if I start it in my home folder (or in a subdirectory of that folder)?

  • Do you intend to get rid of the the password prompt? And could you specify the use case? As generally speaking, stuff in your home should belong to you and not root. – k0pernikus Mar 19 '13 at 15:07
  • Yes, I don't want to type in my password everytime I change something on my files. – Dominik Berger Mar 19 '13 at 16:04
  • I still do not understand your use case, as what you describe still implies something terribly wrong with your file system. Again, files in your home directory should never need root rights to be edited. – k0pernikus Mar 19 '13 at 16:17

I suppose you could create a shortcut to run sudo vim ~/..

  • But please keep in mind what k0pernikus said. – guntbert Mar 19 '13 at 15:49
  • Generally speaking k0pernikus is correct. If the original question doesn't care about that, they could also use the same shortcut with a modification to the /etc/sudoers file to allow this particular sudo command to execute without being prompted for a password. – harrijs Mar 19 '13 at 16:00
  • Explain to me why the bash function was voted up when it does the exact same thing that my answer suggested. – harrijs Mar 19 '13 at 17:33
  • No one but the one person who upvoted can explain. – guntbert Mar 19 '13 at 17:38
  • Harrij, I've seen your answer. But as the qustion conained word "automatically", I sepose alias is not full answer and function is. – sorgel Mar 19 '13 at 18:46

Sadly, the description of use case is missing. But when you want to run different applications depending on target directory, bash functions are useful. For example, add to your ~/.bashrc

function vim () {
  if [[ "$PWD" == "$HOME"* ]];
    sudo vim $1
   vim $1

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.