I've been reading documentation and trying experiments to register a new filetype in vim, and apply desired indentation rules -- all to no avail.
As an example, say I want to register *.test
as filetype "test", and I want to auto-indent 2 spaces after a newline after an open parenthesis curly-brace.
All the documentation I've read converge on these actions:
- Create
~/.vim/ftdetect/test.vim
. - Create
~/.vim/ftplugin/test.vim
.
There are differences in the various documentation's instructions on the ftplugin file's content, e.g. set
vs. setlocal
-- I've tried to exhaust all the variations that I've found, but it just doesn't seem to make a difference.
The end-result of all my trials is that when I edit a *.test
file, I get no auto-indentation.
I.e. this is my desired behavior:
// foo.test
void foo() {
// I want the cursor to start at 2-spaced indented because of the
// opening curly-brace above.
...but this is the observed behavior
// foo.test
void foo() {
// The cursor starts here, in spite of the opening curly-brace above.
When I run :set ft?
in vim, it responds with filetype=test
-- I assume this means that it is "recognizing" *.test
files, so I'm at a complete loss why it's not applying indentation rules.
I do not have any other ftdetect
or ftplugin
files -- I newly created ~/.vim/ftdetect/
and ~/.vim/ftplugin/
as part of what I described above.
In spite of this, auto-indentation is working correctly for C-family files (*.c
, *.cpp
, *.h
, etc.) and "well-known" filetypes.
I don't understand why auto-indentation works for these "well-known" filetypes when they have no explicit ftdetect
or ftplugin
files.
Please help: I want vim to apply 2-space auto-indentation to *.test
-- the same as it is applying to C-family files.
My newly-created ~/.vim/ftdetect/test.vim
:
au BufRead,BufNewFile *.test set filetype=test
My newly-created ~/.vim/ftplugin/test.vim
:
autocmd FileType test setlocal expandtab
autocmd FileType test setlocal shiftwidth=2
autocmd FileType test setlocal softtabstop=2
autocmd FileType test setlocal cino=(0
autocmd FileType test setlocal syntax=on
My ~/.vimrc
:
execute pathogen#infect()
filetype plugin indent on
syntax on
set termguicolors
if has('nvim')
" https://github.com/neovim/neovim/wiki/FAQ
set guicursor=n-v-c:block-Cursor/lCursor-blinkon0,i-ci:ver25-Cursor/lCursor,r-cr:hor20-Cursor/lCursor
endif
filetype plugin on
colorscheme Base2Tone_EveningDark
set expandtab " Expand tab to spaces
set hlsearch
set number " Line numbers
set backspace=indent,eol,start
set cino=(0 " Indent N chars from the line w/ unclosed paren
set laststatus=2 " Status-line always
set scrolloff=4 " Start scrolling when cursor is N lines from top/bottom
set shiftwidth=2 " Number of spaces for each step of autoindent
set softtabstop=2 " Number of spaces that <tab> counts for
set textwidth=80
set statusline=
set statusline+=\%F
set statusline+=\%=%l/%L " Line n/N
set statusline+=%4v " virtual column
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
set formatoptions+=q " Allow formatting of comments with "gq"
set formatoptions+=t " Auto-wrap using textwidth, auto-insert comment leader
set formatoptions+=j " Remove comment leader when joining lines
set t_ut=
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 1
let g:syntastic_c_config_file = '.syntastic_c_config'
My vim version:
$ vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Nov 23 2022 14:54:38)
Included patches: 1-875, 878, 881, 883-884, 936, 948, 1046, 1365-1368, 1382, 1401, 4120, 4151-4152, 4214, 4218, 4397, 4428, 4899, 4919, 4921, 4977, 5023-5024, 805, 5043, 5063, 5126
Extra patches: 8.2.3402, 8.2.3403, 8.2.3409, 8.2.3428, 9.0.0490, 9.0.0530, 9.0.0614
Modified by [email protected]
Compiled by [email protected]
Huge version without GUI. Features included (+) or not (-):
+acl +extra_search +mouse_netterm +tag_old_static
+arabic +farsi +mouse_sgr -tag_any_white
+autocmd +file_in_path -mouse_sysmouse -tcl
+autochdir +find_in_path +mouse_urxvt +termguicolors
-autoservername +float +mouse_xterm +terminal
-balloon_eval +folding +multi_byte +terminfo
+balloon_eval_term -footer +multi_lang +termresponse
-browse +fork() -mzscheme +textobjects
++builtin_terms +gettext +netbeans_intg +textprop
+byte_offset -hangul_input +num64 +timers
+channel +iconv +packages +title
+cindent +insert_expand +path_extra -toolbar
-clientserver +job -perl +user_commands
-clipboard +jumplist +persistent_undo +vartabs
+cmdline_compl +keymap +postscript +vertsplit
+cmdline_hist +lambda +printer +virtualedit
+cmdline_info +langmap +profile +visual
+comments +libcall -python +visualextra
+conceal +linebreak -python3 +viminfo
+cryptv +lispindent +quickfix +vreplace
+cscope +listcmds +reltime +wildignore
+cursorbind +localmap +rightleft +wildmenu
+cursorshape -lua -ruby +windows
+dialog_con +menu +scrollbind +writebackup
+diff +mksession +signs -X11
+digraphs +modify_fname +smartindent -xfontset
-dnd +mouse +startuptime -xim
-ebcdic -mouseshape +statusline -xpm
+emacs_tags +mouse_dec -sun_workshop -xsmp
+eval +mouse_gpm +syntax -xterm_clipboard
+ex_extra -mouse_jsbterm +tag_binary -xterm_save
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
defaults file: "$VIMRUNTIME/defaults.vim"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -Wdate-time -g -O2 -fdebug-prefix-map=/build/vim-mKzl1a/vim-8.1.0875=. -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lm -ltinfo -lnsl -lselinux -lacl -lattr -lgpm -ldl