'w' command to actually move to the next word

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

'w' command to actually move to the next word

Andrei Kulakov
Is there any way to get vim's 'w' command (and similar) to move from word to word?

E.g.:   tar <foo bar="jar">

I want vim to see this as 4 words separated by non-words (i.e. whitespace and punctuation). After all, tar, foo, bar and jar are words and =" is certainly not a word! And that's how I want vim to behave. I want to be on 't' and hitting '3w' should take me to 'j'.

Obviously I want this to work with all punctuation, :;,.- etc.

Thanks!

--
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
 
To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Antony Scriven-3
On 26 March 2010 00:42, AK wrote:

 > Is there any way to get vim's 'w' command (and similar)
 > to move from word to word?
 >
 > E.g.:   tar <foo bar="jar">
 >
 > I want vim to see this as 4 words separated by non-words
 > (i.e. whitespace and punctuation). After all, tar, foo,
 > bar and jar are words and =" is certainly not a word! And
 > that's how I want vim to behave. I want to be on 't' and
 > hitting '3w' should take me to 'j'.

Hmm, that's a good question; I wouldn't mind knowing that
myself. The following will work for your example.

   :nmap w /\i\+<CR>

But it will need refinement to work well. Let me know if
there's an elegant solution! --Antony

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Sven Guckes-3
In reply to this post by Andrei Kulakov
* AK <[hidden email]> [2010-03-26 02:11]:
> Is there any way to get vim's 'w' command
> (and similar) to move from word to word?

  :map w W

:-)

Sven

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andrei Kulakov
Sven Guckes wrote:
* AK [hidden email] [2010-03-26 02:11]:
  
Is there any way to get vim's 'w' command
(and similar) to move from word to word?
    

  :map w W

:-)

Sven
  

No, that moves in:

foo <"bar 

from 'f' to '<'. I want it to move to 'b'. And if there's no space, W would move to after bar. -ak

--
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
 
To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andrei Kulakov
In reply to this post by Antony Scriven-3
Antony Scriven wrote:

> On 26 March 2010 00:42, AK wrote:
>
>  > Is there any way to get vim's 'w' command (and similar)
>  > to move from word to word?
>  >
>  > E.g.:   tar <foo bar="jar">
>  >
>  > I want vim to see this as 4 words separated by non-words
>  > (i.e. whitespace and punctuation). After all, tar, foo,
>  > bar and jar are words and =" is certainly not a word! And
>  > that's how I want vim to behave. I want to be on 't' and
>  > hitting '3w' should take me to 'j'.
>
> Hmm, that's a good question; I wouldn't mind knowing that
> myself. The following will work for your example.
>
>    :nmap w /\i\+<CR>
>
> But it will need refinement to work well. Let me know if
> there's an elegant solution! --Antony
>
>  

Thanks, that's actually pretty good.. I'll use it unless somebody finds
a better way! -ak

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Tim Chase
In reply to this post by Sven Guckes-3
Sven Guckes wrote:
> * AK <[hidden email]> [2010-03-26 02:11]:
>> Is there any way to get vim's 'w' command
>> (and similar) to move from word to word?
>
>   :map w W

A nice thought, but given the OP's original condition, it does
seem weird that "w" stops where "\<" wouldn't match, and where
'iskeyword' isn't a match.  As an example of where I'd *expect*
"w" to stop based on the help description:

   :match Error /\k/

I'd expect "w" to stop at the beginning of each of these "words".
  With the default 'isk' setting, that excludes "<" and "=" ...
both of which "w" stops on in the OP's example.  Mapping "w" to
"W" would prevent "w" from stopping on "jar" in the example given.

For a certain reading of ":help word", it could be interpreted as
meaning there are 3 types of classification: 'isk' characters,
whitespace, and everything-else; with "w" stopping at the
beginning of 'isk' sequences or non-'isk' non-whitespace
sequences (the "everything-else").  Even more confusing, if you issue

   :match Error /\w/

it doesn't highlight the "<" or "=" characters as "word"
characters, despite the same usage of "word" in the descriptor
(per ":help word", a "word" includes sequences of non-whitespace
non-keyword characters surrounded by whitespace).  Yes, the help
for "\w" explicitly gives the character-class, but that seems in
conflict with the definition of "word".  Sigh :-/

I agree with the Antony's response that, to get the "intuitive"
behavior of jumping between 'isk'-defined "word"s, one would have
to do a mapping.  I'd likely use

   :nnoremap w /\<lt><cr>
   :nnoremap e /\>/e-<cr>
   :nnoremap b ?\<lt><cr>

