przbadu
przbadu

Reputation: 6049

Disable autocmd project specific in vim

I have given configuration to remove trailing white spaces in vim

  au BufWritePre *.rb :%s/\s\+$//e
  au BufWritePre *.html :%s/\s\+$//e
  au BufWritePre *.js :%s/\s\+$//e
  au BufWritePre *.jsx :%s/\s\+$//e
  au BufWritePre *.json :%s/\s\+$//e

PROBLEM

I have a project in which multiple developers are working and it's a big one. I don't want to cause lots of conflicts by removing existing trailing whitespace in this project.

EXPECTED

I want to disable this configuration for that specific project only. Lets say my project is in ~/work/projectA.

Upvotes: 4

Views: 2242

Answers (4)

Ingo Karkat
Ingo Karkat

Reputation: 172648

Just to throw another plugin solution into the ring:

My DeleteTrailingWhitespace plugin provides a robust solution (that doesn't have the mentioned problems of changing the view, clobbering the search pattern, and so on). (The plugin page has links to alternative plugins.)

My plugin can be turned off for certain buffers via a b:DeleteTrailingWhitespace flag. You can make an exception for that particular project with the :autocmd provided by the other answers, or (if you want to localize the exception to the project itself, so that it survives file system moves, or even version control clones) use a local vimrc plugin (for example the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration). With that, you can put the :let b:DeleteTrailingWhitespace = 0 command into a .lvimrc in the root of that project, and that's it.

Upvotes: 1

przbadu
przbadu

Reputation: 6049

I think the best way to configure this is using https://github.com/editorconfig/editorconfig-vim.

Global Configuration

Add global ~/.editorconfig to set global editorconfig

[*]
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

Project Specific Configuration

~/work/projectA/.editorconfig

[*]
trim_trailing_whitespace = true

Without breaking anything. and remove trim trailing whitespace code from .vimrc

  " au BufWritePre *.rb :%s/\s\+$//e
  " au BufWritePre *.html :%s/\s\+$//e
  " au BufWritePre *.js :%s/\s\+$//e
  " au BufWritePre *.jsx :%s/\s\+$//e
  " au BufWritePre *.json :%s/\s\+$//e

for list of configuration: http://editorconfig.org/

Upvotes: 2

Luc Hermitte
Luc Hermitte

Reputation: 32966

You could also fix the mappings to check a configuration variable. That variable could then be set in local vimrcs, or in an .editorconfig file with the a hook I've implemented in lh-vim-lib, or even with autocommands (which I don't recommend)

For intance, my autocommand (for vim) executes a function that tests whether b:vim_maintain.remove_trailing, p:vim_maintain.remove_trailing or g:vim_maintain.remove_trailing is set to 1 before doing the substitution.

function! lh#vim#maintain#_save_pre_hook() abort
  let pos = getpos('.')
  let cleanup = lh#on#exit()
        \.register('call setpos(".", '.string(pos).')')
  try
    if lh#option#get('vim_maintain.remove_trailing', 1)
      :silent! %s/\s\+$//
    endif
    if s:must_update_time_stamp()
      ......
     endif
  finally
    call cleanup.finalize()
  endtry
endfunction

Speaking of editor config, more simply, it has an option to decide whether trailing whitespaces shall be removed. It'll be great for projects you share with other people, however, you'll have to patch all your personal projects to add a .editorconfig file.

Upvotes: 1

romainl
romainl

Reputation: 196751

First, you could do all that with a single autocommand:

au BufWritePre *.{rb,html,js,jsx,json} %s/\s\+$//e

Second, you should put it inside a proper "augroup":

augroup TrailingSpaces
    autocmd!
    autocmd BufWritePre *.{rb,html,js,jsx,json} %s/\s\+$//e
augroup END

Third, you could disable that group for that specific project with another (maybe a bit heavy-handed) autocommand:

augroup SpecialProject
    autocmd!
    autocmd BufNewFile,BufRead ~/work/projectA/**/* autocmd! TrailingSpaces
augroup END

Fourth, losing your cursor position after :%s/\s\+$//e is not fun. I would do something like that if I were you:

augroup TrailingSpaces
    autocmd!
    autocmd BufWritePre *.{rb,html,js,jsx,json} let w:wv = winsaveview() | %s/\s\+$//e | call winrestview(w:wv)
augroup END

See :help winsaveview().

Upvotes: 11

Related Questions