Does vim/vi/nano/etc lock files from being written to?

Pretty much as the title suggests, my colleague asserts that using a text editor instead of a parser (such as less) locks the file and in the case of logfiles makes it such that the logfile cannot be written to.

I have never heard of this before and just some initial testing finds this to not be the case. Additionally, I cannot find anything about this on a Google search to suggest where he might have got this idea from.

1

3 Answers

vim doesn't do this, but I'm not sure about the others. However, there's a very simple way to check this:

Open up two terminal windows (konsole, gnome-terminal, etc), and run the following commands in the first console:

touch ~/test.txt
vim ~/test.txt

Now, in your second console, type the following:

echo "This is a test" >> ~/test.txt

If the file is locked by vim, your command in the second console will fail, as the file cannot be written to. It won't fail. Repeat with whatever editor you want as step #2 in the first console.

Obviously, if you try and save any changes in your first console, you're wandering into uncharted territory, but as far as appending to log files go, this is safe.

Some editors, such as vim, gedit and others, create a "shadow" file when opening a file to indicate that the file is open in the editor. This is not locking, and does nothing to other applications on your system - it merely serves as a way for the editor to know that the file is open.

1

vim and vi do (an example of the effect). They don't prevent files from being written to, but throw a warning if you use another instance of vi/vim to edit the same file. nano and emacs (v24.3.1) doesn't (at least by default). I'll have to look up other editors. In fact, most decent editors are actually capable of watching for external changes to the file, suggesting strongly that they can't prevent external changes.

This maybe true if the application uses some locking function (either using O_EXLOCK on systems which support it, or flock, or something else on Windows).

5

I think I understand your Issue. I was trying to work around same issue myself

If you create a document using the command:

sudo nano nanoCreated.py 

The document is created however the document is locked. It can only be edited by nano editor. This will be an issue when you try to edit it with other editors.

However I don't think other editors such as Vim have the same issue.

To work around the issue of locking files:

  1. Create a file using the command: touch nameOfFile.py or with any other extension. You are also welcome to use any other commands such as : cat>nameOfFile.py

  2. You can then access the document using: sudo nano nameOfFile.py

  3. You can then edit the document from there and after you save it, it won't be locked and can be edited used by other text editors
    If you look at the image I've uploaded. I created files nameOfFile and nanoCreated.py files using sudo nano nameOfFile.py and both files were locked as you can see the lock icon. Those two files can only be edited by Nano. However I created the other files using the steps I provided and none of them were locked

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like