how to replace selected text using keyboard shortcut

23 views
Skip to first unread message

Pengembara T.

unread,
Jan 6, 2021, 2:12:44 AM1/6/21
to vim_use
Dear all,

Sorry if this question is already asked before, but googling does not give enough result, maybe wrong search key.

what i want to do is:
- select text using mouse/trackpad (i'm in wondows).
- press some control-key to replace that text.

example, i have following line in file:
===
one
two
three
===

result:
===
<h1>one</h1>
<h2>two</h2>
three
===

usually what i am doing is:
:%s'one'<h1>one</h1>'
:%s'two'<h2>two</h2>'

can i change with:
1. select 'one' with mouse
2. Press <Ctrl-B> (or some other combination) to convert it to <h1>one</h1>.
So Ctrl-B is associated with <h1> and </h1> tag.
3. select 'two' with mouse
4. press Ctrl-2 (or other combination) to convert to '<h2>two</h2>'.
Ctrl-2 is associated with <h2> and </h2> tag.

thanks.


Salman Halim

unread,
Jan 7, 2021, 11:10:20 PM1/7/21
to Vim Users
You can get a powerful solution with a plugin such as surround.vim.

If you really want to do it just for this, you could do something like the following (untested code):

vnoremap <c-b> <esc> `>a</h1><esc>`<i<h1><esc>

I'm on my mobile phone, so can't confirm that this will work at the end of the line, but I think it will. 

--

Salman

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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/8b436976-3a1d-49d6-b579-f870105dcac6n%40googlegroups.com.

Kenny Stauffer

unread,
Jan 25, 2021, 11:43:29 AM1/25/21
to vim_use
Another mapping that does the job: vnoremap <C-b> s<h1><C-r>"</h1>

"s" in visual mode deletes the selected text (which puts the deleted text in the " register) and then enters insert mode.

In insert mode, <C-r>" inserts the contents of the " register. See ":help i^r" for more about inserting registers during insert.
Reply all
Reply to author
Forward
0 new messages