Align columns in VI - vim

I have a bunch of lines that I'd like to split into two columns, and get the data from each column. The data looks something like this:
current_well.well_number
current_well.well_name
current_well.well_type_code
well_location.section
well_location.range
Essentially what I'd like to do is split the line based off of the period, turn the data into two columns, and then grab the data for each column. I know this can be done in Excel, but I'm really interested in VIs solution for this problem. I know that
%s/\./
will format the string with empty spaces. But once I have the data looking like:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
How do I grab all the values for each column so I can paste it into another application?

The linux column command works well for creating the columns
:%!column -s . -t
Then use block copy.

One option is to use this Align plugin to line up the periods so you can more easily select a column in Visual Block mode. e.g. if you do this:
:%Align \.
You'll end up with:
current_well . well_number
current_well . well_name
current_well . well_type_code
well_location . section
well_location . range
If you don't want to use a plugin, try padding your columns with spaces. e.g. for your text:
:%s/\v(.*)\.(.*)/\=printf("%-16s %s", submatch(1), submatch(2))/
That'll leave you with:
current_well well_number
current_well well_name
current_well well_type_code
well_location section
well_location range
Then you can Ctrl-V and select a column of text to copy. Just make sure you pick a column width wider than your widest value.

I've run into this problem a number of times. Changing the dots to tabs will line them up mostly nice, but not really. It's true that all the columns will start on a tabstop, but there's no guarantee that they'll be on the same tabstop. For instance, if this was your original text:
lt1tab.value1
gt1tabstop.value2
and you do:
%s/\./\t/g
and assuming a tabstop is 8 spaces, you'll get:
lt1tab value1
gt1tabstop value2
What you might want to do instead is remove everything but the last column (or whichever column you want). For instance:
%s/^.*\.//
will leave you with:
value1
value2
Which you can easily copy and paste.
If you don't have to use Vim, you can use unix's cut to do what you want:
cut -f2 -d. input_file > output_file

I do this all the time, simply by padding with a lot of space:
%s/./ /
Then <c-v> to enter block mode in the empty space, draw the column position I want, < to shift the text to the right toward my column, then hold down .. Takes seconds.
Another way you can do it if you know the specific column position you want to line up on: go to the first line and start recording (qq), find your delimeter (f.), insert a bunch of space (20i <esc>), delete space back to a specific column (d15|), then move down a line (j). Then you just hold down shift and roll your fingers across #Q a bunch of times (or use a count 10#q) until all the columns are lined up. :)

Obviously, the above methods with block select and/or Excel are easier. But I'm a masochist and I decided to assume you didn't have access to either of those and try to do it with only vi commands. Here's the horrible thing I thought up:
My basic plan is to turn the list of joined columns into two lists, one after the other. This basically involves breaking each row into two lines, then copying every other line to the end of the file.
So, first we have to break each line into two lines with this command:
:%s/\./^M/
Next, swing by the bottom of the file and create an empty line, then return to the first line. This will help with readability later.
Go[Esc]
:1
Now, you need to map the following sequence to your favorite key:
:map [Key] mkjddGp'kj
(For the record, this marks the current row, deletes the row below it, pastes that row at the bottom of the file, returns to the row you started from, and then moves to the next row.)
Finally, press the mapped key once for every row in your list. So, with the example list, you'd press it 5 times. Make sure you start from the first line in the list!!
What you'll have at the end is the following:
current_well
current_well
current_well
well_location
well_location
well_number
well_name
well_type_code
section
range
You can now easily copy each list to wherever you need to put them.

Ctrl V selects a column

Why not block visual mode ? C-v, select what you want, and paste it in some other application.
For that kind of pasting, you should probably set
:set guioptions+=a
as well.
Hmm, now that I've re-read your question. Are you asking how to divide the data into columns, or how to paste it in some other app ?
I think I got it now (3rd reading).
Do as you've done and get the spaces where the dots used to be. Then do another substitution, and replace the spaces with tabs, each tab being, 15 or something. It will line them up nicely.
Then you can select and copy them to wherever.
You can also match the regex to select the second column for example, but although it is light up like a christmas tree, you won't be able to yank it.

