Vim change & jump lists override behaviour - vim

I'm new at vim, started to play with macroses, and founded not obvious override behaviour for jump/change lists.
The main question - are there any stable ways to jump by changes/moves inside same line (except marking each point), cause this lists remember only last change on the same line (if changes are consequtive withing on line).
Example for jumplist. Cursor position at '(', '{' - marked as m.
( smth ) { } %`m%
I jumped firt to ')', next by mark at the same line and next to last bracket
I'm able to do only one ^o - last jump at first line. But expected to jump 2 times more.
Same situation with changes list and g;
't1 t2 t3' -> 't2 t3 t4', I can't move between all 3 t
At least I wanted to jump by same points as stored at undo history. All modern editors like VSCode, Sublime, e.t.c. have this stable option "Alt -" / "Alt +" to jump all prev. cursor positions. What is Vim way?
Founded this when tried to make macro, which swaps any 2 pair of brackets.
{ smth } (
)
Let's mark first pair of brackets with 'mm', to remember whith whom should we swap.
let's mark '{' as main bracket and go to '(', starting macro 'qq'. I'm marking current pair of brackets as 'mc' and swapping later between main and current.
#q=mc%x`m%Plx^O^Op`mx`cPlx`mP
( smth ) {
}
moving cursor to { and running '#q' - we get initial text, double swapping brackets
Problems started, case second pair brackets marked as mm, and cursor located at first pair of brackets. '(' - mm, '{' - start position
{ smth } (
)
only first ^o would work, and then macro will jump outside of this block

There are two rules: "small" moves are never remembered; and, "changes" are remembered upon leaving from insert mode.
So all those macros and stuff they are sort of "batch processing tools", not "usual line editing tools".
Instead, to get t2 t3 t4 from t1 t2 t3 you could do ctrl-a w . w . Or to swap the brackets with d%%p. And so on.
This sort of "small edits" should be done by normals in Vim. So you need to master the normals first. Maybe reading the "Practical Vim" by Drew Neil could help.

Related

Matching square brackets in vim

I am aware of the shortcut for flower braces i.e. Having cursor over { and pressing ]} gets me to the corresponding }. How to do the same for matching square brackets? []
In normal mode, try
vi[oh
vi[ should visually select the entire block between [], and o switch cursor to the front of visual selection, then h moves cursor onto [.
At the expense of more keystrokes, this works in more cases than % where you must hover over the bracket.

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

Quickest way to switch order of comma-sparated list in Vim

Supose I have a function such as
myfunc(arg1 = whatever, arg2 = different)
I would like to transform it to
myfunc(arg2 = different, arg1 = whatever)
What is the quickest command sequence to achieve this? suppose the cursor is on the first "m". My best attempt is fadt,lpldt)%p.
There is a vim plugin: vim-exchange
visual select arg1 = whatever
press Shiftx
visual select arg2 = different
press Shiftx
I would recommend you change it a bit so it will work from wherever the cursor is and so that it will work on any arguments:
0f(ldt,lpldt)%p
All I changed from your method was I added 0 to move the cursor to the beginning and I changed fa to f(l so that it will work regardless of argument name.
Now you can either put this into a macro, or, if you use it a lot, you can make it a mapping:
nnoremap <C-k> 0f(ldt,lpldt)%p
I arbitrarily chose Ctrl-k here put you can use whatever you like.
I wrote a plugin for manipulating function arguments called Argumentative. With it you just execute >, and the argument your cursor is on will shift to the right. It also provides argument text object in the form of i, and a,.
With pure vim, with your cursor at the start of the line:
%3dB%pldt,lp
This is the quickest I could think of on the spot (12 strokes).
This should work for all names as long as there is always a space around the equal signs.
% " Jump to closing brace
3dB " Delete to the beginning of 3 WORDS, backwards
% " Jump to the beginning brace
p " Paste the deleted text from the default register
l " Move right one character
dt, " Delete until the next comma
l " Move right one character
p " paste the deleted text from the default register
You could also turn this into a Macro to use at any time.

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.

Resources