Find and replace with an instance of a word in vim - vim

What I'm trying to do is find an instance of a piece of code e.g.
<textarea id="question1">
and replace it with a part of itself:
<textarea id="question1" name="question1">
How can I reference "question1" in :%s of vim?

:%s;<textarea id="\([^"]*\)">;<textarea id="\1" name="\1">;

Related

Using Sublime Text find/replace to add attribute to an input tag

I have a simple input tags looking like this
<input type="password" name="repassword" placeholder="Enter password">
I want to use Sublime Text 3 and add a aria-label with the same code as in placeholder. The final result I want to be like this
<input type="password" name="repassword" placeholder="Enter password" aria-label="Enter password">
I put this regex inside the Search field <input.*?placeholder="(.*?)"
And this inside the Replace filed <input.*?placeholder=".*?" aria-label="$(1)"
But i end up with this <input.*?placeholder=".*?" aria-label="$(1)">
Update: I have enabled RedEx button in Sublime Text, this is not the problem. As searching is working great.
You have a couple of problems going on here.
The character sequence .*? doesn't have any special meaning in the replacement text, only in the original search. Thus including that in the replacement text just puts those exact characters back. If your intention is to put back the same text that was there originally, you need to capture it like you're doing with the placeholder text as well.
Secondly, the syntax for inserting captured text back is \1 or $1; $(1) is also not special and just injects those characters directly.
To extend your original example, you want to capture the first .*? as well as the second one, and change the replacement text to suit:
Find: <input(.*?)placeholder="(.*?)"
Replace: <input$1placeholder="$2" aria-label="$2"
Note that the first capture is numbered 1, the second 2 and so on.

How to move cursor to first word inside quotes?

I want to move cursor to content. I'm using ^ to move to first non-whitespace character, w to first word.
<div class="container">
| <div class="content">
<p>I love you.</p>
</div>
</div>
The pipe (|) shows the cursor position. How can I move it to content word? Like this:
<div class="container">
<div class="|content">
<p>I love you.</p>
</div>
</div>
Depends on a little but on what you want to do, but basically this can be done with:
f"w
f jumps to the char specified next, so f" jumps to the first quote, and the following w moves to the next word.
But usually you don't just want to go there, if you want to insert a word before "content", you can do it directly with:
f"a
If you want to change the content inside the quote just do the following:
ci"
Vim will automatically jump to the next quote.
There are more similar commands, so you should tell us what you want to do exactly!
If you're not on the right line, you can search with: /".\{-}\<\zs\w

How can I automate inserting <p></p> tags after paragraphs?

I often edit essays in Vim, and I would like to write a macro to insert <p> tags at the beginning and ends of paragraphs (defined as any chunk of text separated from other chunks of text by two or more line breaks). How can I do this?
Here's one option:
exe "%s#\\n\\n#\r</p>\r\r<p>#|norm D/<\/p\<CR>dd"
I would recommend looking into markdown and pandoc if you do a lot of plain text essay writing. Also, the surround.vim plugin by Tim Pope allows for this kind of tag surrounding. For instance, I can do ysiptp<CR> with the surround.vim plugin to surround a paragraph with <p> tags.

help me create a vim snippet (snipmate)

Let's say I have the following snippet:
snippet divt
<div id="${1:div_id}">
${2:'some text'}
</div>
So when I type divt and hit tab twice "'some text'" should be selected, and when I hit tab once more I would like "some text" to be selected (witoutht single quotes). How can I do it?
Edit 1: Thanks for your answers. Probably this example makes more sense:
snippet divt
<div ${1:id="${2:div_id}"}>
</div>
Sometimes I want a div without an id, so i need to be able to delete the id="div_id" altogether. Sometimes i'd like to have an id, so that i can change div_id part only.
I am currently on a promoting trip for UltiSnips which I am maintaning. The snippet that does precisely that looks like this for UltiSnips:
snippet divt "div" b
<div ${1:id="${2:div_id}"}>
</div>
endsnippet
UltiSnips also comes with a converter script for snipMate snippets, so switching should be painless.
SnipMate unfortunately doesn't support nested placeholders but, as per #Benoit's advice, you could use another snippet while editing the second placeholder. Be sure to bring a spinning top with you, though.
I'm not sure what you want to achieve with some text vs 'some text' — both being treated exactly the same way in this context by every html parser on earth — but I would achieve that with a simple
snippet div
<div id="${1:div_id}">
${2}
</div>
and simply typing either
some text
or
'
which would be expanded to (| is the caret)
'|'
thanks to delimitMate or any other similar plugin then
'some text'
Or maybe use surround to change
some text|
into
'some text'
by typing
<Esc>v2bS'
With Surround you can also start with
some text
select it with
v2e
or something similar and type
S'
to add the quotes then select the line with
V
and type
S<div id="div_id">
to obtain
<div id="div_id">
'some text'
</div>
or do it the other way or... someone has to write a blog post with ALL the possible ways to achieve a given task in Vim.

