Partial current word under cursor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Partial current word under cursor

sycc
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

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Partial current word under cursor

Tim Chase
> nnoremap <Leader>s :%s/\<\(<C-r><C-w>\)\>//g<Left><Left>
>
> 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

You might try something like this:

nnoremap <leader>s :%s/\<lt><c-r>=substitute(expand('<cword>'),
'\l\zs\ze\u', '\\zs\\ze', '')<cr>\>//g<left><left>

(all one command in case email decides to add line-breaks).

This will make <leader>s take the current Word under the cursor, look
for the break between the first lowercase letter and the following
uppercase letter (the "#" point in your "get#Something" example) and
add in the \zs and \ze at that point, then use that in a :substitute
command like your original.

It is a special-case for that lower-followed-by-uppercase transition,
but it sounds like that's what you're interested in here.

-tim


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Partial current word under cursor

porphyry5
In reply to this post by sycc
On Friday, February 2, 2018 at 2:42:21 PM UTC-8, S wrote:

> 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
I use

    nmap <F6>w yiwq:i%s/\<<C-V><C-R>"\>/<C-V><C-R>"/g<Esc>F

as an all-purpose tool for the kind of situation you describe. It copies the cursor word and builds the substitute command in the command line window. The terminating F invokes backward movement to the next character you type, to position the cursor exactly in the 'replacement' part of the command that you wish to change.

e.g. with the cursor in the word 'pictiire' that you wish to universally change to 'picture', then typing <F6>wiXru<Enter> does so.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.