在Git中提交一些文件时,我得到了“尾随空白”错误。

我想在保存Python文件之前自动删除这些尾随的空白字符。

您可以配置Vim来实现这一点吗?如果有,怎么做?


我在这里找到了答案。

在我的.vimrc文件中添加以下内容就成功了:

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

末尾的标志表示如果搜索模式失败,该命令不会发出错误消息。。更多信息请参见:h:s_flags。


我通常也有一个:

match Todo /\s\+$/

在我的.vimrc文件中,使行末空格高亮显示。

Todo是一个语法高亮组名,用于高亮Todo、FIXME或XXX等关键字。它有一个讨厌的丑陋的黄色背景色,我发现它是最好的突出你不想在你的代码中的东西:-)


我就是这么做的。我不记得从哪里偷来的了。

autocmd BufWritePre * :call <SID>StripWhite()
fun! <SID>StripWhite()
    %s/[ \t]\+$//ge
    %s!^\( \+\)\t!\=StrRepeat("\t", 1 + strlen(submatch(1)) / 8)!ge
endfun

编译以上加上保存光标位置:

function! <SID>StripTrailingWhitespaces()
  if !&binary && &filetype != 'diff'
    let l:save = winsaveview()
    keeppatterns %s/\s\+$//e
    call winrestview(l:save)
  endif
endfun

autocmd FileType c,cpp,java,php,ruby,python autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()

如果你想在保存到任何文件时应用此命令,请省略第二个autocmd并使用通配符*:

autocmd BufWritePre,FileWritePre,FileAppendPre,FilterWritePre *
  \ :call <SID>StripTrailingWhitespaces()

从http://blog.kamil.dworakowski.name/2009/09/unobtrusive-highlighting-of-trailing.html复制粘贴(链接不再工作,但你需要的位在下面)

这样做的好处是不会突出显示你在行末键入的每个空格,只有当你打开文件或退出插入模式时才会突出显示。非常整洁。”

highlight ExtraWhitespace ctermbg=red guibg=red
au ColorScheme * highlight ExtraWhitespace guibg=red
au BufEnter * match ExtraWhitespace /\s\+$/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave * match ExtraWhiteSpace /\s\+$/

下面是一种通过多个FileType进行过滤的方法。

autocmd FileType c,cpp,python,ruby,java autocmd BufWritePre <buffer> :%s/\s\+$//e

我既突出显示现有的尾随空白,也去掉尾随空白。

我配置我的编辑器(vim)在结尾显示空白,例如。

在我的。vimrc的底部:

highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

在保存文件时,我会“自动剥离”它,在我的情况下*。Rb为ruby文件,同样在我的~/.vimrc中

function! TrimWhiteSpace()
    %s/\s\+$//e
endfunction
autocmd BufWritePre     *.rb :call TrimWhiteSpace()

简单地从文件中去除尾随空格的解决方案在所有情况下都是不可接受的。它将在从一开始就具有此策略的项目中工作,因此在即将提交的提交中没有这样的空白。

假设您只是希望不添加尾随空格的新实例,而不影响未编辑行的现有空格,以使您的提交免受与您的工作无关的更改。

在这种情况下,使用git,你可以使用这样的脚本:

#!/bin/sh

set -e # bail on errors

git stash save commit-cleanup
git stash show -p | sed '/^\+/s/ *$//' | git apply
git stash drop

也就是说,我们保存更改,然后过滤diff中的所有+行,以在我们重新将更改应用到工作目录时删除它们的尾随空格。如果这个命令管道成功,我们就丢弃隐藏。


在.vimrc文件中使用时,这里的其他方法在MacVim中不知何故对我不起作用。这里有一个这样的例子,它突出显示了尾随空格:

set encoding=utf-8
set listchars=trail:·
set list

autocmd BufWritePre *: % s / \ \ + $ / / < CR >:让@ / = " < CR >


对于那些想要为特定的文件类型(FileTypes并不总是可靠的)运行它的人:

autocmd BufWritePre *.c,*.cpp,*.cc,*.h,*.hpp,*.py,*.m,*.mm :%s/\s\+$//e

或者使用vim7:

autocmd BufWritePre *.{c,cpp,cc,h,hpp,py,m,mm} :%s/\s\+$//e

如果要删除空白,应该只对已经是干净的文件进行处理。“在罗马的时候……”在不欢迎虚假更改的代码库上工作时,这是一种良好的礼仪。

此函数检测尾随空格,并仅在已经清除的情况下开启修剪。

这个想法的功劳来自这里的一个评论:https://github.com/atom/whitespace/issues/10(有史以来最长的bug票评论流)

autocmd BufNewFile,BufRead *.test call KarlDetectWhitespace()

fun! KarlDetectWhitespace()
python << endpython
import vim
nr_unclean = 0
for line in vim.current.buffer:
    if line.rstrip() != line:
        nr_unclean += 1

print "Unclean Lines: %d" % nr_unclean
print "Name: %s" % vim.current.buffer.name
cmd = "autocmd BufWritePre <buffer> call KarlStripTrailingWhitespace()"
if nr_unclean == 0:
    print "Enabling Whitespace Trimming on Save"
    vim.command(cmd)
else:
    print "Whitespace Trimming Disabled"
endpython
endfun

fun! KarlStripTrailingWhitespace()
    let l = line(".")
    let c = col(".")
    %s/\s\+$//e
    call cursor(l, c)
endfun

我在评论中看到了这个解决方案 删除不需要的空格

我真的很喜欢。添加。在多余的空白上。

把这个放到你的。vimrc里

" Removes trailing spaces
function TrimWhiteSpace()
  %s/\s*$//
  ''
endfunction

set list listchars=trail:.,extends:>
autocmd FileWritePre * call TrimWhiteSpace()
autocmd FileAppendPre * call TrimWhiteSpace()
autocmd FilterWritePre * call TrimWhiteSpace()
autocmd BufWritePre * call TrimWhiteSpace()

autocmd BufWritePre *.py execute 'norm m`' | %s/\s\+$//e | norm g``

这将使光标保持在保存之前的相同位置