(and their kin for visual-mode).  The only oddity is that "w" now
respects 'wrapscan'.

I originally thought the OP was jesting or had some odd setting;
but tried it (with my default vimrc and with "-u NONE") just to
make sure, and indeed it doesn't behave as I would have expected.
  I can't say I hit it (the "w" or the issue at hand) often
because I usually use t/T/f/F/;/, for my horizontal jumping, or
use the "iw" text-object for deleting/changing/visualizing the
current word.

-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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Antony Scriven-3
On 26 March 2010 01:56, Tim Chase wrote:

 > Sven Guckes wrote:
 > >
 > > * AK <[hidden email]> [2010-03-26 02:11]:
 > > >
 > > > Is there any way to get vim's 'w' command
 > > > (and similar) to move from word to word?
 >
 > [...]

Well a complete definition of what the OP meant by `word'
was not given, but I assumed it to be /\i\+/ which at least
matched the example give.. Let's go with that for the sake
of argument.

 > A nice thought, but given the OP's original condition, it
 > does seem weird that "w" stops where "\<" wouldn't match,
 > and where 'iskeyword' isn't a match. [...]

These days it sounds a bit odd saying this but I guess this
is vi-compatible behaviour.

 > [...] Even more confusing, if you issue
 >
 >  :match Error /\w/
 >
 > it doesn't highlight the "<" or "=" characters as "word"
 > characters, despite the same usage of "word" in the
 > descriptor (per ":help word", a "word" includes sequences
 > of non-whitespace non-keyword characters surrounded by
 > whitespace).  Yes, the help for "\w" explicitly gives the
 > character-class, but that seems in conflict with the
 > definition of "word".  Sigh :-/

Fun fun fun!

 > I agree with the Antony's response that, to get the
 > "intuitive" behavior of jumping between 'isk'-defined
 > "word"s, one would have to do a mapping.  I'd likely use
 >
 >  :nnoremap w /\<lt><cr>
 >  :nnoremap e /\>/e-<cr>
 >  :nnoremap b ?\<lt><cr>
 >
 > (and their kin for visual-mode).  The only oddity is that
 > "w" now respects 'wrapscan'.

Ha! Yes, once you start doing w then you are compelled to do
others as well.

Well, if you're doing it properly you should perhaps map
w to a function. Maybe something along the lines of:

   nno <silent> <buffer> :<C-U>silent call <SID>MyWFunction(v:count1, ...)<CR>
   ono <silent> ... maybe something slightly different ...

Because you just know that there will be corner cases that
require special handling.

 > I originally thought the OP was jesting or had some odd
 > setting; but tried it (with my default vimrc and with "-u
 > NONE") just to make sure, and indeed it doesn't behave as
 > I would have expected.  I can't say I hit it (the "w" or
 > the issue at hand) often because I usually use
 > t/T/f/F/;/, for my horizontal jumping, or use the "iw"
 > text-object for deleting/changing/visualizing the current
 > word.

Well I mostly use / and ? for moving about, but the OP's
question makes a lot of sense to me too. --Antony

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andrei Kulakov
Antony Scriven wrote:
> On 26 March 2010 01:56, Tim Chase wrote:
>
>  > Sven Guckes wrote:
>  > >
>  > > * AK [hidden email] [2010-03-26 02:11]:
>  > > >
>  > > > Is there any way to get vim's 'w' command
>  > > > (and similar) to move from word to word?
>  >
>  > [...]
>
> Well a complete definition of what the OP meant by `word'
> was not given, but I assumed it to be /\i\+/ which at least
> matched the example give.. Let's go with that for the sake
> of argument.



Yes, \i\+ is what I was looking for.


[snip]
>
> Ha! Yes, once you start doing w then you are compelled to do
> others as well.
>
> Well, if you're doing it properly you should perhaps map
> w to a function. Maybe something along the lines of:
>
>    nno <silent> <buffer> :<C-U>silent call <SID>MyWFunction(v:count1, ...)<CR>
>    ono <silent> ... maybe something slightly different ...
>
> Because you just know that there will be corner cases that
> require special handling.



Indeed, I'm also thinking about that because I find :hlsearch very
useful and doing /\i\+ necessitates mapping :nohl after it, but this
leads to some ugly flicker in gvim. So, if someone more experienced than
me makes a function that'd be awesome; if not, I will eventually make
one myself I guess.

