How do I format code from the web using Vim?

For instance, to provide:

while (head == tail) { head++; a[head] = 0 }

right into

while (head == tail)
    a[head] = 0
2022-06-07 14:31:55
Source Share
Answers: 2

(Firstly, you require a semicolon prior to the last } in order for this to be legitimate C ; the semicolon need to be taken ending declarations in C, as opposed to as dividing declarations. Smart automated impression might not function effectively unless you add that semicolon.)

Vim is = command (:help =) can be made use of to run code via an automated impression program, though it calls for some arrangement. The keynote is that you set the equalprg alternative (:help 'equalprg') to a program that uses the impression you desire, and afterwards make use of = in command setting to run it. = functions like various other Vim activity - based commands such as d and also y ; as an example, == indents the existing line, =ap indents a paragraph, or you can make use of an aesthetic array: ggVG= indents the entire documents.

Currently, by default, equalprg is readied to the vacant string, which just does standard per - line impression constructed right into Vim. To do the example you are requesting for, the most effective device for the work is indent:

indent Install indent

indent has a substantial array of alternatives for numerous impression designs ; by default it makes use of the GNU coding design, yet you'll possibly intend to tailor-make it to match your task is choices. See man indent for information. To include rooms in the value of equalprg, you require to estimate them with a backslash, so as an example if you intended to make use of K&R impression design you would certainly make use of :set equalprg=indent\ -kr.

You can additionally have the' =' command pick the ideal impression based upon the documents type, by utilizing :filetype indent on - type :help filetype within vim for even more details.

:set autoindent will certainly inform vim to indent as you type. Including either of these commands (minus the leading ':'-RRB- to your ~/. vimrc documents will certainly make them irreversible.

2022-06-07 14:59:50

Like any kind of various other full-screen editor, VIM does not do that instantly.

Open a documents with vim, press i for insert, and also currently you can modify openly, when done press Esc to exit insert setting and afterwards to close and also conserve the record write :wq for write, give up.

2022-06-07 14:59:48