Vim: automatically format long single lines

I have JavaScript code written as one long line, and I want to reformat it so that each statement is written on one line. Is this possible with Vim? I tried the gqq and == commands and they did not work.

+5
source share
3 answers

It will most likely be easier to reformat using a regular expression:

:%s/;/;\r/gc
:%s/}/}\r/gc
:%s/{/{\r/gc
etc.

to insert a line after; or { }.

(if you are confident enough or the file is long, do not use it c, it will ask for confirmation for each match)

, gg=G .

, gq =

+6

vim-, vim ( ). vim-autoformat, :

https://github.com/Chiel92/vim-autoformat

vim. , javascript-, js-beautifier ( ), , .

+3

, .

function! FormatJavaScript()   
    :%s/;/;\r/gc
    :%s/}/}\r/gc
    :%s/{/{\r/gc
endfun
map <F2> <esc>:call FormatJavaScript()<cr>

, , , , . "help command", - ?

0
source

All Articles