Delete everything between two brackets in Vim, including newlines - vim

Say I have the following python array literal:
def f():
arr = [
1,
2,
3
]
I want to delete everything in the brackets so that it becomes this:
def f():
arr = []
How can I do that with minimal commands in vim?
These are some of my attempts:
Using di] will delete the text, but not the empty newlines, leaving a lot of whitespace I'd have to delete:
def f():
arr = [
]
Using da] will delete the newlines, but also the brackets:
def f():
arr =

You can simply do:
ca[[]<Esc>
or:
ca][]<Esc>
See :help text-objects.

With your cursor on the first opening bracket ([), press V, followed by %. This will select the block which you then can join J, followed by di[.

Select the lines in visual mode (v) and use J to remove newlines. Then use di[.
Or if there are many lines, di[ first, after which you move the cursor to top line and then J. This will potentially leave a space between the brackets which has to be removed with x.

I find using a code formatter shortcut saves a lot of time
If you install vim prettier, you could do di[ <leader>p
Using the formatter in JS for example means I don't have to remove extra spacing, jump to end of line to insert ;, or fix indenting etc

Related

Vim shortcut for unindenting till the start of the line

So I'm aware that you can unindent a line with the shortcut Shift+, (or <) and that you can repeatedly apply it by pressing a number before the shortcut -- for example, 5 + < will unindent 5 times.
Is there a way to repeatedly apply unindent until it reaches the start of the line?
One option is ^d0: move to the start of the indented line, then delete to the start of the entire line.
Sometimes it’s just easier to << then mash . to repeat, though.
Moving to the :left
You can move lines to the left with :left . This will remove all indention.
:left
See :h :left for more information.
Putting with indentation
I often find myself wanting to move/cut a block of code and put/paste with the current line's indention level. This can be done with ]p & [p.
Example:
def foo():
pass
if x == 'bar'
print "hello world"
Assuming you are on the if line you can do the following:
dj]p
This will put/paste the block inline with pass yielding:
def foo():
pass
if x == 'bar'
print "hello world"
Note: ]p & [p need the register to be linewise for this to work correctly or use unimpaired.vim
For more help see::h ]p & :h linewise

Vim: delete everything in a line after character without moving cursor in the first place

Suppose the following line and cursor position:
foo = some_func(1 +[ ]2)
^^^
cursor position
Using di + ( I could easily get rid of everything inside the brackets or delete everything to line start or end using d^ and d$ respectively, but what would I do if I would like to delete everything that comes after =?
The resulting line should be:
foo =[ ]
^^^
cursor position
dT+= deletes everything until (backwards) the character =, but it still leaves 2) in the line, ending up in:
foo =[2])
^^^
cursor position
I could, of course, jump to = first and then use d$ to delete everything until line ending, but I would prefer a simple shortcut based on current cursor position without the need to move the cursor (if such a shortcut exists).
T=D (Jump to after =, delete to end of line) is the shortest way, I believe.
You can do it without the movement first, but it is considerably more complex: :s/=\#<=.*// CR

Delete till end of sentence in vim

So I'm playing vim adventures and I got stuck. I need a Vim command that will delete the keys in red. I thought dd would do it, but that only deletes the current line.
Use das or dis to delete a sentence. Use dap or dip to delete a paragraph. See :help text-objects for details. Unrelated to your question, see this wiki page for plugins that provide other, highly useful text objects.
) jumps to the beginning of next sentence, so d) will delete (from the cursor) till the beginning of the next sentence. Vim detects sentences using ., meaning period + space. This does mean that d) will have some problems if your cursor is on either the period or space delimiting two sentences, and will only delete until the first character of the next sentence (meaning it deletes either a space or the period and space, which is almost never what is desired). das will work as you probably expect, deleting the sentence and the delimiter (period + space).
If you specifically want to move (and delete to) the last character in a sentence it is more complicated according to this vi.SE answer:
The solution was either dk which deletes the line and the line above it or dj which deletes the line and the line below it.
My original question was actually not the right question (there are multiple sentences).
To delete to the end of the sentence, from where your cursor is, use the letters, use "d)". the "d" is the delete command object, followed by a motion object ")" which advances the cursor (and the deletion process) to the end of the sentence.
To delete "around" a sentence, including all the extra whitespace, use "das" (delete around sentence). Or to delete inside the sentence, and not all the whitespace, then use "dis" (delete inside sentence).
Once you understand the VIM language, then you can easily memorize a plethora of operations. Use this table to understand VIM's vocabulary:
COUNT NUMERAL + TEXT OBJECT COMMAND + MOTION (or OPERATORS)
"3das" will perform "delete around sentence 3 times"
So, if practical, you could place a numeral followed by...
a command:
d=delete
y=yank (into memory buffer to "put" later)
c=change (delete then insert new text)
and then a motion:
) = move cursor to end of sentence
( = move cursor to beginning of prior sentence
} = move cursor to the next paragraph
{ = move cursor to the beginning of the prior paragraph
w = move cursor to next word
b = move cursor back a word
$ = move cursor to the end of the logical line
0 = (zero) move cursor to the beginning of the logical line
G = move cursor to the end of the file
gg = move cursor to the beginning of the file
h, j, k, or l (you might have to look those up)
OR instead of a Motion, define a field of area using:
a = around
i = inside
followed by the name of the area around the cursor:
s = sentence
p = paragraph
w = word
t = xml-tag <tag example> lots of text between tags </tag example>
< or > = inside or around a tag, frequently found in xml documents
{ [ ( ) ] } = inside or around any type of bracket ...
... {a large area [some more (a little stuff) things] of a great many things }
I actually find this table from the help file the best overview for block commands:
"dl" delete character (alias: "x")
"diw" delete inner word
"daw" delete a word
"diW" delete inner WORD (see |WORD|)
"daW" delete a WORD (see |WORD|)
"dgn" delete the next search pattern match
"dd" delete one line
"dis" delete inner sentence
"das" delete a sentence
"dib" delete inner '(' ')' block
"dab" delete a '(' ')' block
"dip" delete inner paragraph
"dap" delete a paragraph
"diB" delete inner '{' '}' block
"daB" delete a '{' '}' block
So deleting a sentence is das or deleting a paragraph is dap.
If you want to delete from J up to and including the . start at J and use df.
If you want to delete both lines then 2dd
Another option (not sure if it works in the game) is to delete up to and including the period:
d/\./e
You have to escape the period when using a search pattern like this after the delete command.
If you were limited to a single line, it is much simpler:
df.
You can use the command: d2d, but I do not know whether it works in the game.
Vim grammar is [Nubmer] [Operator/ Command] [Motion or Text Object]
So in this case, you can use: 2dd

Vim - delete until (inclusive) character in multiple lines

I have this code:
def foo(c: Char) = c match {
case 'a': 'B'
}
My cursor is on the space after =. I want to delete everything until, including, the }. How can I do that?
Can I do the same where the cursor is anywhere on the first line? Anywhere in the block (and place the cursor after the =)?
d/}/e
does the job.
d/} deletes until the } but adding the /e flag moves the cursor on the last char of the match, effectively deleting everything between the cursor and the }, inclusive.
Using visual selection works too, in a slightly more intuitive way:
v/}<CR>d
Try with this: d%.
The d is for delete and the % moves between braces.
This should work:
d}
This deletes one paragraph forward.
You can achieve something like this with the EasyMotion plugin.