Vim Surround: Create new tag but don't indent/new line

I would like to mimic Textmates CTRL+ALT+w, which creates a new pair of opening and closing HTML tags on the same line.
In VIM Surround I'm using CTRL+st in Edit mode for this, but it always indents and creates a new line after setting the tag, so that it looks like this (* = cursor position):
<p>
*
</p>
Is there a way to achieve this? :
<p>*</p>
I guess your problem is that the selected area is "line wise". For example, if you select a few lives with V and surround it with tags, the tags will be placed one line above and one bellow the selected lines.
You probably want to create a "character wise" selection, with v before surrounding it.
Anyway, please post the map you created, so we can help debugging this.
Update
After some clarification in the comments, I would tell you that the surround plugin is not the best option. As its name describes, it was created to deal with surrounded content. So you may need content to surround.
In your case, I recommend taking a look in HTML AutoCloseTag. This plugin closes the html tag once you type the >. It is certainly more appropriated, and uses less keystrokes than surround.
<p <--- Now when you type ">", if becomes:
<p>|</p> <--- Where "|" is the cursor.
Obviously, you will get this behavior to every tag. But that may be handy if you like it.
From normal mode, type vstp> to enter visual mode and output an opening and closing <p> tag on the same line at the current cursor position. Use a capital S to maintain the current indent level.
This doesn't place the cursor in between the tags as you describe, but neither does Textmate's CtrlW shortcut (I think you meant CTRL+Shift+w, not CTRL+ALT+w, as the latter just outputs a diamond sign.)
My answer is probably coming to late, but I'll try to help.
I had similar problem with Vimsurround plugin. Every time I select sentence (one line) using ctrl+V and try to surround it with something I get this:
{
var myVar
}
instead of this:
{ var myVar } // what I wanted
I found easy solution: From a normal mode I choose a line with vis command and then I type capital C (my vim surround mapping ) and choose brackets to surround.Then I get one line nicely surrounded.
The question title is technically mislabeled based on what the author was actually looking for, but since I was actually looking for the answer to the question asked in the title, I figure I should provide an answer to it as well.
To create a new tag surrounding an element without the automatic indentation Vim Surround uses when using a block wise selection (ie: VysS), you can instead do something like:
^ys$
This command will move your cursor to the first non-blank character of the line, issue the command that you want to utilize You Surround, and move to the end of the line. Then, simply start entering your tag.
The result is this:
<input type="email" name="email">
Could become something like this:
<li><input type="email" name="email"></li>
The command is repeatable as well with . and all the normal other Vim goodness.
Stumbled upon this question because I was wondering this as well - I believe the simplest way to do this is just:
yss<p>
(yss surrounds a line with something without indenting - see here: http://www.catonmat.net/blog/vim-plugins-surround-vim/)
You can accomplish this by selecting the relevant text object: :h text-objects
...and surrounding that instead of surrounding a Visual Line selection.
The most common example I found myself running into was when trying to surround one tag with another. In that situation, the it and at text objects are quite useful:
*v_at* *at*
at "a tag block", select [count] tag blocks, from the
[count]'th unmatched "<aaa>" backwards to the matching
"</aaa>", including the "<aaa>" and "</aaa>".
See |tag-blocks| about the details.
When used in Visual mode it is made characterwise.
*v_it* *it*
it "inner tag block", select [count] tag blocks, from the
[count]'th unmatched "<aaa>" backwards to the matching
"</aaa>", excluding the "<aaa>" and "</aaa>".
See |tag-blocks| about the details.
When used in Visual mode it is made characterwise.
For example, if you had your cursor in a paragraph and you wanted to surround it with a div on the same line, ysat<div> would accomplish that.

Resources