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}
Related
I found myself struggling with quickly changing attributes inside HTML tags.
For example, I have the following code
<div className="expense-date">
<div className="expense-date__###year">{year}</div>
<div>{month}</div>
<div>{day}</div>
</div>
My cursor is where the ### is, and I want to delete the whole attribute: className="expense-date__###year".
Is there any text object I can use to quickly delete it? Currently I use T ct> (or T ct if there's another attribute), but that's obviously not good enough and won't work if there's a space inside the attribute's value.
There is no built-in "attribute" text object but you can approximate one with something generic like this:
va"oB
where:
va" selects the value,
o moves the cursor to the other side of the selection,
B extends the selection to the beginning of the attribute.
This can be turned into quick and dirty pseudo-text objects:
xnoremap ix a"oB
onoremap ix :<C-u>normal vix<CR>
xnoremap ax a"oBh
onoremap ax :<C-u>normal vax<CR>
a visual mode mapping that covers the desired text,
an operator-pending mode mapping that uses the visual mode one,
to use like this:
yix
dax
vix
cax
…
See this plugin for a much smarter implementation.
Note that nothing in this answer is guaranteed to work in Neovim.
To my knowledge, there is no simple way to do this; unless you perform a deep syntax analysis, there will always be some caveats.
You can however improve your technique, for example with F"F=bF d2f".
F" goes to the beginning of the string;
F= goes to the previous = sign, skipping eventual spaces between;
b goes to attribute name (eventualy in the middle of it e.g. for data-my-attr), skipping eventual spaces;
F goes to the 1st space before the attribute name;
d2f" (or c2f") performs the job.
Of course, you can use it as a macro with q and # in order to simplify its use.
Here's another option:
vi> ........ visual select inner <>
o ............ jump to the other side
w ............ jump to the next word
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).
I've been using VIM for a while and it surprises me each time. Under "Building Sentences" section in this tutorial, I saw the combination of commands cis and yip. I have used Vim quite a while and I am familiar with most commands in Normal Mode. I also know combining the commands in a meaningful way to produce combined actions.
However, the examples I showed above (cis and yip) totally broke my understanding of VIM command system in normal mode. "c" stands for change, "i" stands for insert and "s" stands for substitute but combined action is different than I would expect. I also went through VIM help files but never saw an example illustrating given usage.
Could someone clarify what's going on ?
cis
In vim help it is described as follows
:help c
"Delete {motion} text [into register x] and start insert …"
The next part of the command cis refer to the "motion" part. These commands are for text object selection. An explanation on the different types of text object selections can you get here:
:help text-objects
e.g. for
is – "inner sentence", select [count] sentences …
Analog to the explanation above its the same with yip
:help y
"Yank {motion} text [into register x] … "
And the text selection part yip
ip – "inner paragraph", select [count] paragraphs (see paragraph) …
In this case i does not stand for Insert and s not for substitute.
cis = change inner sentence.
This is completly logic once you understand the basic principle. Each command is like a sentence, it needs an Verb(Action) and an Noun(Object) and there are modifiers.
So the first button is your action C (Change). Now the following keystrokes will not be actions, until the c action ended (Until an Object is provided, or an invalid sequence is inserted). I (inner) is a modifier here and S the Object (Sentence).
I find this especially usefull for Changing words. if you only press cw on a word, you have to have the cursor on the beginning of the word.
With ciw you can change the whole word regardless of the cursor position (Note if you have / or some other seperators in the word, you maybe need ciW)
same letter can have different meanings. E.g. (/{ move to sentences/paragraph back, but ci( or ci{ means change in (...)/{...}.
Same as your s case, s in normal mode alone, does delete & start insert, however in cis, das means sentence.
p case: in normal mode alone, means paste, however in cip, yap ... means paragraph.
:h text-objects
will show you the concept of text-objects. It is a must skill for vim user. ;-)
In Vim, let's say I want to replace the contents of one String with the content of another.
Original input
var1 = "January"
var2 = "February"
Desired output
var1 = "January"
var2 = "January"
What I would usually do is:
Move cursor to line 1
y i " (yank inner quotes)
Move cursor to the destination quote in line 2
v i " p (visual select inner quotes, paste)
While this works well, I generally try to avoid visual mode when possible, so I am not completely satisfied with my step 4 (v i " p).
Is there any way to specify a "destination paste area" without using Visual mode? I suspect it might be something chained to g, but I can't think of anything.
There are many ways of doing this however using visual mode is the easiest.
Use the black hole register to delete the content then paste. e.g. "_di"P
Do ci"<c-r>0. <c-r> inserts the contents of a register
Simply paste and then move over a character and delete the old text. e.g pldt"
However visual mode still has my vote. I find that the concerns most people have is that using visual mode + paste is that the default register is swap with the selected text and it doesn't repeat well. Good news everybody! The 0 register always stores the last yank. The bad news is visual mode still doesn't repeat well. Take a look at this vimcast episode, Pasting from Visual mode, for more information. It mentions a few plugin that help with this.
I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.
It's not particularly pretty, but here goes:
Go to line one and yi"
Move to line two
Type "_di"hp
That deletes what's in the quotes, but sends the deleted text to a black hole register. Then it moves the cursor back one, and pastes what you yanked from line one.
All in all, you can start on the first line and type yi"j"_di"hp. Why is it that people find vim intimidating? ;)
Alternatively, yank the first line as normal, then drop to line two and type ci"<Ctrl+p> and select the previously yanked text from the menu.
Excerpt from my .vimrc:
" Delete to 'black hole' register
nnoremap <leader>d "_d
vnoremap <leader>d "_d
So, you just need to \di"P as your last step (assuming you use \ as a <leader>).
There is a plugin that addresses this problem precisely. It was presumably born out of the same feeling you're feeling, namely that Visual mode often feels less than ideal to advanced Vim users.
operator-replace – Operator to replace text with register content
With operator-replace installed the replacing goes like this.
Yank the word inside the quotes.
yi"
Move to the target line
j
Replace inside the quotes with the yanked text. (I've set up gr as the replace operator: :map gr <Plug>(operator-replace). Pick your own mapping.)
gri"
Check it out! This is part of the truly excellent textobj/operator frameworks. I couldn't work without them.
When I am typing a long code comment in VIM, I manually judge when each comment line reaches 80 characters, then manually typically press < enter >< tab >//< space > and continue on. Likewise it is awkward editing comments, adding or removing text.
// The comments I have to use
// look like this
Ideally, I'd like some kind of comment mode, where you type text, and the 80 line character limit and the // symbols are sorted out automatically. Does anything like this exist?
You can turn on formatting options with set formatoptions=tcq (with tcq each representing an option, there are others as well). Use h formatoptions to see what the various flags are.
In this case you probably want to use set fo+=a.
Personally though, I prefer to just type my comments normally, then when I'm done run gqip. gq is the formatting command, ip for in paragraph. Make sure the comment block is not next to code though or it will suck that up when reformatting your comment.
I use :set textwidth=80 to set the formatting width (actually, 80 is the default).
Then I move the cursor to the first line of the comment and in command mode press gq} to format the comment. It also works for other comment types from other programming languages such as # and /* ... */
A variant on #Alex's suggestion is to select the lines in visual mode and then press gq. This allows you to avoid the problem of gqip reformatting code as well.
Pressing capital V selects an entire line, then you can just move up or down to highlight all the comments and press gq.