Frage

Ich erhalte ‚nachfolgende Leerzeichen‘ Fehler versuchen, einige Dateien in git zu begehen.

Ich mag richtig automatisch diese Hinter Leerzeichen entfernen, bevor ich Python-Dateien speichern.

Können Sie vim konfigurieren, dies zu tun? Wenn ja, wie?

War es hilfreich?

Lösung

Ich fand die Antwort hier .

Wenn Sie die folgende meiner .vimrc Datei hat den Trick.

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

Andere Tipps

Erstellung von oben plus Speicher Cursorposition:

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

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

Wenn Sie dies auf anwenden möchten speichern auf jede Datei, die zweite autocmd auslassen und einen Platzhalter * verwenden:

autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()

Ich habe auch in der Regel ein:

match Todo /\s\+$/

in meiner .vimrc Datei, so dass Zeilenende Leerzeichen hilighted werden.

Todo eine Syntax hilighting group-name sein dass für verwendet wird, hilighting Schlüsselwörter wie TODO, FIXME oder XXX. Es hat eine annoyingly hässlich gelblich Hintergrundfarbe, und ich finde es das Beste ist, Dinge hilight Sie nicht will in Ihrem Code: -)

I both highlight existing trailing whitespace and also strip trailing whitespace.

I configure my editor (vim) to show white space at the end, e.g.

enter image description here

with this at the bottom of my .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()

and I 'auto-strip' it from files when saving them, in my case *.rb for ruby files, again in my ~/.vimrc

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

Here's a way to filter by more than one FileType.

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

Copied and pasted from http://blog.kamil.dworakowski.name/2009/09/unobtrusive-highlighting-of-trailing.html (the link no longer works, but the bit you need is below)

"This has the advantage of not highlighting each space you type at the end of the line, only when you open a file or leave insert mode. Very neat."

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\+$/

I saw this solution in a comment at VIM Wikia - Remove unwanted spaces

I really liked it. Adds a . on the unwanted white spaces.

enter image description here

Put this in your .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()

This is how I'm doing it. I can't remember where I stole it from tbh.

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

A solution which simply strips trailing whitespace from the file is not acceptable in all circumstances. It will work in a project which has had this policy from the start, and so there are no such whitespace that you did not just add yourself in your upcoming commit.

Suppose you wish merely not to add new instances of trailing whitespace, without affecting existing whitespace in lines that you didn't edit, in order to keep your commit free of changes which are irrelevant to your work.

In that case, with git, you can can use a script like this:

#!/bin/sh

set -e # bail on errors

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

That is to say, we stash the changes, and then filter all the + lines in the diff to remove their trailing whitespace as we re-apply the change to the working directory. If this command pipe is successful, we drop the stash.

The other approaches here somehow didn't work for me in MacVim when used in the .vimrc file. So here's one that does and highlights trailing spaces:

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

autocmd BufWritePre * :%s/\s\+$//<CR>:let @/=''<CR>

For people who want to run it for specific file types (FileTypes are not always reliable):

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

Or with vim7:

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

If you trim whitespace, you should only do it on files that are already clean. "When in Rome...". This is good etiquette when working on codebases where spurious changes are unwelcome.

This function detects trailing whitespace and turns on trimming only if it was already clean.

The credit for this idea goes to a gem of a comment here: https://github.com/atom/whitespace/issues/10 (longest bug ticket comment stream ever)

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top