I would like to:
First, save the current highlight state (nohlsearch? or hlsearch?).
...the current cursor position and the current search pattern.
Then search for the trailing spaces, highlighting those and also be able to move around them.
Once done, turn OFF the trailing space highlighting and revert back to whatever the highlight state was (same search pattern, same highlighted characters).
Let's search for the word "line" by typing
/line in the command line. The word line lights up across the document.
Now I would like to run
ShowWhiteSpace('ON') so now the word(s) "line" are no longer highlighted, but the trailing spaces are. By pressing
n I can move around the trailing space sections.
Finally I would like to run
ShowWhiteSpace('OFF') so now the trailing spaces are no longer highlighted and we are back at where we have started with the word(s) line highlighted and our cursor at it's original position.
What I have so far:
I know that I can save the current search pattern and restore it through the
I also know how to store the cursor position so I can restore it.
The problems start when I attempt to track the
echo &hlsearch would return
0 if the current hl state is set using the
:set nohlsearch command or
1 if the
:set hlsearch command is used.
However, in cases where
:nohl is used, there is no way to know if there was a highlighted situation prior to executing the
One possible solution is to give up trying to store and restore the
hlsearch state all together and simply restore the search pattern. Which works, however, once the trailing spaces are lit-up and
n is used to move around them, the
hlsearch seems to be OFF once we restore the search pattern.
Any clean methods to handle a slightly more complex
P.S: I know that I can use
match ExtraWhitespace /\s\+$/ and a syntax highlighting, however, that would not give me the option to move around the highlighted trailing space regions.