Question: Sometimes I accidentally leave blank lines at the end of the file I am editing.  

How can I trim them on saving in Vim?

### Update

Thanks guys, all solutions seem to work.  

Unfortunately, they all reset current cursor position, so I wrote the following function.

```
function TrimEndLines()
    let save_cursor = getpos(".")
    silent! %s#\($\n\s*\)\+\%$##
    call setpos('.', save_cursor)
endfunction

autocmd BufWritePre *.py call TrimEndLines()

```

Answer: 
**1.** An elegant solution can be based on the `:vglobal` command
(or, which is the same thing, on the `:global` with `!` modifier):

```
:v/\_s*\S/d

```

This command executes `:delete` on every line that does not have
non-whitespace characters in it, as well as after it in the remaining
text to the end of buffer (see `:help /\s`, `:help /\S`, and `:help /\_`
to understand the pattern). Hence, the command removes the tailing
*blank* lines.

To delete the *empty* lines in a strict sense—as opposed to blank ones
containing only whitespace—change the pattern in that `:vglobal`
command as follows.

```
:v/\n*./d

```

**2.** On huge sparse files containing large blocks of consecutive
whitespace characters (starting from about hundreds of kilobytes of
whitespace) the above commands might have unacceptable performance.
If that is the case, the same elegant idea can be used to transform
that `:vglobal` commands into much faster (but perhaps less
elegantly-looking) `:delete` commands with pattern-defined ranges.

For blank lines:

```
:0;/^\%(\_s*\S\)\@!/,$d

```

For empty lines:

```
:0;/^\%(\n*.\)\@!/,$d

```

The essence of both commands is the same; namely, removing the lines
belonging to the specified ranges, which are defined according to the
following three steps:

1. Move the cursor to the first line of the buffer before interpreting
the rest of the range (`0;`—see `:help :;`). The difference between
`0` and `1` line numbers is that the former allows a match at the
first line, when there is a search pattern used to define the ending
line of the range.
2. Search for a line where the pattern describing a non-tailing blank
line (`\_s*\S` or `\n*.`) does not match (negation is due to the
`\@!` atom—see `:help /\@!`). Set the starting line of the range to
that line.
3. Set the ending line of the range to the last line of the buffer
(`,$`—see `:help :$`).

**3.** To run any of the above commands on saving, trigger it using
an autocommand to be fired on the `BufWrite` event (or its synonym,
`BufWritePre`).
I found the previous answers using substitute caused trouble when operating on very large files and polluted my registers. Here's a function I came up with which performs better for me, and avoids polluting registers:

```
" Strip trailing empty newlines
function TrimTrailingLines()
  let lastLine = line('$')
  let lastNonblankLine = prevnonblank(lastLine)
  if lastLine > 0 && lastNonblankLine != lastLine
    silent! execute lastNonblankLine + 1 . ',$delete _'
  endif
endfunction

autocmd BufWritePre <buffer> call TrimTrailingLines()

```