Vim count selection and put in statusline

183 views
Skip to first unread message

carlosp

unread,
Feb 17, 2011, 8:10:03 PM2/17/11
to v...@vim.org

Dear all, I would like to know if there is some non-baroque way of, when
doing a selection of some text, set in the status line just the number of
characters in the selection (and of course, when one modifies the selection,
as moving the coursor in the visual mode) the status line should be updated
automatically. The solutions that occurred to me involve using external
commands (like wc -l) and then sending that to vim, each time I move the
selection etc. Does anyone knows a clean way of doing such a thing?

Thanks!
--
View this message in context: http://vim.1045645.n5.nabble.com/Vim-count-selection-and-put-in-statusline-tp3390411p3390411.html
Sent from the Vim - General mailing list archive at Nabble.com.

Christian Brabandt

unread,
Feb 18, 2011, 1:55:51 AM2/18/11
to vim...@googlegroups.com
On Fri, February 18, 2011 2:10 am, carlosp wrote:
>
> Dear all, I would like to know if there is some non-baroque way of, when
> doing a selection of some text, set in the status line just the number of
> characters in the selection (and of course, when one modifies the
> selection,
> as moving the coursor in the visual mode) the status line should be
> updated
> automatically. The solutions that occurred to me involve using external
> commands (like wc -l) and then sending that to vim, each time I move the
> selection etc. Does anyone knows a clean way of doing such a thing?

It is not exactly what you asked for, but when you set 'showcmd',
Vim already displays the size of the visual selection.

See :h 'sc'

regards,
Christian

carlosp

unread,
Feb 18, 2011, 8:19:03 AM2/18/11
to v...@vim.org

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

Thanks a lot Christian. Indeed that is not exactly what I ask for, but is
very very close. That shows me the number of lines. Anyone knows how to
"program" such a behavior? I have never browsed through the vim source code,
I wonder if one would get inspired or totally confused...
--
View this message in context: http://vim.1045645.n5.nabble.com/Vim-count-selection-and-put-in-statusline-tp3390411p3391060.html

Jürgen Krämer

unread,
Feb 21, 2011, 8:02:47 AM2/21/11
to vim...@googlegroups.com

Hi,

carlosp wrote:
>
> Dear all, I would like to know if there is some non-baroque way of, when
> doing a selection of some text, set in the status line just the number of
> characters in the selection (and of course, when one modifies the selection,
> as moving the coursor in the visual mode) the status line should be updated
> automatically. The solutions that occurred to me involve using external
> commands (like wc -l) and then sending that to vim, each time I move the
> selection etc. Does anyone knows a clean way of doing such a thing?

put the following function in your .vimrc or in a file named
~/.vim/plugin/VisualSelectionSize.vim

function! VisualSelectionSize()
if mode() == "v"
" Exit and re-enter visual mode, because the marks
" ('< and '>) have not been updated yet.
exe "normal \<ESC>gv"
if line("'<") != line("'>")
return (line("'>") - line("'<") + 1) . ' lines'
else
return (col("'>") - col("'<") + 1) . ' chars'
endif
elseif mode() == "V"
exe "normal \<ESC>gv"
return (line("'>") - line("'<") + 1) . ' lines'
elseif mode() == "\<C-V>"
exe "normal \<ESC>gv"
return (line("'>") - line("'<") + 1) . 'x' . (abs(col("'>") - col("'<")) + 1) . ' block'
else
return ''
endif
endfunction

and then change your 'statusline' option so that it contains

%{VisualSelectionSize()}

at an appropriate place, e.g., with

:set statusline.=%(\ %{VisualSelectionSize()}%)

you would append it to your current status line. The group %(...%) takes
care of removing the space if VisualSelectionSize() returns an empty
string when you are not in a visual mode.

Regards,
J�rgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

carlosp

unread,
Feb 21, 2011, 8:56:45 AM2/21/11
to v...@vim.org
Thanks a lot Jürgen! I will try it later and modify it to fit exactly my needs.

2011/2/21 Jürgen Krämer-4 [via VIM]
<[hidden email]>:
> Jürgen
>
> --
> Sometimes I think the surest sign that intelligent life exists elsewhere
> in the universe is that none of it has tried to contact us.     (Calvin)
>
> --
> 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
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://vim.1045645.n5.nabble.com/Vim-count-selection-and-put-in-statusline-tp3390411p3394112.html
> To unsubscribe from Vim count selection and put in statusline, click here.


View this message in context: Re: Vim count selection and put in statusline

Vivek Bhat

unread,
Feb 21, 2011, 10:43:26 AM2/21/11
to vim...@googlegroups.com
On Mon, Feb 21, 2011 at 7:26 PM, carlosp <carlos...@gmail.com> wrote:
Thanks a lot Jürgen! I will try it later and modify it to fit exactly my needs.

2011/2/21 Jürgen Krämer-4 [via VIM]
<[hidden email]>:
I use the default vi settings for status line.. and it does show me the number of selected characters in status line on right side.. As I keep selecting the characters in visual mode it keeps updating the selected characters in status line.

Thanks,
Vivek Bhat
Reply all
Reply to author
Forward
0 new messages