Search code examples
vimspaceeol

Remove EOL spaces of selection only if there are


How do I check if a selection has EOL spaces and remove them only if there are?

I need to remove the EOL spaces of a selection in order to do an other operation.
I would like to check if there are but don't know how to do this.


Solution

  • You can suppress errors in the :substitute command using the e flag. So eckes' suggestion would become:

    :'<,'>s/\ \+$//ge
    

    Then any errors are ignored, and scripts are not interrupted. See :help s_flags for more information.

    If you really want to check if there are trailing spaces you could try using something like

    if matchstr(getline("."),'\s\+$') == ""
        " there is no trailing whitespace
    else
        " there is some trailing whitespace
    endif