Friday, February 2, 2018

Partial current word under cursor

Hello!

It's a standard practice of mine having to replace whole words with something else, very useful when coding.  I have the following mapping in my vimrc:
    nnoremap <Leader>s :%s/\<\(<C-r><C-w>\)\>//g<Left><Left>
which lets me just hit \+s and replace the current word under the cursor.
Now I've found myself needing more and more frequently to insert something in between a word.  For example, say I have the word "getSomething" and I want to replace all occurrences with "getThisSomething", that is insert 'This' in between 'get' and 'Something'.  I know how to do this by a hand-written regex but I wanted to create a mapping like the one above.

I was thinking that I would set the cursor where I need to insert the new word, as in "get#Something" where # represents the cursor and then I'd need to replace it like so:
s/\<\(left-of-cursor)\zs\ze(right-of-cursor)\>/whatever/g
but I haven't been able to find anything that lets me split the word under the cursor.  It's probably not possible to do with such a simple mapping and I might need a function for it but my knowledge of vim-scripting is next to nothing so I thought you guys could help me figure this out.

Thank you very much!
-- Sycc90

Virus-free. www.avg.com

No comments:

Post a Comment