0

I am trying to achieve different ts, sts, sw and (no)et settings depending on project.

My idea was to have an alias, kvim f.ex., looking like alias kvim="vim -u ~/.kvimrc" with ~/.kvimrc containing only:

let g:IsKernelCode="yes"
source ~/.vimrc

And then in my ~/.vim/ftplugin/c/c.vim I could have sections like:

if exists("g:IsKernelCode")
     "kernel-code-settings
...

This seams to work when I open a single file. But kvim -p a_bunch_of_files*.c will result in only the first file having my kernel-code-settings.

What am I missing?

evading
  • 3,032
  • 6
  • 37
  • 57

3 Answers3

1

You should have a look at the following Q/A which enumerates most solutions: Vim: apply settings on files in directory

Since then, for simple settings like the ones you're interested in, you also have editor-config solutions.

Regarding your approach: having a bash alias that runs vim with a global variable set, and testing this variable from your ftplugin: this won't scale.

  • Every time you'll want to work with a project having different stylistic settings, you'll need to edit your ftplugins.
  • Also, you won't be able to edit files from different projects with the correct stylistic settings applied to each individual buffer.
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • What I ended up doing was `alias kvim='vim --cmd '"'"'let g:IsKernelCode="yes"'"'"`, discarding my `.kvimrc` and keeping the `.vim/ftplugin/c/c.vim` as in my question. If you add that to your answer I can accept it and I don't have to post and accept my own solution =) – evading Nov 16 '17 at 12:45
  • If this is what works for you, post and accept your answer. But understand this cannot scale. In another project you will want other settings. Worse, you may end up editing files from two different projects that require different settings. – Luc Hermitte Nov 16 '17 at 12:48
  • Yes, that is understood. – evading Nov 16 '17 at 12:57
1

You don't need to define an entire alternative vimrc configuration if you just want to set a flag variable. You can do that with --cmd:

alias kvim='vim --cmd "let g:IsKernelCode=\"yes\""'

As you use a global flag (with the g: prefix), this will apply to all opened files (both on startup and later interactively with :edit). For each opened file, the ftplugin/c/c.vim will be sourced again. You can check by putting :echomsg commands in there.

Note that using special settings for a project or subdirectory is a common use case. Various solutions have been devised for this; here's an overview:

Central configuration

If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:

:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4

It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....

On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:

Local config with built-in functionality

If you always start Vim from the project root directory, the built-in

:set exrc

enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.

Local config through plugin

Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.

Note that reading configuration from the file system has security implications; you may want to :set secure.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

What I ended up doing was alias kvim='vim --cmd '"'"'let g:IsKernelCode="yes"'"'", discarding my .kvimrc and keeping the .vim/ftplugin/c/c.vim as in my question.

evading
  • 3,032
  • 6
  • 37
  • 57