How to use visual-mode in a search pattern?

10 views
Skip to first unread message

stosss

unread,
Apr 19, 2010, 2:56:51 AM4/19/10
to Vim Users
I read :help search, :help visual-mode and the FAQ on the same.

I am not understanding how to use visual-mode in my search pattern. I
can't figure out how to make it work.

Will some one enlighten me?

--
If we can but prevent the government from wasting the labours of the
people, under the pretence of taking care of them, they must become
happy. - Thomas Jefferson

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

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

John Beckett

unread,
Apr 19, 2010, 4:09:43 AM4/19/10
to vim...@googlegroups.com
stosss wrote:
> I am not understanding how to use visual-mode in my search
> pattern. I can't figure out how to make it work.

You haven't said what you want to do. A short example is often
useful. If you want to search for selected text, see:
http://vim.wikia.com/wiki/Search_for_visually_selected_text

John

stosss

unread,
Apr 19, 2010, 5:42:26 AM4/19/10
to vim...@googlegroups.com
On Mon, Apr 19, 2010 at 4:09 AM, John Beckett <johnb....@gmail.com> wrote:
> stosss wrote:
>> I am not understanding how to use visual-mode in my search
>> pattern. I can't figure out how to make it work.
>
> You haven't said what you want to do. A short example is often
> useful. If you want to search for selected text, see:
> http://vim.wikia.com/wiki/Search_for_visually_selected_text
>
> John

That tip looks like it is probably useful but I still can't figure it
out either. I am new and still learning.

Here is what I am trying to do.

There are curly quotes in a text file and I want to replace them with
straight quotes. Since I don't see any way to type a curly quote in
vim. I need to high-light one (visual-mode? select-mode? CTRL
something?) so I can use/put it in my search pattern so they can be
replaced with straight quotes.

--
If we can but prevent the government from wasting the labours of the
people, under the pretence of taking care of them, they must become
happy. - Thomas Jefferson

lessthanideal

unread,
Apr 19, 2010, 6:37:15 AM4/19/10
to vim_use
On Apr 19, 10:42 am, stosss <sto...@gmail.com> wrote:
> There are curly quotes in a text file and I want to replace them with
> straight quotes. Since I don't see any way to type a curly quote in
> vim. I need to high-light one (visual-mode? select-mode? CTRL
> something?) so I can use/put it in my search pattern so they can be
> replaced with straight quotes.

You can use Ctrl-R in command-line mode to get the contents of a
register or
an expression, see

:help c_ctrl-r

So you can do it like this:
- in normal mode move the cursor to be on the quote character
- type v to enter visual mode
- type y to yank that character into register 0 (see :help registers)
(this takes you out of visual mode)
- start entering the replace command in the usual way by typing

:%s/

- type Ctrl-R, it prompts you with a " character
- type 0, it fills in the contents of register 0 i.e. your curly quote
- finish off the replace command in the usual way i.e. type

/'/g<Enter>

regards,
Geoff

John Little

unread,
Apr 19, 2010, 7:24:36 AM4/19/10
to vim_use

> Since I don't see any way to type a curly quote in
> vim...

From wikipedia "Quotation mark glyphs":
‘O’ U+2018 (8216), U+2019 (8217) Single quotes (left and right)
“O” U+201C (8220), U+201D (8221) Double quotes (left and right)

Thus knowing the codepoints, you can enter them in vim using ctrl-v
u. For example, to get a right double curly quote type (without
spaces, in insert mode, or on the command line or at the / prompt)

ctrl-v u 201d

(If ctrl-v is your paste, use ctrl-q).

Another way to find the codepoints is to paste the text in vim, then
use ga (in normal mode) on them.

If you were going to be doing this a lot, mappings in your .vimrc or a
script file might help, f. ex.

inoremap <f6> <c-v>u201d

HTH, and is relevant, John

stosss

unread,
Apr 19, 2010, 12:55:55 PM4/19/10
to vim...@googlegroups.com
Thank you to all. Very helpful.

--
If we can but prevent the government from wasting the labours of the
people, under the pretence of taking care of them, they must become
happy. - Thomas Jefferson

John Beckett

unread,
Apr 19, 2010, 6:46:45 PM4/19/10
to vim...@googlegroups.com
stosss wrote:
> There are curly quotes in a text file and I want to replace
> them with straight quotes. Since I don't see any way to type
> a curly quote in vim. I need to high-light one (visual-mode?
> select-mode?

I highly recommend that tip I mentioned; I use it all the time
for searching for text that I have selected.

The following is what I use (in my vimrc) to convert curly
quotes to straight:

" Convert curly quotes to straight.
" Any argument causes substitute to confirm changes.
function! ToStraight(line1, line2, args)
let flags = 'eg'
let range = a:line1 . ',' . a:line2
if empty(a:args)
let range = 'silent ' . range
else
let flags .= 'c'
endif
let search = @/
exe range . "s/['']/'/" . flags
exe range . 's/[""]/"/' . flags
nohl
let @/ = search
endfunction
command! -nargs=? -range ToStraight call ToStraight(<line1>, <line2>, '<args>')

If the utf-8 encoding is not corrupted, you will see the curly
quotes inside the [brackets] in the two "exe range..." lines.

Usage examples:
:%ToStraight " convert all
:ToStraight " convert current line only
:%ToStraight c " convert all and confirm each

John

Tony Mechelynck

unread,
Jun 25, 2010, 11:59:36 PM6/25/10
to vim...@googlegroups.com, stosss
On 19/04/10 11:42, stosss wrote:
> On Mon, Apr 19, 2010 at 4:09 AM, John Beckett<johnb....@gmail.com> wrote:
>> stosss wrote:
>>> I am not understanding how to use visual-mode in my search
>>> pattern. I can't figure out how to make it work.
>>
>> You haven't said what you want to do. A short example is often
>> useful. If you want to search for selected text, see:
>> http://vim.wikia.com/wiki/Search_for_visually_selected_text
>>
>> John
>
> That tip looks like it is probably useful but I still can't figure it
> out either. I am new and still learning.
>
> Here is what I am trying to do.
>
> There are curly quotes in a text file and I want to replace them with
> straight quotes. Since I don't see any way to type a curly quote in
> vim. I need to high-light one (visual-mode? select-mode? CTRL
> something?) so I can use/put it in my search pattern so they can be
> replaced with straight quotes.
>

Especially on non-US keyboards, special characters are often obtained by
means of AltGr with or without Shift, see mine at
http://users.skynet.be/antoine.mechelynck/other/keybbe.htm . In
particular, I have curly brackets {} at AltGr + unshifted 9 and 0,
French quotes «» at AltGr + w and x, opening and closing double quotes
above “” at AltGr + v and b, opening and closing single quotes above ‘’
at AltGr + Shift + v and b. (This post is in UTF-8, BTW.)

To yank a single character in the text and use it in a search, you don't
need Visual mode: yank the character under the cursor (in Normal mode)
with yl then (in Command-line mode) put the default register (containing
your yank) with Ctrl-R double-quote.


Best regards,
Tony.
--
The Great Bald Swamp Hedgehog:
The Gerat Bald Swamp Hedgehog of Billericay displays, in
courtship, his single prickle and does impressions of Holiday Inn desk
clerks. Since this means him standing motionless for enormous periods
of time he is often eaten in full display by The Great Bald Swamp
Hedgehog Eater.
-- Mike Harding, "The Armchair Anarchist's Almanac"

Reply all
Reply to author
Forward
0 new messages