Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of the exclamation mark ('!') in Vim's command line after certain commands like ":w!"?

Tags:

vim

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

like image 216
alexchenco Avatar asked Jun 22 '10 15:06

alexchenco


People also ask

What does exclamation mark do in terminal?

Overview. If we work a lot with the interactive Linux command line, chances are we might have come across the exclamation symbol. On most shells, this symbol is used to rerun previously executed commands through history expansion, thereby, increasing our productivity.

What does two exclamation marks mean in Linux?

The double factorial, symbolized by two exclamation marks (!!), is a quantity defined for all integer s greater than or equal to -1. For an even integer n , the double factorial is the product of all even integers less than or equal to n but greater than or equal to 2.

What is the exclamation mark?

The exclamation mark (!), known informally as a bang or a shriek, is used at the end of a sentence or a short phrase which expresses very strong feeling. Here are some examples: What a lovely view you have here! That's fantastic!

How do you grep with an exclamation mark?

at the start of a pattern, by either using a backslash ( \! ), or otherwise making it not the first character of the regex ( /[!] , for example).


3 Answers

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.

like image 85
Amardeep AC9MF Avatar answered Oct 06 '22 01:10

Amardeep AC9MF


In your examples the exclamation point means to force the action (e.g. :w! will overwrite an existing file and :q! will quit without saving).

That said, there are many other uses depending on the command, e.g.:

  • :set <option>! toggles a boolean option, e.g. :set number!

  • ! followed by some shell command executes that command directly from the editor, e.g. :! ls /etc

  • :w !cmd pipes the contents of the current buffer to the command cmd, e.g. :w !sudo tee % (a.k.a. the write with sudo trick).

like image 38
Eugene Yarmash Avatar answered Oct 06 '22 00:10

Eugene Yarmash


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!
like image 40
Andrew Langman Avatar answered Oct 05 '22 23:10

Andrew Langman