aboutsummaryrefslogtreecommitdiff
path: root/vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-10-30 13:33:09 +1300
committerTom Ryder <tom@sanctum.geek.nz>2017-10-30 13:33:09 +1300
commitd3d998c68c335b35525172c700ff958d5a016399 (patch)
treec63fa039ab95756494ca1230d432cda15509bf76 /vim
parentRemove vim/after installation from vim/bundle (diff)
downloaddotfiles-d3d998c68c335b35525172c700ff958d5a016399.tar.gz
dotfiles-d3d998c68c335b35525172c700ff958d5a016399.zip
Configure indent dynamically based on filetype
When a new buffer is created with no filename, it's often because I want to paste text into it without being bothered by autoindent or tab translation, and so I'd rather Vim just accepted the text as literal input without trying to indent it for me. Similarly, for CSV files or Vim help files, space-based automatic indentation is undesirable or not meaningful. It's better to allow Tab to insert literal tab characters in this case, especially if it's a classic Unix-style tab-separated file. However, these settings should definitely be set on buffers which actually do have virtually any other filetype. I don't really like that long `if` condition there--it might be better to find some way to simulate a case/switch structure instead. I originally wanted to include a "negative match" in the `autocmd` definition, which I imagined looking something like this: autocmd FileType '',csv,help,text call FileTypeIndentConfig(&filetype) autocmd FileType !'',csv,help,text call FileTypeIndentConfig(&filetype) However I can't find anything in the Vim :help or by searching online that suggests this is possible. This function-based approach seems good enough for now. This hasn't been tested on old versions of Vim yet.
Diffstat (limited to 'vim')
-rw-r--r--vim/config/indent.vim31
1 files changed, 20 insertions, 11 deletions
diff --git a/vim/config/indent.vim b/vim/config/indent.vim
index f6dfd416..1ae8a909 100644
--- a/vim/config/indent.vim
+++ b/vim/config/indent.vim
@@ -1,14 +1,7 @@
-" Adopt the indent of the last line on new lines; interestingly, plugins that
-" do clever things with indenting will often assume this is set
-set autoindent
-
-" Use spaces instead of tabs
-set expandtab
-
-" Indent with four spaces when an indent operation is used
+" Indent with four literal spaces when 'expandtab' is on
set shiftwidth=4
-" Insert four spaces when Tab is pressed
+" Insert four spaces when Tab is pressed and 'expandtab' is on
set softtabstop=4
" How many spaces to show for a literal tab when 'list' is unset
@@ -19,6 +12,22 @@ set tabstop=4
set smarttab
" When indenting lines with < or >, round the indent to a multiple of
-" 'shiftwidth', so even if the line is indented by one space it will indent
-" up to 4 and down to 0, for example
+" 'shiftwidth', so even if the line is indented by one space it will indent up
+" to 4 and down to 0, for example; all this when 'expandtab' is on
set shiftround
+
+" Tabs vs spaces and automatic indentation behaviour depends on there being an
+" actual filetype that's more than just plain text (or a Vim help buffer).
+function! FileTypeIndentConfig(ft)
+ if a:ft == '' || a:ft == 'csv' || a:ft == 'help' || a:ft == 'text'
+ setlocal noautoindent noexpandtab
+ else
+ setlocal autoindent expandtab
+ endif
+endfunction
+
+" Call the function that we just declared each time the filetype is set
+augroup dfindent
+ autocmd!
+ autocmd FileType * call FileTypeIndentConfig(&filetype)
+augroup END