My thinking on this issue is that w/b would be much easier to move
around than f/F in most cases if, well, w/b worked sensibly. The trouble
with w/b is that my brain just doesn't parse a few punctuation chars
together as a word, i.e. if I look at 5-6 vim "words" where a few of
them are real words and a few are bunches of punctuation, they just
don't look as single entities to me - at all! I guess my point is that I
don't mind so much that it doesn't make sense, I just wish w/b were easy
and inuitive to use. F/f seem quite a bit more work as you need to look
for a specific char and then type the same.

Thanks for the responses! -ak

--
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
 
To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Antony Scriven-3
On 26 March 2010 02:38, AK wrote:

 > Antony Scriven wrote:
 >
 > [...]
 >
 > > Well, if you're doing it properly you should perhaps map
 > > w to a function. Maybe something along the lines of:
 > >
 > >    nno <silent> <buffer> :<C-U>silent call <SID>MyWFunction(v:count1,
 > > ...)<CR>
 > >    ono <silent> ... maybe something slightly different ...
 > >
 > > Because you just know that there will be corner cases
 > > that require special handling.
 >
 > Indeed, I'm also thinking about that because I find
 > :hlsearch very useful and doing /\i\+ necessitates
 > mapping :nohl after it, but this leads to some ugly
 > flicker in gvim. So, if someone more experienced than me
 > makes a function that'd be awesome; if not, I will
 > eventually make one myself I guess.

No, make one yourself now. It will be instructive. And if it
doesn't work send what you do have to the list and we'll
help. And that way it will not be instructive just for you
but also for other readers of this list.

 > My thinking on this issue is that w/b would be much
 > easier to move around than f/F in most cases if, well,
 > w/b worked sensibly. The trouble with w/b is that my
 > brain just doesn't parse a few punctuation chars together
 > as a word [...]

Well that's why I use / and ? almost exclusively for moving
about: it's consistent, works for small and large distances,
and if you can touch-type is about as instantaneous as you
can get. But I probably would make use of a `w' that worked
as you've described. Things such as `dw' will require
careful thought however. I might be inclined to leave them
as they are. --Antony

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

tyru-2
In reply to this post by Andrei Kulakov
Hi.

Try smartword.
http://www.vim.org/scripts/script.php?script_id=2470

On Mar 26, 9:42 am, AK <[hidden email]> wrote:

> Is there any way to get vim's 'w' command (and similar) to move from
> word to word?
>
> E.g.:   tar <foo bar="jar">
>
> I want vim to see this as 4 words separated by non-words (i.e.
> whitespace and punctuation). After all, tar, foo, bar and jar are words
> and =" is certainly not a word! And that's how I want vim to behave. I
> want to be on 't' and hitting '3w' should take me to 'j'.
>
> Obviously I want this to work with all punctuation, :;,.- etc.
>
> Thanks!

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Christian Brabandt
In reply to this post by Antony Scriven-3
On Fri, March 26, 2010 3:50 am, Antony Scriven wrote:

> On 26 March 2010 02:38, AK wrote:
>  > Indeed, I'm also thinking about that because I find
>  > :hlsearch very useful and doing /\i\+ necessitates
>  > mapping :nohl after it, but this leads to some ugly
>  > flicker in gvim. So, if someone more experienced than me
>  > makes a function that'd be awesome; if not, I will
>  > eventually make one myself I guess.
>
> No, make one yourself now. It will be instructive. And if it
> doesn't work send what you do have to the list and we'll
> help. And that way it will not be instructive just for you
> but also for other readers of this list.

Something like this may be?

fu! <sid>MyWMotion(count)
    for i in range(a:count)
        call search('\i\+')
    endfor
endfu

noremap <silent> w :<c-u>call <sid>MyWMotion(v:count1)<cr>

I haven't looked at how to make this work with visual mode yet.

regards,
Christian

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andrei Kulakov
Christian Brabandt wrote:
[snip]
>
> Something like this may be?
>
> fu! <sid>MyWMotion(count)
>     for i in range(a:count)
>     call search('\i\+')
>     endfor
> endfu
>
> noremap <silent> w :<c-u>call <sid>MyWMotion(v:count1)<cr>
>
> I haven't looked at how to make this work with visual mode yet.
>
> regards,
> Christian
>



Excellent! Here I extended this to handle 'b'. I think that's enough for now, when I have a bit more spare time, I'll think of adding more stuff.

