vim: insert successive numbers in a row - vim

Let's say I want to initialize this tuple:
t = (
#(id, name)
(1, 'aasd'),
(2, 'bsfd'),
(3, 'asf'),
...
(21, 'aefae'),
)
I am sure I can do as follow with vim.
1/ Type this:
t = (
#(id, name)
(, 'aasd'),
(, 'bsfd'),
(, 'asf'),
...
(, 'aefae'),
)
2/ Visual select comma row, and type a tricky key sequence which would write successive number
Does anyone know what is the tricky key sequence I should type?

Instead of using VisIncr newer vims (starting with Version 8) support incrementing in visual mode. So I would go with:
Press Ctrl-V and mark the column with the commas
I1ESC to initialize each column to 1
Visually block select the second to last row (using e.g. gvj)
press gCtrl-A to have each row sequentially incremented.

This can be solved with a macro
Position the cursor on the first number: 3Gf1
Start recording: qq
Yank the number lyT, go one down j, paste P, increment ^A (Ctrl+ A), stop recording q.
Execute the macro for the remaining lines: 20#q
All together: 3Gf1lyT(jP^Aq20#q
To avoid the counting, and apply the increment until there are no more lines, you can also turn this into a recursive macro:
Position the cursor on the first number: 3Gf1
Clear macro register q and start recording: qqqqq
Yank the number lyT, go one down j, paste P, increment ^A (Ctrl+ A), re-invoke macro #q. All together: lyT(jP^A#q

Using the VisIncr plugin:
press Ctrl-v and mark the column with the commas (that is, where you want the numbers)
Shift-i1Esc - this should insert a column of 1s
gv - mark the column of 1s
:I - this should change the column of 1s into numbers 1 ... 21.

Related

How to move/shift around several rows in V-line mode using vim. Lines are 2 rows apart

For example, I want to select lines starting with B28, B29 and B30 using Shift + v in row B28 , then select row B29 and so on..., then press 'd' and then move to ROW 1 and press 'Shift-p' in the first row to paste all these rows there.
ROW 1
A26 51.00824
D26 35.94841
D27 35.94841
B28 7.07486
A28 35.95497
D28 179.99932
B29 4.15400
A29 90.00068
D29 179.99932
B30 7.07490
Visual mode(s) can only select contiguous regions (this applies to characterwise, linewise, and blockwise visual mode, regardless of the value of virtualedit). The only exception is ragged line-endings with, say, vip$.
But you can accomplish your goal other ways. For example:
:global/^B\d+/move /ROW 1/-
Should move all lines starting with B followed by digits to the line after ROW 1. (They will probably be reversed; in your case, a simple :sort n will probably be do, but generally :[range]!tac or :[range]global/./move <firstline> can reverse lines.)
Or, you could record a macro like so:
Mark insertion point: :/ROW 1/mark a
start a recursive macro in register q: qqqq (the first three clear the register)
go to next occurence to move /^B\d+
move it dd'ap
adjust mark ma
recursive invocation #q
fini q
Now hit #q and watch the magic.
If you needed to repeat the above many times for different things, I would write a series of commands to get it working once, then turn that into a function and generalize the things that are variable. VoilĂ , automation.
Another method, just for fun:
:g/^B/normal! dd1G}P
:g/^B/<cmd> executes <cmd> on every line starting with a B,
normal! <macro> executes normal mode macro <macro>,
dd cuts the line to the unnamed register,
1G moves the cursor to line 1,
} moves the cursor to the empty line after the current paragraph, this is key because it allows us to put the next line below the last one that was put and thus to respect order,
P puts the content of the unnamed register above the current line.
Reference:
:help :global
:help :normal
:help /^
:help dd
:help G
:help }
:help P
--- EDIT ---
There are plenty of ways to address your target, even if it is not on line 1.
With a line number:
:g/^B/normal! dd23G}P
With a mark:
ma
:g/^B/normal! dd'a}P
With a search:
:g/^B/normal! dd?ROW1^M}P " ^M is obtained with <C-v><CR>

How to select all LINES between parenthesises in VIM

Say I have the following code
aaa;
bbb;
ccc void () {
xxx;
yyy;
}
ddd;
eee;
Now suppose my cursor is at yyy. I'd like to highlight all code between the parenthesis { and } inclusive of the complete line the brackets are on. This means the highlight will look like
before select
after select
va} is not a solution as that produces this
Effectively it should be a linewise selection. But the corresponding "text-object" forces a charwise one (so there's no difference between va{ and Va{).
However, you can make a selection linewise anytime. So va{V achieves the desired result.
I'm not sure if any mapping is needed at all. But at least ab should not be touched as it normally stands for parentheses ("()-block").
vnoremap aB aBV
Now vaB will select {}-block linewise, while va{ will do "normal" {}-block selection.
nmap vab va{$o0
Breaking it down
vab
highlights within the brackets inclusive of the brackets. The cursor finishes at the end of the highlight.
$
moves the cursor to the end of the line
o
moves the cursor to the other end of the highlight block
0
moves the cursor to the start of the line

Align at longest word

I have the following code:
a = 123
p.value 0.123
p.long.name = "abc"
How can I align each line like shown below in vim?
a = 123
p.value = 0.123
p.long.name = "abc"
Thanks for any hints.
Without plugin:
:%s/=/ &/
:%s/\%13c\s\+=/=
First command will insert spaces before first equal signs on all lines, second one will remove all spaces before an equal sign at 13th column. You could also use Visual block selection and <..... to shift left as many times as necessary.
However this is really unclean. With the tabular plugin you just type :Tab /=/ and this will do the work and the range will be calculated automatically (greatest range around the cursor in which all lines match the pattern).

vim shortcut to traverse the word from right to left

Vim shortcut to traverse the words in forward direction i.e left to right is w / W
What is the shortcut to go in the opposite direction?
Similarly x deletes character on which cursor is there. So continuously pressing it deletes characters from left to right. Is there a shortcut to delete characters from right to left i.e characters before the cursor.
b goes backwards to the previous beginning of a word.
X will delete the character before the cursor
db will delete from the cursor position to the previous beginning of a word (excluding the letter at the cursor). e.g. ([] is the cursor position)
first middle la[s]t " type b
first middle [l]ast " type b again
first [m]iddle last " type db
[m]iddle last
Of course b is just a movement command, so you can tack it on to any command that takes a movement, e.g. cb will cut/change to the beginning of the word, vb will highlight it, etc.

Concept of Index in Vim

Using Vim, how can I print the numbers, say, 1 through 100 on consecutive lines?
e.g.
1
2
3
.
.
100
Type:
1
Then record a macro:
qa (start recording a macro stored in register a)
yy (copies the line)
p (inserts that line below)
CTRL+A (increases number by one)
q (stops recording)
Next you can invoke that macro 99 times, by:
99#a
One way:
:call append( 0, range(1,100) )
First parameter is line number to begin the insert. Use line() function to choose another one.
Type 1 on the first line in insert mode. Leave insert mode.
Then enter in 'macro mode' with qa
type yyp then CTRLA
Leave macro mode by retyping q
Now type 98#a to invoke 98 times the macro creating the new line and increasing by one the count
You can use a simple for loop:
:for i in range(1, 100) | silent put = i | endfor
In insert mode: CTRL-R=join(range(1,100),"\n")Enter.
If you're not in insert mode it's tricky because you have to escape " and | :
:put =join(range(1,100),\"\n\")

Resources