Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some commands in vim require a colon while some don't?

Tags:

vim

Some of the commands in vim are given by first typing a colon (:) eg . :wq for saving a file and quitting . While some of the commands don't require a colon for example the Replace command (R). I want to know what is the difference between the two approaches ? Is there any specific rule as to which ones require a colon and which ones don't ?

like image 960
Geek Avatar asked Dec 27 '12 08:12

Geek


People also ask

What are the two main modes in Vim and what is each used for?

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

What does exclamation do in Vim?

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!

What is %! In Vim?

At the beginning of a Vim command line statement, you can specify a range over which to operate. % is a common range for specifying the whole file, shorthand for 1,$ , i.e. line 1 to end of file.


2 Answers

You have to look into the history of vi, the predecessor of Vim, for an explanation. A long time ago, when text editing had to be done with a keyboard and attached printer (called a terminal), there was no mouse, no display other than the paper, and therefore, little interactivity. Editing consisted of short, mnemonic commands via an editor called ex. You issued a command addressing one or several lines (e.g. :substitute/foo/bar), and the editor obeyed. In case you were not sure about the command's effects, you could :print some lines.

Time passed, video terminals appeared, and the vi editor incorporated the ex commands (because they were useful and the programmers were used to them), but introduced more interactive commands like delete (x), insert (i), and so on. The ex commands are still available in command-line mode, which is started with :, and concluded with Enter.

Vi and Vim are special in this regard, because they have these different modes where the same keys mean different things depending on which mode one is in. To become proficient in Vim, you have to learn about the different modes, and how to best use them to achieve your editing goals.

:help vim-modes gives you a starting point into the excellent and comprehensive help facilities.

like image 192
Ingo Karkat Avatar answered Sep 30 '22 15:09

Ingo Karkat


The commands that "don't require" a colon are called "normal (mode) commands".

The commands that "require" a colon are called "Ex commands".

Vim, being a modal editor, has many commands that are contextual to the mode you are in. The most obvious effect is that hitting the same key in different contexts may produce different results.

In insert mode, most keys on your keyboard are used to actually input characters into your document.

You have to switch to normal mode to yank, put, delete, move your cursor around… normal mode is where you do the laser-focused editing Vim is famous for and use commands like dcggsi/.* and so on.

You enter command-line mode by hitting : in normal/*visual* mode. It is typically used for two things:

  • perform administrative tasks (writing to disk, switching buffers, opening files…)
  • use cool editing commands like :m10 or :t1 or :g/foo/d

The many commands that you can use in this mode are (very powerful) remnants of Vim's past and are called Ex commands.

In short, neither normal mode commands nor Ex commands start with a colon. The colon is simply used to change modes.

like image 27
romainl Avatar answered Sep 30 '22 15:09

romainl