开发者

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

I realized that in gvim Control+S as the :update 开发者_JS百科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


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.


: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.


From the help:

:[range]up[date][!] [++opt] [>>] [file]
                        Like ":write", but only write when the buffer has been
                        modified.  {not in Vi}


:help :update says:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