Your code can't work because the while
loop will be evaluated when your code is sourced (if you put it in your vimrc, it will be evaluated when vim starts and it sources your config). At this point it doesn't make sense to do stuff with highlighting.
The other issue is your use of getchar()
: you can use it to detect which move the user is doing.
Instead you probably want to start by reading :h autocmd
. Auto-commands are a way to execute some code on a specific event.
For example the auto command event :h CursorMoved
triggers your code when the cursor moves. This is probably what you want to you.
The doc shows how to use auto commands and there are a lot of other questions on this site about how to use and debug them, I think now that you know about them you should be able to create one which will do what you want.