Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between :update and :w in Vim?

Tags:

I realized that in gvim Control+S as the :update command. I always save using Ctrl+S, so I assume that :update is another way of saying "refresh the changes." Is :update basically the same as write :w? Will I have problems if I replace :update for :w!?

edit: I wanted to change :update to :w! becauase I have a file that says "ready-only please add !." And I thought this was the only solution

like image 861
alexchenco Avatar asked Jun 22 '10 12:06

alexchenco


People also ask

What does W do in Vim?

For example, :w saves your file and :q allows you to exit Vim.

What is the difference between appending and inserting in Vim?

The append command will put the cursor after the current position, while the insert command will put the cursor before it. Using the append command is like moving the cursor one character to the right, and using the insert command.

What does CW command do?

The cw command preprocesses any specified troff files containing English-language text to be typeset in the constant-width (CW) font. The cw command reads standard input if you do not specify a file or if you specify a - (minus sign) as one of the input file names. The cw command writes to standard output.


2 Answers

Here is another way to explain the difference between :write (shortcut :w) and :update (shortcut :up) :

Whenever we type :w, VIM will literally write the buffer to the file, no matter the buffer is empty or not. That MEANs it will update the timestamp of the file to the time :w typed , even if the contents of the file did NOT actually changed.

While with :up, just like the vim help manual says, the VIM will ONLY update the timestamp when the file has been changed.


For example, when I open a file just for reading, but I may accidentally (or, habitually) type :w or :wq, and if I do care about the timestamps of the file (i.e. last modified time), then there's no turning back. The following examples (in BASH shell) show the effects:

$ touch test.txt $ $ stat test.txt       File: `test.txt'   Size: 0           Blocks: 0          IO Block: 4096   regular empty file Device: 811h/2065d  Inode: 98828498    Links: 1 Access: (0664/-rw-rw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank) Access: 2014-03-15 22:30:52.159258193 +0800 Modify: 2014-03-15 22:30:52.159258193 +0800 Change: 2014-03-15 22:30:52.159258193 +0800 

Now let's VIM the file and try :up and :w command respectively:

$ vim test.txt 

Do not editing, just type :up and then :q

$ stat test.txt   File: `test.txt'   Size: 0           Blocks: 0          IO Block: 4096   regular empty file Device: 811h/2065d  Inode: 98828498    Links: 1 Access: (0664/-rw-rw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank) Access: 2014-03-15 22:33:10.002269244 +0800  <--- Different! Modify: 2014-03-15 22:30:52.159258193 +0800  <--- Didn't Change! Change: 2014-03-15 22:30:52.159258193 +0800  <--- Didn't Change! 

As you can see, only the Access time is changed, this is because we read(Access) the data in the file. But the Modify time & Change time are still the same.


Now let's vim again and use the :w command.

$ vim test.txt 

Do not editing, BUT this time type :w and then :q

$ stat test.txt   File: `test.txt'   Size: 0           Blocks: 0          IO Block: 4096   regular empty file Device: 811h/2065d  Inode: 98828538    Links: 1 Access: (0664/-rw-rw-r--)  Uid: (  514/    rank)   Gid: (  514/    rank) Access: 2014-03-15 22:40:26.728239153 +0800  <--- Different Again! Modify: 2014-03-15 22:40:26.728239153 +0800  <--- Changed! Change: 2014-03-15 22:40:26.728239153 +0800  <--- Changed! 

Now we can see the difference between :up and :w! The data of the file is Modified and the file status also Changed, although there is nothing really changed in the file.


So to avoid this, one can map the :w command to the :up command using :map :w :up.

like image 157
YaOzI Avatar answered Oct 10 '22 19:10

YaOzI


:help :update is pretty clear on that:

Like ":write", but only write when the buffer has been modified. 

So the file will only written if the contents of the buffer have actually been changed. So if you use :update (or press Ctrl+S in GVim) and there are no unsaved changes in the buffer, it won't do anything.

like image 34
jkramer Avatar answered Oct 10 '22 20:10

jkramer