[patch] v/V with a count

53 views
Skip to first unread message

Christian Brabandt

unread,
May 24, 2012, 4:14:23 PM5/24/12
to vim...@vim.org
Bram,
attached patch fixes this issue from the todo list:

,----
| Use a count before "v" and "V" to select that many characters or lines?
| (Kikyous)
`----

regards,
Christian
--
Geschichte schreiben ist eine Art, sich das Vergangene vom Halse
zu schaffen.
-- Goethe, Maximen und Reflektionen, Nr. 251
visual_count.diff

Bram Moolenaar

unread,
May 25, 2012, 7:12:51 AM5/25/12
to Christian Brabandt, vim...@vim.org

Christian Brabandt wrote:

> Bram,
> attached patch fixes this issue from the todo list:
>
> ,----
> | Use a count before "v" and "V" to select that many characters or lines?
> | (Kikyous)
> `----

Thanks, that's useful. I will change it to take care of 'selectmode'.
And update the docs.

--
It doesn't really matter what you are able to do if you don't do it.
(Bram Moolenaar)

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ben Fritz

unread,
May 25, 2012, 10:05:49 AM5/25/12
to vim...@googlegroups.com, Christian Brabandt, vim...@vim.org
On Friday, May 25, 2012 6:12:51 AM UTC-5, Bram Moolenaar wrote:
> Christian Brabandt wrote:
>
> > Bram,
> > attached patch fixes this issue from the todo list:
> >
> > ,----
> > | Use a count before "v" and "V" to select that many characters or lines?
> > | (Kikyous)
> > `----
>
> Thanks, that's useful. I will change it to take care of 'selectmode'.
> And update the docs.
>

Will this break typing 1v to reselect an area equal to the last visual selection, but at the new cursor position? From just below :help <LeftRelease>:


If Visual mode is not active and the "v", "V" or CTRL-V is preceded with a
count, the size of the previously highlighted area is used for a start. You
can then move the end of the highlighted area and give an operator. The type
of the old area is used (character, line or blockwise).
- Linewise Visual mode: The number of lines is multiplied with the count.
- Blockwise Visual mode: The number of lines and columns is multiplied with
the count.
- Normal Visual mode within one line: The number of characters is multiplied
with the count.
- Normal Visual mode with several lines: The number of lines is multiplied
with the count, in the last line the same number of characters is used as
in the last line in the previously highlighted area.
The start of the text is the Cursor position. If the "$" command was used as
one of the last commands to extend the highlighted text, the area will be
extended to the rightmost column of the longest line.

Ingo Karkat

unread,
May 25, 2012, 10:24:52 AM5/25/12
to vim...@googlegroups.com
On 25-May-2012 07:05:49 -0700 (PDT), Ben Fritz wrote:

> Will this break typing 1v to reselect an area equal to the last visual
> selection, but at the new cursor position?

Good catch, Ben! I use that (also with a count > 1) in some of my plugins, and I
also find it useful in interactive use, though mostly for characterwise mode.

Alternatively, we could offer a Vimscript implementation (maybe just for
linewise mode, which I find most useful) in vimrc_example.vim, so users can
choose whether to have this, and plugins aren't affected (when using :normal!).

nnoremap <silent> V :<C-u>execute 'normal! V' . v:count1 . '_'<CR>

-- regards, ingo
--
-- Ingo Karkat -- /^-- /^-- /^-- /^-- /^-- /^-- http://ingo-karkat.de/ --
-- http://vim.sourceforge.net/account/profile.php?user_id=9713 --

James McCoy

unread,
May 25, 2012, 11:41:39 AM5/25/12
to vim...@googlegroups.com
On Fri, May 25, 2012 at 07:05:49AM -0700, Ben Fritz wrote:
> On Friday, May 25, 2012 6:12:51 AM UTC-5, Bram Moolenaar wrote:
> > Christian Brabandt wrote:
> >
> > > Bram,
> > > attached patch fixes this issue from the todo list:
> > >
> > > ,----
> > > | Use a count before "v" and "V" to select that many characters or lines?
> > > | (Kikyous)
> > > `----
> >
> > Thanks, that's useful. I will change it to take care of 'selectmode'.
> > And update the docs.
> >
>
> Will this break typing 1v to reselect an area equal to the last visual selection, but at the new cursor position? From just below :help <LeftRelease>:

