Manipulate selected text

29 views
Skip to first unread message

bterkuile

unread,
Sep 29, 2011, 10:07:48 AM9/29/11
to vim_mac
Hi there,

I am now trying macvim and something is not working anymore. A normal
operation for me is to select text and do a search and replace on
this. My actions for this are:
1. Select text
2. type: :s/foo/bar/g (Or any of all the other options that make
vim cool)

In macvim however when I want to type :s/foo/bar/g when a selection is
made the colon is inserted (and yes, I am in command mode)

How can I fix this? Or is there even a better way of doing this
(meaning typing commands, not just replacing text). I cannot find the
solution on google.

Thank you!

Benjamin

David Turetsky

unread,
Sep 29, 2011, 12:14:28 PM9/29/11
to vim...@googlegroups.com
See if inserting a % as in :%s does the trick. Otherwise I believe the operation will only be confined to the current line

--
David

> --
> You received this message from the "vim_mac" 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

Kyle Lippincott

unread,
Sep 29, 2011, 12:26:46 PM9/29/11
to vim...@googlegroups.com
This sounds an awful lot like select mode to me.  Verify before you hit : that you're in 'VISIUAL' not 'SELECT' mode.  Investigate the 'selectmode' option and 'behave' command to see why this might be getting turned on.  It's meant to mimic Microsoft Windows selection model behavior, but I find it very confusing..

If you *want* to use select mode but switch to visual for something, ctrl-o will switch to visual for one command, ctrl-g will straight up switch.  If you accidentally erase something in select mode when you meant to hit ctrl-[og] to get to visual first, you can undo it by hitting 'u', then 'gv' to regain the previous selection (but you'll still be in select mode, so don't forget the ctrl-o or ctrl-g this time ;)).

Andrew Stewart

unread,
Sep 30, 2011, 2:20:33 AM9/30/11
to vim_mac
On Sep 29, 4:07 pm, bterkuile <bterku...@gmail.com> wrote:
> I am now trying macvim and something is not working anymore. A normal
> operation for me is to select text and do a search and replace on
> this. My actions for this are:
> 1. Select text
> 2. type:     :s/foo/bar/g (Or any of all the other options that make
> vim cool)
>
> In macvim however when I want to type :s/foo/bar/g when a selection is
> made the colon is inserted (and yes, I am in command mode)

I can't reproduce this on my MacVim. If I select something visually,
as soon as I type `:` the status line shows `:'<,'>` -- which means
use the visual selection as the range for the operation. You must
have something configured differently. How are you selecting text?

> How can I fix this? Or is there even a better way of doing this
> (meaning typing commands, not just replacing text). I cannot find the
> solution on google.

Yes, use ranges. Turn on line numbers with `:set number`, then to
substitute (for example) over lines 4 to 6 use `:4,6s/foo/bar/g`. See
`:help range` for other line numbers you can use, such as % (the whole
file), $ (the last line in the file), and so on. You can even use
offsets.

Yours,
Andy Stewart

Tony Mechelynck

unread,
Sep 30, 2011, 11:44:11 AM9/30/11
to vim_mac
On Sep 29, 6:26 pm, Kyle Lippincott <spect...@pewpew.net> wrote:
> This sounds an awful lot like select mode to me.  Verify before you hit :
> that you're in 'VISIUAL' not 'SELECT' mode.  Investigate the 'selectmode'
> option and 'behave' command to see why this might be getting turned on.
>  It's meant to mimic Microsoft Windows selection model behavior, but I find
> it very confusing..
>
> If you *want* to use select mode but switch to visual for something, ctrl-o
> will switch to visual for one command, ctrl-g will straight up switch.  If
> you accidentally erase something in select mode when you meant to hit
> ctrl-[og] to get to visual first, you can undo it by hitting 'u', then 'gv'
> to regain the previous selection (but you'll still be in select mode, so
> don't forget the ctrl-o or ctrl-g this time ;)).

gv will put you back in Visual mode, even if you were in Select mode
before. At least that's how it happens on GTK2 gvim, and I can't see
why MacVim would do it differently. Not knowing about Ctrl-G, I've
often used <Esc>gv to go from Select to Visual.

Kyle Lippincott

unread,
Sep 30, 2011, 1:13:35 PM9/30/11
to vim...@googlegroups.com
It probably depends on selectmode then.. I think by default on Marin
behavior it's only set to 'mouse', but I set it to all three
(including 'cmd') before writing that. Even more reason for me to
dislike the feature - too hard to tell what's going to happen ;)

Tony Mechelynck

unread,
Oct 1, 2011, 6:09:21 AM10/1/11
to vim_mac
On Sep 30, 7:13 pm, Kyle Lippincott <spect...@gmail.com> wrote:
> It probably depends on selectmode then.. I think by default on Marin
> behavior it's only set to 'mouse', but I set it to all three
> (including 'cmd') before writing that.  Even more reason for me to
> dislike the feature - too hard to tell what's going to happen ;)

I have 'selectmode' set to "key,mouse", meaning that it will be
started by a mouse drag, by a shift-click or by a shift-arrow key, but
not by hitting v V or Ctrl-V

I see that 'selectmode' is set by the :behave command; I use
neither :behave mswin nor :behave xterm but some intermediate values,
as follows:
set selectmode=mouse,key
set mousemodel=popup
set keymodel=startsel
set selection=inclusive

Best regards,
Tony.
--
Reader, suppose you were an idiot. And suppose you were a member of
Congress. But I repeat myself.
-- Mark Twain

Carl Jacobsen

unread,
Oct 6, 2011, 8:33:11 PM10/6/11
to vim_mac

Another similar approach (and what I usually use) is with marks; set
a mark at one end of the range, then maneuver to the other end of the
range and set another mark (e.g. I'll often mark the top line using
"ma" and the bottom line with "mb"), then use:

:'a,'bs/foo/bar/g

I suppose it's more keystrokes than using "v" for visual mode, but it
has worked since long before Vim arrived on the scene, and it isn't
subject to the changes in behavior depending on various options that
this thread seems to be finding. See ":help mark-motions" for more.

Cheers,
Carl

Benjamin ter Kuile

unread,
Nov 15, 2011, 5:13:56 AM11/15/11
to vim...@googlegroups.com
After using the good old terminal vim for a while again I will give it another shot. Thanks for all the suggestions/workarounds. The many tips to work around the problems shows how versatile vim is. For me there were two tips that made it work for me, so here the summary:

typing ctrl-g when text is selected makes it possible to do actions on the selected text (not replacing it) (thanks to Kyle Lippincott)
And my final solution given by Tony Mechelynck, add the following to your ~/.gvimrc file:
   set selectmode=mouse,key
   set mousemodel=popup
   set keymodel=startsel
   set selection=inclusive
This returns the for me expected behaviour. 

Thanks for the help!

Reply all
Reply to author
Forward
0 new messages