I want to remove trailing blank lines (blank lines could also be just a bunch of tabs and spaces).
In my .vimrc I have:
autocmd BufWritePre * :%s#\($\n\s*\)\+\%$## " trim white spaces at the end of file
This works, however if a file has already had its trailing blank lines removed, I get the following error message:
Error detected while processing BufWrite Auto commands for "*":
How may I fix this?
From :help :s_flags:
[e] When the search pattern fails, do not issue an error message.
So you could try:
:%s#\($\n\s*\)\+\%$##e
Related
Using a minimal vimrc to remove trailing whitespace.
set list " show invisible characters
set listchars=trail:· " display extra whitespace
autocmd BufWritePost <buffer> :%s/\s\+$//e
When I open a project and start working on a file with trailing white space when I save the file I expect it to be removed, but it doesn't get removed.
When I source .vimrc manually and save the file it suddenly works.
What is causing this and how do I solve it?
BufWritePost is executed after writing. You don't need to source .vimrc — you can just write the second time.
But to really fix the problem use BufWritePre.
I have gVim 8.0.69 installed on my Windows 10 system along with Cygwin. I'm using the same .vimrc for both to keep them in sync. Some settings don't apply to both, so I'm using some conditional expressions in my .vimrc to selectively apply settings:
if !(has("gui_running"))
if version >= 747
" line 199 is the next line
set listchars=eol:-|,tab:>_,trail:~,extends:>,precedes:<,space:_
else
set listchars=eol:-|,tab:>_,trail:~,extends:>,precedes:<,
endif
endif
gVim is reporting the following error when I start it:
Error detected while processing C:\Users\<user name>\.vimrc:
line 199:
E488: Trailing characters: ,tab:>_,trail:~,extends:>,precedes:<,space:_
The line number refers to the first set listchars command.
Executing :echo !(has("gui_running")) in the gVim window reports 0, and :echo has("gui_running") returns 1, so I know the conditional is evaluating correctly after the program has started
Why is this line even being executed?
The problem doesn't come only from the | character but it comes mainly from the number of characters entered to the string setting eol.
From :help listchars it says:
*lcs-eol*
eol:c Character to show at the end of each line. When
omitted, there is no extra character at the end
It means that eol at most needs 1 single character but you gave it two : - and |.
If you still want to list eol by | you need to escape it so it will not be interpreted as a pipe.
The exact set cmd would be in that case:
set listchars=eol:\|,tab:>_,trail:~,extends:>,precedes:<,space:_
I don't know for sure, but vim still has to scan the commands to find the corresponding else or endif.
You have a | in there; | is a command separator. You could have written
if ...
set foo=bar|endif
so vim has to look at the part after the pipe to see what command it is.
I think you need to escape the pipe with set listchars=eol:-\|,tab:>_,....
I have a dead simple function in Vim that cleans source code by calling :retab and removing trailing whitespace, like so:
:function CodeClean()
: retab
: %s/\s\+$//
:endfunction
If my source code has no trailing whitespace, I get the following error messages:
Error detected while processing function CodeClean:
line 2:
E486: Pattern not found: \s\+$
So for my purposes, I either need to tell the substitution command that match errors should be silent, or tell the function invocation to ignore errors, or something else. How do I surpress error messages on substitution failure?
You can try add the 'e' option to the substitute or use :silent! as a prefix to any command
:%s/\s\+$//e
:silent! %s/\s\+$//
Notice:
You'll need to use :silent!, as :silent only removes normal messages
(and only up to the first error, subsequent messages will all be
shown)---comment of #Marth, Thanks!
Use the e flag, i.e :%s/\s\+$//e.
From :h s_flags :
[e] When the search pattern fails, do not issue an error message and, in
particular, continue in maps as if no error occurred. This is most
useful to prevent the "No match" error from breaking a mapping. Vim
does not suppress the following error messages, however:
Regular expressions can't be delimited by letters
\ should be followed by /, ? or &
No previous substitute regular expression
Trailing characters
Interrupted
{not in Vi}
Or you can use :silent! %s/\s\+$// to ignore all error messages.
When I write to a file with :w, vim sometimes (NOT ALWAYS) jumps to the end of the file after the write operation is complete. I don't understand why this happens. I've been going through my .vimrc to see if I have some kind of bug. My .vimrc is quite large so I don't include the full source here, I think the only parts of my .vimrc which are perhaps relevant to this question are the following parts:
nore ; :
inoremap jj <Esc>
" Automatically remove all trailing whitespace.
" Every time the user issues a :w command, Vim will automatically remove all
" trailing whitespace before saving
autocmd BufWritePre * :%s/\s\+$//e
" Restore cursor position
au BufReadPost *
\ if line("'\"") > 0|
\ if line("'\"") <= line("$")|
\ exe("norm '\"")|
\else|
\exe "norm $"|
\endif|
\endif
However I don't see how these parts of my .vimrc can cause the jump behavior after writing, a full source of my .vimrc is available here. I hope somebody has an idea about what is causing the unwanted jump.
Here is a command from my ~/.vimrc:
command! -range=% TR mark `|execute <line1> . ',' . <line2> . 's/\s\+$//'|normal! ``
The trick is to create mark ` before the trimming and jump back to it afterward.
You can change your autocmd to:
autocmd BufWritePre * :mark `|%s/\s\+$//e|normal! ``
Even with #romainl's addition of the mark, this still isn't fully transparent:
the view (of displayed lines) may still change (winsaveview() instead of a mark would fix that)
the :s command clobbers the last search pattern
A plugin (like my DeleteTrailingWhitespace plugin) would provide a more robust solution. (The plugin page has links to alternative plugins.)
Any idea on how to delete all the spaces and tabs at the end of all my lines in my code using vim? I sometimes use commands to add things at the end of my lines, but sometimes, because of these unexpected blanks (that is, I put these blanks there inadvertently while coding), which serve no purpose whatsoever, these commands don't do the right job... so i'd like to get rid of the blanks once and for all using some vim command. Thanks in advance!
In vim:
:%s/\s\+$//
Explanation:
: command
% apply to entire file
s search and replace
/\s\+$/ regex for one or more whitespace characters followed by the end of a line
// replacement value of an empty string
I use this function :
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
Leader,w to remove trailing white spaces
noremap <leader>w :call DeleteTrailingWS()<CR>
Remove trailing white spaces when saving a python file:
autocmd BufWrite *.py :call DeleteTrailingWS()