I saw that in the docs when reviewing your patch, but I couldn't
actually get Vim to do what it said should happen. 1v just made Vim
beep at me. So, did that actually work or am I just unable to figure
what needs to be done to make it work?

--
James
GPG Key: 4096R/331BA3DB 2011-12-05 James McCoy <jame...@debian.org>
signature.asc

Ben Fritz

unread,
May 25, 2012, 2:24:34 PM5/25/12
to vim...@googlegroups.com

I've found that in order for 1v to work, you need to have "done something" with the last visual selection. E.g. this will not work:

vaw<Esc>3w1v (enter visual mode, select a word, leave visual mode, move forward 3 words, try to select the same sized visual area)

But this will work:

vawy3w1v (enter visual mode, select a word, yank it, move forward 3 words, select the same sized visual area)

Ingo Karkat

unread,
May 25, 2012, 2:45:59 PM5/25/12
to vim...@googlegroups.com
Yeah, I noticed that, too. This is probably due to an implementation detail, and
I think it should be changed. I would have expected 1v to work as soon as the
visual selection has been quit via <Esc>; i.e. as soon as gv is able to restore it.

-- regards, ingo

Christian Brabandt

unread,
May 26, 2012, 2:01:09 AM5/26/12
to vim...@googlegroups.com
Hi James!
I saw it in the code and didn't know how to trigger that code path ;)

But anyhow, it should still work and I think it does.

Mit freundlichen Gr��en
Christian

Ingo Karkat

unread,
May 26, 2012, 4:21:46 AM5/26/12
to vim...@googlegroups.com
Yes, it still seems to work, good. I still wonder about the previously noted
behavior that the previous visual selection must have been "operated on" in
order for 1v to work. (Whereas the help contradicts this: "If Visual mode is not
active and the "v", "V" or CTRL-V is preceded with a count, the size of the
*previously highlighted* area is used for a start."

So, if I understand this right, your patch allows to select [count] characters /
lines, until the visual selection has been operated on once, from then on the
existing behavior of 1v takes over, selecting a [count] multiple of the
previously operated on selection. It effectively establishes a default selection
of a single character / line from Vim start.

-- regards, ingo

Bram Moolenaar

unread,
May 27, 2012, 4:06:12 PM5/27/12
to Ingo Karkat, vim...@googlegroups.com
The idea of "1v" is that a Visual operation can be repeated at another
location. Thus the area is only stored when performing an operation.
That is different from "gv" which simply selects the same area as
before.

I'm not sure if we should change this, some scripts may depend on it and
break in mysterious ways if it is changed. Since users only notice now
that we add a new feature, it's probably not very important to make the
change.

--
Often you're less important than your furniture. If you think about it, you
can get fired but your furniture stays behind, gainfully employed at the
company that didn't need _you_ anymore.
(Scott Adams - The Dilbert principle)

Ben Fritz

unread,
May 27, 2012, 11:45:57 PM5/27/12
to vim...@googlegroups.com
On Saturday, May 26, 2012 3:21:46 AM UTC-5, Ingo Karkat wrote:
>
> So, if I understand this right, your patch allows to select [count] characters /
> lines, until the visual selection has been operated on once, from then on the
> existing behavior of 1v takes over, selecting a [count] multiple of the
> previously operated on selection. It effectively establishes a default selection
> of a single character / line from Vim start.
>

This is what seems to happen with the latest patched Vim.

I think it's a little confusing that [count]v does one thing if you haven't ever acted on a visual selection, and something else if you have.

Reply all
Reply to author
Forward
0 new messages