Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
FANDOM
Explore
Current Wiki
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Remove unwanted spaces
(section)
Back to page
Edit
Edit source
View history
Talk (0)
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
Anti-spam check. Do
not
fill this in!
==Display or remove unwanted whitespace with a script== The following is a more elaborate procedure that can display or remove unwanted whitespace. Here, "unwanted" means any spaces before a tab character, or any space or tab at the end of a line. <pre> function ShowSpaces(...) let @/='\v(\s+$)|( +\ze\t)' let oldhlsearch=&hlsearch if !a:0 let &hlsearch=!&hlsearch else let &hlsearch=a:1 end return oldhlsearch endfunction function TrimSpaces() range let oldhlsearch=ShowSpaces(1) execute a:firstline.",".a:lastline."substitute ///gec" let &hlsearch=oldhlsearch endfunction command -bar -nargs=? ShowSpaces call ShowSpaces(<args>) command -bar -nargs=0 -range=% TrimSpaces <line1>,<line2>call TrimSpaces() nnoremap <F12> :ShowSpaces 1<CR> nnoremap <S-F12> m`:TrimSpaces<CR>`` vnoremap <S-F12> :TrimSpaces<CR> </pre> This is a similar function which similates the manual steps for removing the whitespace. <pre> function StripTrailingWhitespace() if !&binary && &filetype != 'diff' normal mz normal Hmy %s/\s\+$//e normal 'yz<CR> normal `z endif endfunction </pre> However, this has minor side-effects, such as influencing undo history and sometimes changing scroll position.
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Follow on IG
TikTok
Join Fan Lab