1

I am working on linux kernel code sometimes, and I almost every time I forget to set :set noexptab on vim when I am editing the kernel source code.

My default vimrc expands tabs into spaces, but I would like to have a function that could distinguish if I am currently editing kernel source code or not, and set the expandtab accordingly.

Basically, the kernel sources are in the linux-x.x.x directory, where is the root directory that I open the vim. I believe this is the start point.

campescassiano
  • 809
  • 5
  • 18
  • 3
    Related: http://vim.wikia.com/wiki/Project_specific_settings – rgoliveira Dec 12 '17 at 03:35
  • Duplicate of https://stackoverflow.com/q/456792/15934 ? – Luc Hermitte Dec 12 '17 at 11:27
  • @LucHermitte probably, i am not sure. It is actually not the exact same use-case, since it is not project specific, but for a kind of path. But it seems to be so close to the post you mentioned that everybody should be able to extract the information there. I am open to delete my answer and move it there as comment to sth's post or leave it out completly, if this gets judged as duplicate. However it could surely be more usefull for this site with a different title – Doktor OSwaldo Dec 12 '17 at 11:46
  • @DoktorOSwaldo It is project specific (as it's often the case when it's about stuff specific to files in a given set of directories): it's about the files from linux-kernel project. Should we merge all these Q/A (and there are already several, 4?5?) into one? I don't know. I just wasn't in the mood to describe all the solutions again. – Luc Hermitte Dec 12 '17 at 13:27
  • @LucHermitte after thinking about it, I think it should be closed and marked as duplicate. You are right, even without the wildcard sth's solution would work if you put all your linux-x.x.x folders in the same directory. The wildcard obviously should not be the point which makes my answer uniq. My thougth was just, if SO would benefit from this question if the title would be right. I don't think so afterwards as it is in the end no difference at all and I shouldn't have put an answer in here. – Doktor OSwaldo Dec 12 '17 at 13:33

2 Answers2

4

There are a lot of Plugins and other methods to handle project specific settings. I would usually strongly support the plugin approach, since it does not cluster your vimrc. But in your case that would mean to copy the project setting file to every new kernel folder. So in this case I would use following autocommand:

autocmd BufNewFile,BufRead /path/to/folder/linux-*/* set noexpandtab
Doktor OSwaldo
  • 5,732
  • 20
  • 41
1

I often have a number of settings per project (and more generally per client) so it's useful for me to have a generalized way of specifying per-client settings. I create a file called local.vimrc (or more generally, named <client-name>.vimrc and symlinked) in the root of whatever project it is I'm working on and then let this do its magic...

function! LoadLocalVimrc(filename)
    let l:filepath = fnamemodify(a:filename, ':h')
    let l:file = findfile("local.vimrc", l:filepath . ";/")
    if l:file != ''
        let l:file = fnamemodify(l:file, ":p")
        execute "source" l:file
        execute "nnoremap <buffer> <F8> :$tabe " . s:this_file . "<CR>:sp " . l:file . "<CR>"
    endif
endfunction

autocmd BufEnter * call LoadLocalVimrc(expand("<afile>"))

Then the local.vimrc uses map <buffer> ... and setlocal to keep mappings and settings local to the buffers that use them (as I sometimes have files from different clients loaded up in different tabs).

(In that code snippet above I'm remapping F8 to open my vimrc and the local.vimrc in a new tab; s:this_file is captured elsewhere as expand("<sfile>").)

dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62