tags:

views:

125

answers:

4

Basically I would like to know the difference between: :w and :w! or :wq and :wq!

+14  A: 

The ! qualifier tells Vim to force the operation. For example, if the file was read-only you would use :w! to write it anyway. If the file was modified and you wanted to quit without saving, you would use :q!. :wq! just means force write and quit in one command.

Amardeep
@Amardeep Yes I recently changed my mapping: Ctrl+S used to be :update and now is :w!, because I have a persistent ready-only file. Would I have problems in the future because of that change?
janoChen
@janoChen, yes, you'll get use to `Ctrl-S` and won't practice `:w!` enough :)
OscarRyz
@janoChen: Ctrl+S doesn't seem to have a default mapping so there should be no problem elsewhere. Your only issue might be accidentally saving over a read-only file that you didn't want to overwrite.
Amardeep
@janoChen you may have problems with using `<C-s>` in terminal, using `:w!` instead of `:up` should not cause much problem because you hardly hit `<C-s>` by accident.
ZyX
+3  A: 

The exclamation point usually means to force some action.

Another use of the exlamation mark: ! followed by some command executes this command directly from the editor.

:! ls /etc
eugene y
+2  A: 

It really depends on the command considered. Regarding the ones you have enumerated, it forces the command as others have already answered you.

However, there are other commands like :global, :map, :make, :silent, ..., where the bang (!) has other effects. Read their documentation:

:help help

(and we can give the bang any meaning we want in the commands we define)

Luc Hermitte
+1  A: 

Besides the situations where the exclamation point forces things, like writes, it will turn a command into a toggle command. So if I do:

:set cursorline

the line my cursor is on will be highlighted. I can turn it off with:

:set nocursorline

Or I could do:

:set cursorline!

That command flips between the two settings, off and on.

I turn cursor line highlighting off and on frequently, and the toggle command lets me do it with a simple function key mapping. Without the toggle, I would need either two mappings: one to turn it on, and a second to turn it off. Or I would have to write a function to determine whether the cursorline setting was on or off, and then turn on the opposite setting.

This works with, as far as I know, all command line settings that have on and off settings, like hlsearch, paste, cursorcolumn, number, insearch, etc.

Note also that the exclamation point will toggle the no version of the command. For example, you could also toggle the cursor line setting with:

:set nocursorline!
andrewl