I'd convert the dots to tabs, then block select each column:
:%s/\./CtrlV-Tab/g
CtrlV to then block select the columns.
Or, as you'd have tab separated values, they will paste directly into columns in Excel.

At the top of the file:
!}awk -F. '{print $1}' >> %
You will be told the file has changed. Select the L (load) option.
Add a blank line under the original lines, go back to the top.
for(i=1;i<=NR;i++);print $2 >> %
Again select the L option
Delete the original lines.
You now have the first and second parts in two groups.

One possible solution:
w (or W for WORDS) jumps to next word,
CTRL-R CTRL-W gets the word under cursor and pastes it into command-line
Now you have to chain those commands in a loop and then output them in your own manner.

You can select columns in gvim by typing either Ctrl-VCtrl-G or Ctrl-QCtrl-G (depending on if you are using windows insert key mappings) and then selecting with the mouse.

nnoremap <BS> v"by:%!column -t -s<C-r>b -o' '<CR>
This is a nice solution following on from #NES & #SergioAraujo idea. Put it in your vimrc.
USAGE. Move the cursor to the delimiter. Then hit Backspace.
Explanation we use the named register b to hold our delimiter. Then substitute it as the variable for the --separator option of the column UNIX utility.
And Backspace is a good key because in normal mode is the same as h. But then again you might also be prone to hitting it accidentally.

Related

How to append each line from 11-20 to each line from 1-10 in VIM

