I was over at vimcasts and I stumbled upon the episode on Tidying whitespace.
They came up with a function that removes trailing whitespace. Unlike my “homemade” solution, it goes the extra mile by keeping the history clean and putting your cursor back to where it was before you invoked the command.
" Preparation: save last search, and cursor position.
let _s=@/
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
There’s room for improvement, however. Although the Single Responsibility Principle is used to talk about classes, arguably it also applies to functions. That function doesn’t do one thing, it does two (useful) things: saving the “state” and executing a command to remove the trailing whitespace.
Here’s a function that preserves the state:
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
execute a:command
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
Notice how you can inject the command into that function. Even if Vim does function pointers (to some extent), let’s just punt on this one and pass a string.
Here’s the mapping to strip out trailing whitespace:
Back on Tidying whitespace, k00pa mentioned in the comments how he modified the original function to perform the same indentation. But, it was a copy and paste(!). With the “Preserve” function, we can turn this into a one-liner.
Great tip, thanks! I’ve updated the show notes for the episode to include your refactoring: http://vimcasts.org/episodes/tidying-whitespace
Off topic: what are you using to get Vimscript syntax highlighting on your blog? I use Alex Gorbatchev’s SyntaxHighlighter, with my own Vimscript brush and blackboard theme: http://github.com/nelstrom/SyntaxHighlighter
Awesome!
As for the highlighting … it’s a very manual process. Most of the details can be found on another one of my post: https://technotales.wordpress.com/2007/08/11/code-syntax-highlighting-for-blogs-with-vim/
If you take the result of :TOhtml and start “tweaking” it a little bit (to make WordPress happy), you end up with the above.
Pardon my asking, but is the theme you’re using here based on an actual color scheme? It looks really nice.
It is.
Enjoy: http://github.com/jpalardy/dotfiles/blob/master/vim/colors/256_xoria.vim
This is nice!
Why not instead of
let l = line(“.”)
let c = col(“.”)
use getpos() and setpos()?
No reason.
My Vim scripting is based on what I copy and paste from other scripts.
[…] found the post “Preserve: A Vim function that keeps your state” that describes a function called Preserve that saves the search history and cursor position. […]
I like the refactoring, but I miss the messages like “4 changes on 4 lines”. Is there a way I can regain them? (Sorry if it is annoying to comment on a years-old post.)
Is that possile to add a range option to the Preserve function?
Like a:firstline a:lastline, so I can do something…
” Removing trailing spaces
:1,15 call Preserve(“s/\\s\\+$//e”)
I have found another great reference that can help us:
https://bobbywlindsey.com/2017/07/30/vim-functions/
Try:
:call Preserve(“1,15 s/\\s\\+$//e”)
Great Tip!
Is there a way to call the Preserve function more than once for a mapping?
For example, I normally have to run these commands (I source them from a file):
%s/‘/’/e
%s/’/’/e
%s/“/”/e
%s/”/”/e
It’d be nice to map it to a key, which calls the Preserve function with all the above substitute commands.
Thanks.