0

Search-fu power is eluding me for this one.

Have many Nagios (*.cfg) files under audit, using Vim to work on them.

Notice that doing gg=G is not correctly formatting the files or a block if using = with movement commands.

My .vimrc looks like this:

syntax on
filetype on
filetype plugin on
filetype indent on

set t_Co=256

set title
set hlsearch
set incsearch
set ignorecase
set shiftround
set smarttab
set expandtab
set shiftwidth=2
set softtabstop=2
set tabstop=2
set smartindent
set copyindent
set autoindent
set mouse=a
set laststatus=2
set number
if v:version > 703
    set relativenumber " Introduced from v7.3 onwards
endif
set ruler
set showcmd
set background=dark
colorscheme elflord

" MultipleSearch
"======================
"let g:MultipleSearchMaxColors = 20
"let g:MultipleSearchColorSequence="darkcyan,gray,LightCyan,LightBlue,LightGreen,blue,green,magenta,cyan,gray,brown"
"let g:MultipleSearchTextColorSequence= "white,DarkRed,black,black,black,white,black,white,red,black,white"

" block cursor (and blinking?) mostly (a fix) for cygwin
let &t_ti.="\e[1 q"
let &t_SI.="\e[5 q"
let &t_EI.="\e[1 q"
let &t_te.="\e[0 q"

" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %

Thing is, that when formatting a file full of this kind of blocks with gg=G:

define service{
use                     prod
host_name               some_host
service_description     some_service_desc
servicegroups           some_sgroup
notes_url               https://some.notes.comh/notes.md
check_period            somep_S21h00-S09h00
notification_period     somep_S21h00-S09h00
contact_groups          some_contacsg
notification_options    c,r
check_command           check_nrpe_timeout!30!check_proc2!java!1:1!prod!"important-process"
}

Expected:

define▪service{¬
▪▪use▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪prod¬
▪▪host_name▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪some_host¬
▪▪service_description▪▪▪▪▪some_service_desc¬
▪▪servicegroups▪▪▪▪▪▪▪▪▪▪▪some_sgroup¬
▪▪notes_url▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪https://some.notes.comh/notes.md¬
▪▪check_period▪▪▪▪▪▪▪▪▪▪▪▪somep_S21h00-S09h00¬
▪▪notification_period▪▪▪▪▪somep_S21h00-S09h00¬
▪▪contact_groups▪▪▪▪▪▪▪▪▪▪some_contacsg¬
▪▪notification_options▪▪▪▪c,r¬
▪▪check_command▪▪▪▪▪▪▪▪▪▪▪check_nrpe_timeout!30!check_proc2!java!1:1!prod!"important-process"¬
}¬

But, getting instead:

define▪service{¬
▪▪use▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪prod¬
▪▪▪▪host_name▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪some_host¬
▪▪▪▪service_description▪▪▪▪▪some_service_desc¬
▪▪▪▪servicegroups▪▪▪▪▪▪▪▪▪▪▪some_sgroup¬
▪▪▪▪notes_url▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪https://some.notes.comh/notes.md¬
▪▪▪▪check_period▪▪▪▪▪▪▪▪▪▪▪▪somep_S21h00-S09h00¬
▪▪▪▪notification_period▪▪▪▪▪somep_S21h00-S09h00¬
▪▪▪▪contact_groups▪▪▪▪▪▪▪▪▪▪some_contacsg¬
▪▪▪▪notification_options▪▪▪▪c,r¬
▪▪▪▪check_command▪▪▪▪▪▪▪▪▪▪▪check_nrpe_timeout!30!check_proc2!java!1:1!prod!"important-process"¬
}¬

Some settings from the .vimrc have been applied locally with no avail.

setlocal expandtab smarttab smartindent shiftwidth=2 softtabstop=2 tabstop=2 autoindent mouse=a hlsearch incsearch ignorecase

Probably, I am missing a basic thing here or is just the normal behavior from the filetype for *.cfg files, thanks for the pointers.

5
  • 1
    Welcome to Vi and Vim! Take a look at How to debug my vimrc and see if that helps narrow things down any further. The interaction of various indent settings can be a bit mystifying at times. I recommend setting very few of them globally and leaving the rest to ftplugins (distributed, plugins, or your own tweaks in after). For example, the only one I have globally is autoindent, plus filetype plugin indent on. I use after/ftplugin/… to customize each filetype with setlocal
    – D. Ben Knoble
    Jun 29, 2021 at 19:09
  • 1
    Vim is trying to indent using C indenting rules, since there is no indent plugin for plain config files. This will inevitably result in incorrect indentation. Unfortunately I don't have an easy solution for you--you may have to write your own indent plugin. What does the output of ":set filetype?" (with the question mark as part of the command) show?
    – Heptite
    Jun 30, 2021 at 1:23
  • @D.BenKnoble I have for example completely disabled the showed .vimrc config file and trying with only the systems defaults and also with some options in setlocal mode, still the same bad indentation results.
    – alemani
    Jun 30, 2021 at 7:39
  • @Heptite True that = uses the C-indenting/lisp way to format things (according to the :help =) but generally this does the job O.K., not this time. To answer your question with/without my .vimrc I get filetype=cfg which is fine, changing filetype won't affect the formatting since it will still use the defaults for =.
    – alemani
    Jun 30, 2021 at 7:57
  • I will go the hack route, either using a macro or something like vim-autoformat, tabular or vim-easy-align, but I try to avoid plugins for portability issues, looking maybe to build my own indent.vim file for this kind of file.
    – alemani
    Jun 30, 2021 at 8:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.