简体   繁体   中英

Vim - removing leading and trailing spaces in a function

I am trying to remove leading and trailing spaces in a function but it does not work:

function Trim(s)
    echo ">>>".a:s."<<<"
    let ss = substitute(a:s, '\v^\s*([^\s]+)\s*$', '\1', '') 
    echo ">>>".ss."<<<"
endfunction

The regex \\s*([^\\s]+)\\s* works ok on https://regex101.com/

Replacing * with + does not make any difference.

Testing:

: call Trim("   testing    ")

Output:

>>>   testing    <<<
>>>   testing    <<<

Also it seems to matter if I use double quotes versus single quotes in substitute function.

Where are the problems and how can they be solved? Thanks.

Your issue is caused by your collection.

You should use [^ ] instead of [^\\s] :

function! Trim(s)
    echo ">>>".a:s."<<<"
    let ss = substitute(a:s, '\v^\s*([^ ]+)\s*$', '\1', '') 
    echo ">>>".ss."<<<"
endfunction

This is because collections work on individual characters and \\s is not an individual character; it's seen as \\ followed by s , which doesn't resolve to anything because s is not a special character that needs escaping.

If you want your collection to include both spaces and tabs, use this:

[^ \t]
[ \t]

where \\t represents a tab.

As romainl explained, [^\\s] means neither \\ nor s . The contrary of \\s (ie anything but a space or a tab) would be \\S .

Otherwise, here is another solution: in lh-vim-lib I've defined the following

function! lh#string#trim(string) abort
  return matchstr(a:string, '^\v\_s*\zs.{-}\ze\_s*$')
endfunction

Regarding the difference(s) between the various kinds of quote characters, see this Q/A on vi.SE: https://vi.stackexchange.com/questions/9706/what-is-the-difference-between-single-and-double-quoted-strings

You are including what needs to be retained in your search/replace. Much easier is to just look for what needs te be removed and substitute that

:%s/\v(^\s+|\s+$)//g

Breakdown

%s             Start a global search/replace
\v             Use Very Magic 
(^\s+          search for leading spaces
|              or
\s+$)          search for trailing spaces
//g            remove all search results from entire line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM