我想你的第一個正則表達式只是稍微偏離的最佳途徑。這是我使用:
" highlight empty space at the end of a line
autocmd InsertEnter * syn clear EOLWS | syn match EOLWS excludenl /\s\+\%#\@<!$/
autocmd InsertLeave * syn clear EOLWS | syn match EOLWS excludenl /\s\+$/
highlight EOLWS ctermbg=red guibg=red
注意\
的+
之前和從你缺少!
前<
。
我也有一個函數映射到±空間(我已經設置爲一個逗號),以去除所有尾隨空格的文件:
function! <SID>StripTrailingWhitespace()
" Preparation: save last search, and cursor position.
let [email protected]/
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
nmap <silent> <leader><space> :call <SID>StripTrailingWhitespace()<CR>
很多更多的信息在這裏:http://vim.wikia.com/wiki/Highlight_unwanted_spaces