I have an autocmd which removes all trailing white space on write:

The autocmd:

autocmd BufWritePre * :%s/\s\+$//e

For markdown files I need there to be trailing spaces.

I'm looking for something like this:

if [ ! -f *.md ]; then
    autocmd BufWritePre * :%s/\s\+$//e
  • All comments purged. Please remember to be kind and that edits are meant to help improve posts. There can be disagreements on style and approach, but there's no need to continue to discuss and poke after further edits are made. If site policy or approach of edits needs to be examined the meta is more appropriate for that. vi.meta.stackexchange.com.
    – Dom
    Apr 17 at 22:38

1 Answer 1


You can simply do:

autocmd BufWritePre * if &ft!='markdown' | %s/\s\+$//e | endif

Rationale: you can't express a negative/exclusion in an autocommand pattern so the usual approach is to match on everything with *, and handle the conditional logic in the autocommand itself.

  • @DBenKenoble like I have said I appreciate that english is not your first language so apologies if any offensive comments were made. Good day.
    – Nickotine
    Apr 25 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.