How To Save A File In Vim / Vi Without Root Permission With sudo

· >

How to Save a File in Vim/Vi Without Root Permission with Sudo

If you are working in Vim/Vi and don’t have root privileges, you can easily save the file using the sudo command. It is a quick and effective way to save a file without needing root permission.

What is VIM?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X.

VIM Features

Vim is rock stable and is continuously being developed to become even better. Among its features are:

  • persistent, multi-level undo tree
  • extensive plugin system
  • support for hundreds of programming languages and file formats
  • powerful search and replace
  • integrates with many tools

It is generally not a good idea to save a file in Vim/Vi with sudo unless you specifically need to save the file as the root user. This is because using sudo to edit a file can cause unintended consequences, such as making it difficult for other users to edit the file or changing the file’s ownership and permissions in unexpected ways.

Steps To Save A File In Vim/Vi Without Root Permission With Sudo

  • Start Vim/Vi: open the Terminal application and type in the command “vim file_name” to open your file in the editor.
  • Edit the file: edit the file as required.
  • Save the file using sudo: press Esc to exit from insert/edit mode and type :w !sudo tee % . This will save the file using sudo.
  • Enter the root password : a prompt will appear asking for the system’s root password. Enter the password and press Enter. The file will be saved.

This is an easy way to save a file in Vim/Vi without having root permissions. It is important to remember that it is not recommended to use sudo in general due to the risks of access and security. Therefore, it is best to use the above method only when needed and be sure to log out of the root account when you are done.


Therefore, to save a file in Vim/Vi without root permissions, use the command :w !sudo tee %. This will prompt you to enter the root password and allow you to save the file without having to be a root user. Remember to use this command only when necessary and log out of the root account after you are done.

Notify of
Inline Feedbacks
View all comments