How to add a word at the beginning of multiple lines in vim?

In Vim,
How do i add a word at the beginning of all lines?
Also how do i add it at end?
Eg..
If i have
A
B
C
D
I want to make it to
int A =
int B =
etc..
use visual block mode (Ctrl-v) to select the column you want, and then hit I, type the characters you want, and then hit Esc
So in this case, you'd put your cursor on A, hit Ctrl-v, go down to D, hit I and type int (it'll only appear on the first line while you type it), and then hit Esc at which point it'll apply that insert to all visually selected portions.
This works for anywhere in the document, beginning of line or end of line.
:he v_b_I for more info on Visual Block Insert
You can do this:
:%s/^/at the beginning/
:%s/$/at the end/
:%s/.\+/int & =
+ won't match on empty lines
If you need to copy just the first word, then do:
:%s/^\w\+/int & =/g
If you want to preserve indentation, then do:
:%s/^\(\s*\)\(\w\+\)/\1int \2 =/g
A global substitute should do i:
:%s/.\+/int & =/
This is how it works: in the second part of the substitution (ie in the int & =) the ampersand is replaced with what machted in the first part (the .*). Since .* matches the entire line, each line is subsituted as wanted.
If you have empty lines (in which you don't want to have any replacements), you could go with a
:%s/^\S\+$/int & =/

Resources