4

我一直在我的 .vimrc 中使用以下技术来删除行尾的多余空格...

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

但我意识到我不希望 Markdown 文件(例如.mdor 或.markdown)发生这种情况,所以我有以下 VimScript ......

fun! StripTrailingWhiteSpace()
    " don't strip on these filetypes
    if &ft =~ 'md\|markdown'
        return
    endif
    %s/\s\+$//e
endfun
autocmd bufwritepre * :call StripTrailingWhiteSpace()

但这仍然会删除所有文件的空格。

所以我然后尝试了以下(这似乎更好,因为它更短)......

let blacklist = ['md', 'markdown']
autocmd BufWritePre * if index(blacklist, &ft) < 0 | :%s/\s\+$//e

但是,这仍然会删除所有文件的空格?

这些技术似乎都不起作用?他们将空格留在文件中吗?

关于如何做到这一点的任何想法(目前我不得不在单独的写作应用程序而不是 Vim 中编辑 Markdown 文件,这很烦人)。

4

4 回答 4

3

第一个功能应该可以工作,除非你不应该寻找md. 是Markdown文件的ft缩写。filetypemarkdown

通过如下更改它可以正常工作。(在 Vim 7.4 上测试)

fun! StripTrailingWhiteSpace()
  " don't strip on these filetypes
  if &ft =~ 'markdown'
    return
  endif
  %s/\s\+$//e
endfun
autocmd bufwritepre * :call StripTrailingWhiteSpace()
于 2013-11-12T18:13:49.950 回答
0

您的实现很简单,但有一些缺点(例如,它破坏了最后的搜索模式/搜索历史)。如果你不反对安装插件,你可以试试我的DeleteTrailingWhitespace 插件。有了它,您可以通过设置标志来排除某些缓冲区,例如:

autocmd FileType markdown let b:DeleteTrailingWhitespace = 0
于 2013-11-13T09:31:05.950 回答
0

Thanks Kevin Sjoberg for the initial response, but it turns out I have a bigger issue.

Which is if I run :set filetype? from within my Markdown file then it reports back that Vim thinks the filetype is modula2

I'm not sure how I can fix this, so if any one knows then please do comment!

As a temp fix I've used the following work around...

fun! StripTrailingWhitespace()
    " don't strip on these filetypes
    if &ft =~ 'modula2\|markdown'
        return
    endif
    %s/\s\+$//e
endfun
autocmd BufWritePre * call StripTrailingWhitespace()

...so it checks for both modula2 (whatever that is?) AND markdown files.


UPDATE: well it seems this is a known issue https://github.com/tpope/vim-markdown/issues/15 so I tried the suggested fix...

au BufRead,BufNewFile *.md set syntax=markdown

...but that didn't help, Vim still interpreted the file as modular2

Also tried adding the following into my vimrc file...

au! BufNewFile,BufRead *.md setf markdown

...but that didn't work to change the format.


UPDATE 2

Fixed it, the suggestion by both https://github.com/tpope/vim-markdown/issues/15 and Kevin Sjoberg were almost there.

I just added into my vimrc file a modified version of their suggestion au Bufread,BufNewFile *.md set filetype=markdown

于 2013-11-12T18:26:30.783 回答
0

我意识到您解决的问题不仅仅是您尝试设置的功能。

但是,我喜欢您最初使用文件类型列表的“黑名单”方法。因此,对于其他喜欢这样登陆的人来说,这就是我所拥有的。

它还保存您的光标位置,并在运行删除空格后将其放回原处。

function! StripTrailingWhitespace()
    " Preparation: save last search, and cursor position.
    let _s=@/
    let l = line(".")
    let c = col(".")
    " do the business:
    %s/\s\+$//e
    " clean up: restore previous search history, and cursor position
    let @/=_s
    call cursor(l, c)
endfunction

let noStripWhiteSpaceTypes = ['markdown']
autocmd BufWritePre * if index(noStripWhiteSpaceTypes, &ft) < 0 | call StripTrailingWhitespace() | endif
于 2018-12-14T00:40:34.490 回答