I have a file in which first 10 lines are the columns of a table and the rest 10 lines are the values of each column.
How can I use norm in VIM to append the values after each column names like this:
column1
...
column10
value1
...value10
-->
column1: value1
...
column10: value10
It is a little similar with this(Vim - Copy Nth word of each line, from line 10-100, to end of line), but I don't know how to go to line 1:10 and append the copied lines.
Any idea will be appreciated!
Fairly naive and crude way to do this, but:
:1,10norm! 10j0d$10kA: ^[p
Explanation:
1,10norm!: for lines 1 to 10, do the following (the ! means any custom mapping you have will be ignored, thanks to D. Ben Knoble for reminding of this):
10j: move down 10 lines
0d$: delete the whole line (not including newline)
10k: move back up 10 lines
A:: append (at the end of the line) ': ' (note the trailing space)
^[: input escape character, going back to normal mode. This (^[) is a single character and is inputted by typing Ctrl-v then escape, not by typing ^[.
p: paste the line deleted in step 3
Another (copy-pastable) way, (ab)using the substitute command:
:1,10s/\v(.*)\ze(.*\n){10}(.*)/\1: \3/ | 11,20d
which does:
1,10s/: for lines 1 to 10, execute the following substitution:
\v: use very-magic regex mode (see :help \v)
(.*): capture the entire current line (eg column1)
\ze: signal the end of the match. This way everything read (and captured) afterwards will not be affected (but can still be read)
(.*\n){10}: skip 10 (including current) lines, ie skip selector to 10 lines below
(.*): capture the line (eg value1)
/: end the 'select' part of the substitute command
\1: \3: replace with captured groups (eg column1: value1)
|: command separator
11,20d: delete lines 11 to 20
Use blockwise-visual mode to perform the operations.
You can enter visual block mode with Ctrl-V and it allows you to select and operate on columns. It also allows you to perform the same action on a block, which you can use to add the : to the lines with the column names.
I'll use normal Vim syntax for keystrokes in my examples, <C-v> means Ctrl-V.
Start by deleting the values into the default register, using a visual block:
11G<C-v>9j$d
Then Add the : to the column lines, also using a visual block:
1G<C-v>9j$A: <Esc>
Then add some more spaces to the first line, to ensure there's room for all the column names to fit:
A <Esc>
Finally, put the visual block at the end of the first line:
$p
It will actually put it on all lines all the way to the end.
This is slightly different from what you specified, since the values are all aligned on the same column. If you want different spacing, you can perhaps use a :s operation to fix spacing.
10:s/: */: /<cr>
Depending on where you pasted (if some column names had more trailing spaces than the first one), you might have some trailing spaces after the pasted values to fix as well, but that should be easy to do using a similar procedure.
Visual block operations are really powerful, it's a great feature to learn and keep in your "toolbox" in Vim. They're really handy with this kind of problem where thinking in "columns" makes the most sense.

VIM to delete a range of lines into a register

I am trying to delete a range of lines into a register a. Is this the easiest way to achieve this?
:5,10d a
The definition of "easiest" depends on what do you have, and what do you want to do
if you have a start line number and end number, e.g.
:2349,5344d a
is the easiest way.
You don't have to consider the questions like
"where is my cursor?"
"how many lines would be removed?"
...
If you are about to remove a small amount of lines, particularly they are on same screen. (You could use relative-linenumber.) for example: "a5dd but you have to move your cursor to the first line you want to delete. And this could be done by option 1 too: 5:d a<CR> (vim will automatically translate it into .,.+5d a<CR>)
If you just know the 1st line of deletion, and find the last line you want to delete by reading your text, (of course, small amount of lines) you could press V, and press j by reading, when it reaches the deletion ending border, press "ad
If the "range" in your question is the "range" concept in vim, The first option would be better. since it could be 234,540, it could be 1;/foo, /foo/,/bar/... :h range see detail
so back to the first sentence in my answer, There is no absolutely easiest way. It all depends on what do you have, and what do you want to do.
The other way to achieve this would be to highlight the range of lines in visual line mode. (Shift-V)
Then type "ad while in visual line mode. This will put the deleted lines into the a register.
" followed by a register puts the next delete, yank or put into that register.
Below is the documentation for " (quote)
*quote*
"{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank
or put (use uppercase character to append with
delete and yank) ({.%#:} only work with put).
Another example of deleting multiple lines and putting it in a register. To delete 6 lines and put them in a register you can got to the line and type "a6dd. This puts the 6 deleted lines into register a.

Placing characters at a specific column in vi

Is there a way to place a character at a specific line in vim, even if the line is short?
For example, I'm contributing to a project which has a comment block style which is 79 columns wide, with a comment character at either end e.g.
!--------------!
! Comment !
! More Comment !
!--------------!
but it's really annoying to space over to it, even with guessing large numbers (35i< SPACE>< ESC>)
Is there a simple command which will do this for me, or a macro or something I could write?
set ve=all
then you could move (h,j,k,l) to anywhere you want. no matter how short your line is.
there are 4 options block, all, insert, onemore for details:
check :h virtualedit
in this way, after you typed short comment, then type <ESC>080l to go to the right place to the tailing !
you can map it too, if it is often used
then it works like this:
Put this in your .vimrc file and restart vim:
inoremap <F1> <C-r>=repeat(' ', 79-virtcol('.'))<CR>!<CR>
With this just press F1 (or whatever key you map) in insert mode after entering comment text to automatically pad with spaces and insert a ! in column 79.
Another simple way is to keep an empty comment box of the correct size somewhere, yank/paste it where needed and just Replace the spaces in it with your comment each time.
If you want to reformat a box that is too short, one way is to start from the comment in your example, make a Visual Block (Ctrl+v) selecting the single column just to the left of its right-hand edge, yank it (y), then repeatedly paste it (p). This will successively move the entire right-hand side of the comment one step, extending the box rightward. Repeat until it has the desired length.
If you already entered the comment text, you can use a macro to add the right-hand ! mark at the correct place. For example, record a macro (qa) that appends more characters than are needed for any line (e.g. 80ASpaceEsc), then use the goto column (|) to go to the correct place (79|) and replace the excess characters from there (C!Esc), then move down one line (j), and stop recording (q). Repeating this macro (#a) then "fixes" each line in turn and moves to the next. In total: qa80A<space><esc>79|C!<esc>jq and then #a whenever needed. Sounds complex but is convenient once you have it.
There are certainly good answers here already, particularly the virtualedit answer. However, I don't see the method which seems most intuitive to me. I would create an empty line for the last row of the comment which is just surrounded by the exclamation points. Then I would yank and paste a new copy of the empty line, go to the old empty line and go to the point at which I want to edit and use overstrike mode (R) to add my text without affecting the placement of the ending exclamation point.
Sometimes the simplest methods, while slightly more clunky, are the easiest to use and remember.
I usually just copy and paste an existing line in the comment block (or copy one from another file) and then modify it. If the text you're replacing is about the same size as what you want to write (e.g., if you're changing the author's name), you probably only need to add or delete a few spaces to make everything line up. It's a lot less painful than spacing out to the desired width.
If you ever have a block that gets too long this way, a neat trick is to place the cursor on the 79th column and press dw. That will clear all spaces up to the ! at the end.
My AlignFromCursor plugin offers commands and mappings that align only the text to the right of the cursor, and keep the text to the left unmodified. So, with the cursor in the whitespace left of the trailing !, you can align it with <Leader>ri.

How to insert a block of white spaces starting at the cursor position in vi?

Suppose I have the piece of text below with the cursor staying at the first A currently,
AAAA
BBB
CC
D
How can I add spaces in front of each line to make it like, and it would be great if the number of columns of spaces can be specified on-the-fly, e.g., two here.
AAAA
BBB
CC
D
I would imagine there is a way to do it quickly in visual mode, but any ideas?
Currently I'm copying the first column of text in visual mode twice, and replace the entire two column to spaces, which involves > 5 keystrokes, too cumbersome.
Constraint:
Sorry that I didn't state the question clearly and might create some confusions.
The target is only part of a larger file, so it would be great if the number of rows and columns starting from the first A can be specified.
Edit:
Thank both #DeepYellow and #Johnsyweb, apparently >} and >ap are all great tips that I was not aware of, and they both could be valid answers before I clarified on the specific requirement for the answer to my question, but in any case, #luser droog 's answer stands out as the only viable answer. Thank you everyone!
I'd use :%s/^/ /
You could also specify a range of lines :10,15s/^/ /
Or a relative range :.,+5s/^/ /
Or use regular expressions for the locations :/A/,/D/>.
For copying code to paste on SO, I usually use sed from the terminal sed 's/^/ /' filename
Shortcut
I just learned a new trick for this. You enter visual mode v, select the region (with regular movement commands), then hit : which gives you this:
:'<,'>
ready for you to type just the command part of the above commands, the marks '< and '> being automatically set to the bounds of the visual selection.
To select and indent the current paragraph:
vip>
or
vip:>
followed by enter.
Edit:
As requested in the comments, you can also add spaces to the middle of a line using a regex quantifier \{n} on the any meta-character ..
:%s/^.\{14}/& /
This adds a space 14 chars from the left on each line. Of course % could be replaced by any of the above options for specifying the range of an ex command.
When on the first A, I'd go in block visual mode ctrl-v, select the lines you want to modify, press I (insert mode with capital i), and apply any changes I want for the first line. Leaving visual mode esc will apply all changes on the first line to all lines.
Probably not the most efficient on number of key-strokes, but gives you all the freedom you want before leaving visual mode. I don't like it when I have to specify by hand the line and column range in a regex command.
I'd use >}.
Where...
>: Shifts right and
}: means until the end of the paragraph
Hope this helps.
Ctrl + v (to enter in visual mode)
Use the arrow keys to select the lines
Shift + i (takes you to insert mode)
Hit space keys or whatever you want to type in front of the selected lines.
Save the changes (Use :w) and now you will see the changes in all the selected lines.
I would do like Nigu. Another solution is to use :normal:
<S-v> to enter VISUAL-LINE mode
3j or jjj or /D<CR> to select the lines
:norm I<Space><Space>, the correct range ('<,'>) being inserted automatically
:normal is probably a bit overkill for this specific case but sometimes you may want to perform a bunch of complex operations on a range of lines.
You can select the lines in visual mode, and type >. This assumes that you've set your tabs up to insert spaces, e.g.:
setl expandtab
setl shiftwidth=4
setl tabstop=4
(replace 4 with your preference in indentation)
If the lines form a paragraph, >ap in normal mode will shift the whole paragraph above and below the current position.
Let's assume you want to shift a block of code:
setup the count of spaces used by each shift command, :set shiftwidth=1, default is 8.
press Ctrl+v in appropriate place and move cursor up k or down j to select some area.
press > to shift the block and . to repeat the action until desired position (if cursor is missed, turn back with h or b).
Another thing you could try is a macro. If you do not know already, you start a macro with q and select the register to save the macro... so to save your macro in register a you would type qa in normal mode.
At the bottom there should be something that says recording. Now just do your movement as you would like.
So in this case you wanted 2 spaces in front of every line, so with your cursor already at the beginning of the first line, go into insert mode, and hit space twice. Now hit escape to go to normal mode, then down to the next line, then to the beginning of that line, and press q. This ends and saves the macro
(so that it is all in one place, this is the full list of key combinations you would do, where <esc> is when you press the escape key, and <space> is where you hit the space bar: qai<space><space><esc>j0q This saves the macro in register a )
Now to play the macro back you do # followed by the register you saved it in... so in this example #a. Now the second line will also have 2 spaces in front of them.
Macros can also run multiple times, so if I did 3#a the macro would run 3 times, and you would be done with this.
I like using macros for this like this because it is more intuitive to me, because I can do exactly what I want it to do, and just replay it multiple times.
I was looking for similar solution, and use this variation
VG:norm[N]I
N = numbers of spaces to insert.
V=Crtl-V
*** Notice *** put space immediate after I.

How to insert variable number of spaces necessary to align textual columns in Vim?

I’m a fan of Visual mode in Vim, as it allows to insert text before any given column.
For example, insertion of spaces after the quotation leaders below:
> one
> two
> three
can be done via <Ctrl-V>jjI <Esc>:
> one
> two
> three
as follows:
Start Visual mode with Ctrl-V.
Extend visual selection with jj.
Insert some spaces with I__.
Propagate the change to all the lines of the block selection with Esc.
Now I have a text file that needs some formatting. This is what it looks like:
start() -- xxx
initialize() -- xxx
go() -- xxx
Now I want to align part of this text to arrange it into columns like this:
start() -- xxx
initialize() -- xxx
go() -- xxx
The problem I have is that I cannot insert a different amount of indentation into each line and merely indenting a fixed amount of spaces/tabs is insufficient.
How can you do an indentation where all indented text will have to be aligned at the same column?
Update
I only figured out a rather verbose and unwieldy method:
Find the string position to indent from: \--.
Insert n (let's say 20) spaces before that: 20i <Esc>.
Delete a part of those spaces back to a certain column (let's say 15): d|15.
Save those steps as a macro and repeat the macro as often as necessary.
But this approach is very ugly, though!
I'm much better off without any vim plugins.
Here is my solution:
<Shift-V>jj:!column -ts --
Then insert -- into multiple lines just as you wrote in the question.
You can also append a number of comments at insertion time.
:set virtualedit=all
<Ctrl-V>jjA-- xxx<Esc>
You have to use a specific plugin, you can use either Tabular or Align plugin in this case.
They both allow you to align text on specific characters, like -- in your example. Their syntax is a bit different though. Pick the one that suit you the most.
Without plugin and if you have already entered your comments without emix's solution:
:,+2 s/--/ &
This will ensure all comments are to be shifted leftwise in order to align them properly.
Then blockwise select the column to which you want to align the text, and : 100<
An easy way to align text in columns is to use the Tabular or
Align plugin. If neither of these is ready at hand, one can use
the following somewhat tricky (and a little cumbersome looking) yet
perfectly working (for the case in question) commands.1,2
:let m=0|g/\ze -- /let m=max([m,searchpos(#/,'c')[1]])
:%s//\=repeat(' ',m-col('.'))
The purpose of the first command is to determine the width of the
column to the left of the separator (which I assume to be --
here). The width is calculated as a maximum of the lengths of the text
in the first column among all the lines. The :global command is used
to enumerate the lines containing the separator (the other lines do
not require aligning). The \ze atom located just after the beginning
of the pattern, sets the end of the match at the same position where
it starts (see :help \ze). Changing the borders of the match does
not affect the way :global command works, the pattern is written in
such a manner just to match the needs of the next substitution
command: Since these two commands could share the same pattern, it can
be omitted in the second one.
The command that is run on the matched lines,
:let m=max([m,searchpos(#/,'c')[1]])
calls the searchpos() function to search for the pattern used in the
parent :global command, and to get the column position of the match.
The pattern is referred to as #/ using the last search pattern
register (see :help "/). This takes advantage of the fact that the
:global command updates the / register as soon as it starts
executing. The c flag passed as the second argument in the
searchpos() call allows the match at the first character of a line
(:global positions the cursor at the very beginning of the line to
execute a command on), because it could be that there is no text to
the left of the separator. The searchpos() function returns a list,
the first element of which is the line number of the matched position,
and the second one is the column position. If the command is run on
a line, the line matches the pattern of the containing :global
command. As searchpos() is to look for the same pattern, there is
definitely a match on that line. Therefore, only the column starting
the match is in interest, so it gets extracted from the returning list
by the [1] subscript. This very position equals to the width of the
text in the first column of the line, plus one. Hence, the m variable
is set to the maximum of its current value and that column position.
The second command,
:%s//\=repeat(' ',m-col('.'))
pads the first occurrence of the separator on all of the lines that
contain it, with the number of spaces that is missing to make the text
before the separator to take m characters, minus one. This command
is a global substitution replacing an empty interval just before the
separator (see the comment about the :global command above) with the
result of evaluation of the expression (see :help sub-replace-\=)
repeat(' ',m-col('.'))
The repeat() function repeats its first argument (as string) the
number of times given in the second argument. Since on every
substitution the cursor is moved to the start of the pattern match,
m-col('.') equals exactly to the number of spaces needed to shift
the separator to the right to align columns (col('.') returns the
current column position of the cursor).
1 Below is a one-line version of this pair of commands.
:let m=0|exe'g/\ze -- /let m=max([m,searchpos(#/,"c")[1]])'|%s//\=repeat(' ',m-col('.'))
2 In previous revisions of the answer the commands used
to be as follows.
:let p=[0]|%s/^\ze\(.*\) -- /\=map(p,'max([v:val,len(submatch(1))+1])')[1:0]/
:exe'%s/\ze\%<'.p[0].'c -- /\=repeat(" ",'.p[0].'-col("."))'
Those who are interested in these particular commands can find their
detailed description in this answer’s edit history.
This is a modification on Benoit's answer that has two steps.
First step, block select text search and replace -- with lots of spaces.
'<,'>s/--/ --/
Now all the comments should have lots of spaces, but still be uneven.
Second step, block select the text again and use another regex to match all the characters you want to keep (say the first 20 characters or so) plus all the spaces following, and to replace it with a copy of those first 20 characters:
'<,'>s/\(.\{20}\)\s*/\1/
Not quite as easy as Benoit's, but I couldn't figure out how to make his second step work.

Resources