I would like to create a mapped vim command that helps me align variable assignments across multiple lines. Imagine that I have the following text in a file:
foo = 1; barbar = 2; asdfasd = 3; jjkjfh = 4; baz = 5;
If I select multiple lines and use the regex below, noting that column 10 is in a space for all lines, the space after column 10 will be removed to the equal sign.
:'<,'>s/^\(.\{10}\)\s*\(=.*\)$/\1\2/g
Here is the result:
foo = 1; barbar = 2; asdfasd = 3; jjkjfh = 4; baz = 5;
Is there a way to get the current cursor position (in particular, the column position) when making a selection of a visual block and use this column in a regular expression?
Alternatively, if you can find the max column for any of the equal signs on the selected rows and insert a space so that everyone is equal to the signs aligned in the column, which is preferred to solve the previous problem. Imagine you are quickly converting:
foo = 1; barbar = 2; asdfasd = 3; jjkjfh = 4; baz = 5;
in
foo = 1; barbar = 2; asdfasd = 3; jjkjfh = 4; baz = 5;
with block selection and key combination.
chardson
source share