vim: удалить предыдущий отступ кода и преобразовать в другой

649
ramgorur

У меня есть проект ac с несколькими файлами (более 100), коды написаны в стиле Whitesmiths, но я хочу изменить их на отступ в стиле K & R. Можно ли использовать vim в автоматическом режиме?

Например, у меня есть скрипт emacs-lisp для достижения этой цели -

(progn  (find-file "{}")  (mark-whole-buffer)  (setq indent-tabs-mode nil)  (untabify (point-min) (point-max))  (indent-region (point-min) (point-max) nil)  (save-buffer)) 

Мне было интересно, есть ли подобный трюк, который можно сделать с vim.

Обновление: я нашел еще один хороший инструмент под названием astyle, который также поддерживает автоиндентирование для разных языков (с широким диапазоном стилей).

3
Следует отметить [GNU Indent] (http://www.gnu.org/software/indent/), который делает все это сам по себе и хорошо протестирован. Не изобретать велосипед и т. Д. Daniel Andersson 11 лет назад 4

2 ответа на вопрос

5
Ingo Karkat

Either pass all C files to the Vim executable vim file1.c dir/file2.c, or add them as arguments from inside Vim (see :help file-searching):

:args **/*.c **/*.h 

Then, you can mass-edit them via :argdo. Vim has a built-in indenting mechanism, or it can use an external code formatter. Read up on the details at :help C-indenting.

Once you've configured the indent settings ('cindent', 'cinoptions', etc.), you can apply all files via

:argdo execute 'normal! ggVG=' | update 

(ggVG selects the entire buffer in visual mode, = then re-formats.)

У вас есть лучшие ответы Vim :) +1 Ярослав Рахматуллин 10 лет назад 0
4
Ярослав Рахматуллин

отступ человека * прокрутка прокрутка прокрутка *

 The Kernighan & Ritchie style is used throughout their well-known book "The C Programming Language". It is enabled with the ‘-kr’ option. The Kernighan & Ritchie style corresponds to the following set of options:  -nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0 -cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss 

Например:

indent -kr source.cpp 

Похожие вопросы