Vim: Moving cursor to the end of a block - vscodevim

Given that I am in the middle of a code block, I want to go to the end of the block without using h,j,k,l.
I am looking for similar behaviour with %, which will also work when my cursor is somewhere in the code block.
Example:
I want to move the cursor to where to the line 22, where Container block ends.
I would appreciate for your help, thanks.

Take a look at the following commands:
]) -> Move the cursor to the closed closing of first bracket '('
]} -> Move the cursor to the closed closing of second bracket '{'
As you are looking for the nearest first bracket, the first one should work for your case.

Related

Insert visual vim selection in the line below at the same position

I have a tedious task of converting chords from leadsheets into an inline format.
The format I have
G A
Never gonna give you up
F#m Bm
Never gonna let you down
G A F# Bm
Never gonna run around and desert you
And the format I need would b
Never gonna g[G]ive you [A]up
Never gonna l[F#m]et you do[Bm]wn
Never gonna r[G]un a[A]round and d[F#]esert [Bm]you
The problem with a script in Ruby or Python is, that the format isn't very consistent. So there could be newlines, weird characters, sometimes the chords are seperated with a dash... I haven't looked through all of the files, but I suspect more malformed stuff.
So I thought, if I have to go through of every leadsheet by hand, at least I can save some time using vim. This is the mapping I've come up with so far:
nmap <C-X> viWc[]<ESC>PviWx<down>p
Select the chord: viW
delete it and go into insert mode: c
add brackets
exit insert mode: <ESC>
paste the register to the left: P
select the chord again, including the brackets: viW
delete the selection: x
move cursor down, and paste it: p
But I few things I can't figure out is:
Sometimes the chords already have brackets, then I don't want to surround them with more brackets. Any idea how to only add brackets, if the chord isn't already surrounded with them?
It would be cool to do this with whole lines of chords. Sometimes there are multiple chords on the same line, and selecting them one by one is tedious. Any idea on how to operate the mapping above on a whole line?
well point 1 could be solved by adding the brackets around the chords on every second line:
:g/^/s/\[\=\zs[a-zA-Z#]\+\ze]\=/\[\0\]/g|+t+|-d
Credits: https://vi.stackexchange.com/questions/34785/search-and-replace-on-odd-even-numbered-lines-using-g
This however sucks, because it moves arround the chords, so we have to remove all brackets first and replace them with space
:%s/\[\([a-zA-Z#]\+\)\]/\1 /g
Then we can do the first line again, but remove some space too. Since there are no brackets left, it gets simpler (Note we use other brackets to get ride of some side effect the following code has):
:g/^/s/\([a-zA-Z#]\+\) \{0,3\}/\{\1\} /g|+t+|-d
Also we add a trailing whitespace at the end of the line so that the df} command will not move the cursor to a wrong place
Now that we have curly brackets everywhere, we can use reverse search with ?{ and then create a macro that jumps from results to result and moves it down, replaces the curly brackets with normal brackets and then calls itself (recursive macro):
gg0
qqq
?{<CR>
qq
df}jPr]F{r[n#qq
#q
And nearly all should be done.
The result:
Never gonna g[G]ive you [A]up
Never gonna l[F#m]et you dow[Bm]n
Never gonna r[G]un a[A]round and d[F#]esert [Bm]you
Note, we have to search backwards (? instead of /) so we can delete the chords directly.
If you have problems understanding what I did, feel free to ask.

How to delete a paragraph as quickly as possible

I want to delete the following codes in my config file:
server {
listen 80;
server_name xxx;
location / {
try_files xx;
}
}
I know I can use 7dd, but this is not handy enough- if the section is too long, counting the rows would be inconvenient.
Is there a better way to do this?
Sometimes, I have to delete a whole function, any ideas for that?
As in common in Vim, there are a bunch of ways!
Note that the first two solutions depend on an absence of blank lines within the block.
If your cursor is on the server line, try d}. It will delete everything to the next block.
Within the entry itself, dap will delete the 'paragraph'.
You can delete a curly brack block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).
You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter
]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect.
If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.
Note that much of this answer is taken from previous answer I gave on a similar topic.
If there is a blank line immediately after these lines, Vim will identify it as a paragraph, so you can simply use d} to delete it (assuming the cursor is on the first line).
Try using visual mode with %.
For example, say your cursor is at the beginning of the "server" line.
Press v to go into visual mode. Press % to highlight to the end of the block. Press d to delete it.
As David said their are many ways. Here are a few that I like:
Vf{%d This assumes you are on the line w/ server and you do a visual line selection, find the { and then find the matching } via the % command.
set relativenumber or set rnu for short. This turns on line numbering relative to your cursor. So you do not have to count the lines just look and 7dd away to deleting your block.
Use VaB to visually select a block i.e. { to }, line-wise. While still in visual mode continue doing aB until the proper block is selected then execute d. This means you can select a block from inside the block instead of at the start or end.
d} will delete a paragraph. This works in your current scenario because there is no blank line inside the block. If there is one then all bets are off. Although you can continue pressing . until the block is properly deleted.
If inside a block you can jump to the current block via [{ then continue executing [{ until you are at the correct block then V%d or d%dd to delete the block
Using the techniques above you can delete a function as well, but you can also use the [M and friends ([m, ]m, ]M) to jump to the start and endings of methods but they commonly work for functions as well.
For more information
:h %
:h 'rnu'
:h aB
:h }
:h .
:h [{
:h [m
Given matching parens, or braces, you can use % with d to delete, spanning lines.
So, assuming that you're on the server { line, you can do d%. This is generally useful for all code blocks, e.g. function blocks, loop blocks, try blocks.
Similarly, dip works, but d} is shorter. However, both will only delete to the next empty line.

Put cursor at the end of selected block after some operation

Couple of examples that I encounter quite often.
Indenting some code
E.g. I want to put code block inside if statement:
puts("hello");
puts("world");
I enter if (pid > 0) { before the first line, then press Esc,j,Shift+v,j,>:
if (pid > 0) {
puts("hello");
puts("world");
Now cursor is at the beginnig of the first puts and to enter closing bracket I need one extra j to go to the end of the indented block.
Pasting block and adding to end of it
Almost the same example. When extracting some code to function, after pasting it with p I need to append return statement or closing bracket at the end. If I'm lucky there will be empty line after pasted block and I can use } to jump right there, but it's not always the case.
Question
So the question is how to jump to the end of the selected block after some operation performed on it (pasting, indenting, etc)?
`]
Will move you to the end of yanked or previously changed text.
See
:h `]
As an alternative behaviour, you could insert the closing } first and then indent the code inside the new scope automatically.
Being at the last line of the new scope in normal mode:
o}ESCv%=

Move from first character of function declaration to ending brace in VIM

If I have a function declared as such:
public static void main(String[] args){
blahblahlbah;
}
Is there anyway to move from the "p" in public to the ending "}" of the function?
It can be assumed that the method body is of considerable length and does contain curly braces if that makes a difference.
Thanks!
Try this key sequence: f{% Should do it.
There is not a few ways to accomplish the movement.
The most appropriate motion command is
]M
which is short and to the point: It moves the cursor to the end of the next
method in a Java-like source code.
There are also three satellite motions that together with ]M give
possibility to jump to next or previous starts and ends of methods, see :help
]m and below. Therefore, in addition to ]M, in this situation one can
issue
]m%
The rest of the answer contains discussion of some tricks that should be used
only if the aforementioned motion commands for some reason have failed to
solve the problem in your case.
Another simple idea to take advantage of is to jump over the argument list,
find the next opening curly brace and move to its closing counterpart,
%l%
or
f{%
or even
/%Enter%
A similar idea to move the cursor to the very beginning of the method's body
and than jump to the next unmatched closing curly brace leads to the following
command,
j]}
Note, though, that three of the last four commands work only if the function
header is a single line. If that is not the case, they need modifications.
Under some assumptions on the code formatting, it is also possible to achieve
the same result using plain-text-oriented movements. If the opening curly
brace is the last character on the method header line, one can use
$%
or
g_%
if that brace is the last non-blank character.
In conclusion, stick to the ]M movement as far as it works for you (it
should, in the vast majority of cases), fall back upon tricks based on
combinations of other text motion commands, otherwise.
/{ will take you to the opening brace and % will jump to the matching closing one.
][ moves to the next } in the first column. So this will work for you as long as your function is properly indented.
See :help ][ and :help section
I use this while searching through a file and my cursor is positioned over a particular search result in the middle of some long function. Often, I'll want to know which function I'm in so ][% gets me to the function signature, and then n returns me to the search result where I started.

How does one yank a function call in vim?

Suppose a line of text reads:
$x = ( frobnicate( foo( bar( $x, $y )[ 1 ]))[ 1 ]);
The cursor is on the 'f' of 'frobnicate' and I want to yank the text which includes the call to frobnicate. (That is, everything up to the 3rd closing parenthesis. I can certainly do:
y3f)
or do it interactively with
vf);;y
but neither of these is appealing. (I don't want to have to count the '3' manually, nor do the repeated find until I hit the end point.) Is there an easy way to accomplish the move from 'f' to the matching closing paren? I'm thinking something like the v_i 'inner word' motion command.
y% seems to work.
From help %:
Find the next item in this line after
or under the cursor and jump to its
match. inclusive motion. Items can
be: ([{}]) parenthesis or
(curly/square) brackets
You could try yel% to position the cursor to the opening parentheses and then jump to the closing one with %, but it does not look much more efficient than your current solution.
The only advantage is that you don't have to count anything, and it works with nested call.
Edit :
as pointed by Peter, y% works and is a superior solution (even if I don't quite understand why it works. Vim is amazing!)

Resources