1

I'd like to quickly replace all non word characters up to first word \w character with a space.

----So that this line becomes
    So that this line becomes

----------------And this line looks like this
                And this line looks like this

--=======--^^---Likewise this becomes
                Likewise this becomes

This command: 0v\wr (space after the r) works in normal mode, but when I place it in my _vimrc file, with nnoremap <leader>! 0v\wr (with space being the last character), I get stuck in visual mode without character replacement.

  • 1
    Use <space> instead of an actual space. – romainl Sep 19 '15 at 16:39
  • That's a start, but I still get stuck in Visual mode without character replacement. – zundarz Sep 19 '15 at 19:25
  • Well, how do you usually get out of visual mode? – romainl Sep 19 '15 at 21:00
  • When I'm in normal mode tying 0v\wr<space>, the code performs as expected, I never need to escape visual mode as r<space> returns me to normal mode. When I add <esc> to the end of the _vimrc script, I get to operator pending mode.(Half size blinking cursor). – zundarz Sep 19 '15 at 21:14
  • So, you just don't add the <esc> and problem was solved, right? – VanLaser Sep 19 '15 at 22:56
3
nnoremap <leader>! 0v<S-Right>hr<Space>

This possible solution may be also useful

:%s/^.\{-}\</\=repeat(' ',strlen(submatch(0)))/g

Read more

:h functions
:h function-list
  • Take my upvote for the use of repeat in the substitution, that's what I wanted to get to but couldn't remember or find how to do it! :-) – statox Sep 19 '15 at 22:12
  • ryuichiro, You steered me in the right direction. The error was in using \w instead of just w. The h shifts my cursor to the left one position and won't replace the one non-word character to the left of my word character. Both of the following work. nnoremap <leader>!v<S-Right>wr<Space> and nnoremap <leader>!vwr<Space>. The latter is the original using w instead of \w. – zundarz Sep 20 '15 at 2:21
2

Here is a possible solution which correct all the lines of your file in one command. (Warning: might cause eye bleeding to regex masters)

You can add these lines to your vimrc:

function! Test()
    let match = search('^\s*[^a-zA-Z0-9 ]\+\w')
    while match
        s/^\(\s*\)\(\W\)\(\w*\)/\1 \3/
        let match = search('^\s*[^a-zA-Z0-9 ]\+\w')
    endwhile
endfunction

nmap <Leader>! :call Test()<CR>

The function has a while loop which is true while a line begins by 0 or more whitespaces followed by a non word and non whitespace character followed by a word character.

Each time such a line is found the first non word character following a beginning whitespace is replaced by a whitespace.

And the mapping is here to call the function with leabder+!.

For the following file:

 ---So that this line becomes
----------------And this line looks like this
    =====--^^---Likewise this becomes
 -=======--^^---Likewise^^^^^ this becomes

You get:

    So that this line becomes
                And this line looks like this
                Likewise this becomes
                Likewise^^^^^ this becomes

That might not be the most elegant solution but you get the result :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.