fu! <sid>MyWMotion(count, dir)
    for i in range(a:count)
        if a:dir
            call search('\i\+', 'b')
        else
            call search('\i\+')
        endif
    endfor
endfu

nnoremap <silent> w :<c-u>call <sid>MyWMotion(v:count1, 0)<cr>
nnoremap <silent> b :<c-u>call <sid>MyWMotion(v:count1, 1)<cr>



--
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
 
To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

zzapper
In reply to this post by Andrei Kulakov
AK wrote in news:[hidden email]:

> Is there any way to get vim's 'w' command (and similar) to move from
> word to word?
>
> E.g.:   tar <foo bar="jar">
>
> I want vim to see this as 4 words separated by non-words (i.e.
> whitespace and punctuation). After all, tar, foo, bar and jar are words
> and =" is certainly not a word! And that's how I want vim to behave. I
> want to be on 't' and hitting '3w' should take me to 'j'.
>
> Obviously I want this to work with all punctuation, :;,.- etc.
>
> Thanks!
>
Hmm this has always bothered me w/o me realising it, I end up using the f and
t commands and oft times W (as recommended by Sven)


--
zzapper
http://zzapper.co.uk/ Technical Tips

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andy Wokula
In reply to this post by Andrei Kulakov
Am 26.03.2010 14:03, schrieb AK:

> Christian Brabandt wrote:
> [snip]
>>
>> Something like this may be?
>>
>> fu! <sid>MyWMotion(count)
>> for i in range(a:count)
>> call search('\i\+')
>> endfor
>> endfu
>>
>> noremap <silent> w :<c-u>call <sid>MyWMotion(v:count1)<cr>
>>
>> I haven't looked at how to make this work with visual mode yet.
>>
>> regards,
>> Christian
>>
>
>
> Excellent! Here I extended this to handle 'b'. I think that's enough for
> now, when I have a bit more spare time, I'll think of adding more stuff.
>
> fu! <sid>MyWMotion(count, dir)
> for i in range(a:count)
> if a:dir
> call search('\i\+', 'b')
> else
> call search('\i\+')
> endif
> endfor
> endfu
>
> nnoremap <silent> w :<c-u>call <sid>MyWMotion(v:count1, 0)<cr>
> nnoremap <silent> b :<c-u>call <sid>MyWMotion(v:count1, 1)<cr>

I just uploaded motpat.vim:

http://www.vim.org/scripts/script.php?script_id=3030

     call motpat#Map(0, 'w', 'b', '\i\+')

" 0 for global mapping, 1 for buffer-local

btw: due the nature of search(), w will not go to EOF when the pattern is not
found.

--
Andy

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: 'w' command to actually move to the next word

Andrei Kulakov
Andy Wokula wrote:

> Am 26.03.2010 14:03, schrieb AK:
>> Christian Brabandt wrote:
>> [snip]
>>>
>>> Something like this may be?
>>>
>>> fu! <sid>MyWMotion(count)
>>> for i in range(a:count)
>>> call search('\i\+')
>>> endfor
>>> endfu
>>>
>>> noremap <silent> w :<c-u>call <sid>MyWMotion(v:count1)<cr>
>>>
>>> I haven't looked at how to make this work with visual mode yet.
>>>
>>> regards,
>>> Christian
>>>
>>
>>
>> Excellent! Here I extended this to handle 'b'. I think that's enough for
>> now, when I have a bit more spare time, I'll think of adding more stuff.
>>
>> fu! <sid>MyWMotion(count, dir)
>> for i in range(a:count)
>> if a:dir
>> call search('\i\+', 'b')
>> else
>> call search('\i\+')
>> endif
>> endfor
>> endfu
>>
>> nnoremap <silent> w :<c-u>call <sid>MyWMotion(v:count1, 0)<cr>
>> nnoremap <silent> b :<c-u>call <sid>MyWMotion(v:count1, 1)<cr>
>
> I just uploaded motpat.vim:
>
> http://www.vim.org/scripts/script.php?script_id=3030
>
>     call motpat#Map(0, 'w', 'b', '\i\+')
>
> " 0 for global mapping, 1 for buffer-local
>
> btw: due the nature of search(), w will not go to EOF when the pattern
> is not
> found.
>

Excellent! Works like a charm.. I thought at first that I'd want to have
cw work as vim default, but I decided to use ce instead when I need that
behaviour, and that gives me an option of using cw to change word and
all trailing punctuation.

Thanks! -ak

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

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.