I want to use vim to delete everything inside of a paragraph tag. Is there a way? (d i p) does not work like di " does or di>. I have tried everything but I do no see anything online for this.
To borrow an answer from here, you can use vit
v = visual select
i = inside
t = tag
And see also :help v_it
And of course you can change the action from v to d to delete instead of select, or my favorite, c to replace it (deletes it and enters insert mode so you can type something else).
Related
I have the following text layout. and I want to delete from ON to the ";" I tried to use "dt;", but it said it can't find ";", looks like "t" only try to find the char in the same line, possible to find the next lines?
INNER JOIN () a *cursor* ON f.StoreID = a.StoreID
AND f.UPC = a.UPC
AND f.type = 'tom' ;
...other text...
You will have to search for it and delete up to it using d/;<CR>. (<CR> is carriage return: pressing the return/enter key) Using d with a search behaves like using t with d. It will delete up to that character (or word if using a search). This is the behavior you want. If you want to also delete the ; you should use v/;<CR>d to select all of it and then delete it.
You are correct that t, T, f and F all only look at the current line.
Zach's answer is the correct way to do this with vanilla vim however there are several plugins that you could use if you don't wish to use search.
I'd recommend you take a look at vim-sneak: Sneak is a minimalist, versatile Vim motion plugin that jumps to any location specified by two characters. Using sneak you would use dz;<cr> to delete to the next occurrence of ;. This has the advantage of not leaving all other ; characters highlighted.
The vim-sneak README also list a bunch of similar plugins.
"dt;" is only usefull in one line.I don't think there is a way to do this unless you define a macro, which makes no sense , because you can't decide delete to which ";", next line or next next line .I think you type D with your cursor on ON, then move to nextline and type dd.Use simple cmd to compose a strong cmd.Just like the unix, keep it simple。
I would add another option, using easyMotion plugin.
you can d<leader><leader>f; then choose which ; you want to delete to. pretty handy.
in vim I use
Vj..jgp
or
Vk..kgp
for selected a paragraph and format.
how I can selected and format more fast the paragraph ?
As El Isra suggests in the comments, you can do gqap instead of vapgq to avoid unnecessarily going to visual mode. Some other useful variants of ap include aw (select a word) as (select a sentence), ab (select a () block), and aB (select a {} block). There are a number of plugins that extend this functionality for HTML tags as well, so you may be interested in searching for those.
Edit: Changed V to v.
Edit 2: Changed vgaqp to gqap.
Try
gqq
or
{gq}
Consider following text file:
something
something
something = someother thing
other thing = third thing
another thing = forth thing
I want to make it look like this:
something
something
keyword something = someother thing
keyword other thing = third thing
keyword another thing = forth thing
so that, I add keyword to each line, what is contains a equals symbol in it.
Can I do this with global command, or how do you recommend I should do this?
:g/=/s/^/keyword /
or
:g/=/normal ikeyword
Note the space after "keyword"
For this type of problem, it's also quite common to use a solution like:
:%!sed '/=/s/^/keyword /'
I'm not quite sure what you're attempting to accomplish. Your title suggests a common pattern, but I don't see one in your example. So I'll show you both.
Making Changes Among Things With A Common Pattern
You can do search and replace with the following:
:s/<regex you are searching for>/<string to replace with>/g
s/pattern/replacement/ does search & replace, and the extra g will propogate the changes
Multi-Line Edit
Vim also lets you edit multiple lines at once. Say you want to edit the following three lines:
something = someother thing
other thing = third thing
another thing = fourth thing
Put your cursor on the s at the first something line.
Press <ctrl>-v outside of insert mode to go into Visual mode.`
Scroll down to the a on the bottom line. All three starting characters of all 3 lines should be highlighted.
Press A to append or I to enter directly into insert mode and start typing. When you hit escape your changes should reflect! You can also do other commands like y and d, etc.
Is there a way to do this? I know you can do all the obvious ones like ,c and ,cs
But I don't think there's a binding for commenting out an entire function...
From anywhere inside the function, do:
va{,c<space>
off course, you can map this to something:
:map ,o va{,c<space>
so pressing ,o inside a function will comment it (or uncomment it if it is already commented).
It depends..
It depends on how the function is, and where you are.
public function test()
{
$name = "whatever";
$data = array(
'name' => $name
);
return $data;
}
Scenario 1: Cursor Line 1 at public function test()
Sequence: Vj%
V Start linewise visual mode
j Go down one line
% Go to matching closing bracket
Scenario 2: Cursor Line 3 at $name = "whatever"
Sequence: va{ok
v Start visual mode
a{ Arround bracket
o Exchange cursor from top to bottom of selection
k Go up one line
Then, comment as usual ,,c depending on your Nerd Commenter mapping.
There are no binding for commenting out the whole function (as far as i know). I think there are couple of ways you can achieve this - for example you can place cursor on the closing bracket, go to the visual line mode, press % key (and select additional line if you place opening bracket in new line) and then use \cc for example.
When I want to achieve this i use textobj-user and textobj-rubyblock (I am currently programming mostly in Ruby) plugins, which allow me to easily select block of code with var and expand it with ar. That's quite nice, because I don't need to go to the end keyword (in C that would be closing bracket), but I select whole function without moving cursor from the function's body. I have not tried this, but for you this plugin should work. That's not a solution with one binding, but it's quite quick too. I hope this will be usefull for you. :)
My previous question seems to be a bit ambiguous, I will rephrase it:
I have a file like this:
copythis abc
replacethis1 xyz
qwerty replacethis2
hasfshd replacethis3 fslfs
And so on...
NOTE: replacethis1, replacethis2, replacethis3, ... could be any words
How do I replace "replacethis1","replacethis2","replacethis3",.. word by "copythis" word by using minimum vim commands.
One way I can do is by these steps:
delete "replacethis1","replacethis2","replacethis3",.. by using 'dw'
copy "copythis" using 'yw'
move cursor to where "replacethis1" was and do 'p'; move cursor to where "replacethis2" was and do 'p' and so on...
Is there a better way to do this in VIM (using less number of vim commands)?
Since you changed your question, I'd do it this way:
Move to the first "replacethis1" and type cw (change word), then type "copythis" manually.
Move to the next "replacethis", hit . (repeat last operation)
Move to the next "replacethis", hit .,
and so on, and so on.
If "copythis" is a small word, I think this is the best solution.
The digit needs to be included, and there could be more than one instance per line:
:%s/replacethis\d/copythis/g
Given that "replacethis[1-3]" can be arbitrary unrelated words, the quickest/simplest way to do this globally would be:
:%s/replacethis1\|replacethis2\|replacethis3/copythis/g
(Note that you need to use \| to get the pipes to function as "or". Otherwise, vim will look for the literal | character.)
I've been struggling with this for a long time too, I think I just worked out the cleanest way:
Use whichever command is cleanest to put copythis into register r:
/copythis
"rye
Then go to the replacement and replace it with the contents of r:
/replacethis
cw<CTRL-R>r<ESC>
Then you can just n.n.n.n.n.n.n. for the rest of them, or if they're wildly different just go to the beginning of each and hit .
The key is replacing and pasting in one step so you can use . later.
:%s/copythis/replacethis/g
To replace all occurrences of copythis with replacethis. Or you can specify a range of line numbers like:
:8,10 s/copythis/replacethis/g
Note, the /g on the end will tell it to replace all occurrences. If you leave that off it will just do the first one.
create this mapping:
:map z cwcopythis^[
( ^[ is the escape character, you can type it in vim using Ctrl+V Ctrl+[ )
go to each word you want to replace and press z
if u need to do essentially the same action multiple times - swap 1st word of one line with second word of the next line, I say you could record a macro and call it whenever you need to
Have you tried string replacement?
%s/replacethis/copythis
A host of other parameters are possible to fine-tune the replacement. Dive into the Vim help for more details. Some more examples here.
You can remap e.g. the m key in normal mode to delete the word under the cursor and paste the buffer: :nnoremap m "_diwP.
Then you can just copy the desired word, move the cursor anywhere onto the to-be-replaced word and type m.
EDIT: Mapping to m is a bad idea since it is used to mark locations. But you can use e.g. ; anyway.