Vim: Concatenate 2 columns of text horizontally - vim

Say I have a file
a
b
c
And in another I have
1
2
3
Can I, in Vim (or in shell in general), somehow copy the second one into the first one to get
a 1
b 2
c 3
?

Follow the below step by step approach in vim, to achieve the same.
Open the first file containing
a
b
c
Open the second file containing
1
2
3
In the second file, go to vertical select, by typing Ctrl + q (vertical select mode) for windows gVim, if in other OS, go for Ctrl + v (vertical select mode). once all the lines are selected, press y to yank the content.
Go to first file and go to line 1 after a and type p to paste the content. you will get the content as desired by you.

use this on shell:
paste file1 file2 | sed 's/\t/ /' >> outputfile
If you remove the sed part the output file will have tab separated values.

Use this :
vi file2 file1
ctrl+v
select all the column with arrow down, then hit y in command mode, then :n in command mode.
In the second file, line 1, add two space in edit mode, then hit p in command mode

Related

Allign the words to the specified column in vim using commands

How I can move or shift the words in the entire file to the specified column?
For example like below:
Before :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
After :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
How it can be done using command mode?
Here the thing is we need to shift the 2nd word to the specified column
Here the specified column is 8
except for vim-plugins mentioned by others, if you were working on a linux box with column command available, you could just :
%!column -t
% could be vim ranges, e.g. visual selections etc..
Approach with built-in commands
First :substitute the whitespace with a Tab character, and then :retab to a tab stop to column 8, expanding to spaces (for your given example):
:.,.+4substitute/\s\+/\t/ | set tabstop=7 expandtab | '[,']retab
(I'm omitting the resetting of the modified options, should that matter to you.)
Approach with plugin
My AlignFromCursor plugin has commands that align text to the right of the cursor to a certain column. Combine that with a :global command that invokes this for all lines in the range, and a W motion to go to the second word in each, and you'll get:
.,.+4global/^/exe 'normal! W' | LeftAlignFromCursor 8
I use the Tabular plugin. After installing it, you run the following command:
:%Tab/\s
where \s means whitespace character
I have made two functions for this problem.
I have posted it here : https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
We need to call this function in vim editor and give the Number of Occurrence of the Character or Space that you wants to move and the character inside the '' and the column number.
The number of occurrence can be from the starting of each line (MCCB function) or can be at the end of each line (MCCE function).
for the above example mentioned in the question we can use the MCCB function and the character we can use space, so the usage will be like this in the vim editor.
:1,5call MCCB(1,' ',8)
So this will move the first space (' ') to the 8th column from line number 1 to 5.

Remove last column in VIM

In VIM how would I remove the last column of a txt file.
1 2 3 4
x x x x
x x x x
I want to remove column 4
Result:
1 2 3
x x x
x x x
Thank you
try this on the lines (either with %, as a selection, as movement commands...):
s/ [^ ]*$//
Explanation: The regular expression matches everything from the end of line to the last delimiter, and discards that.
i think vertical visual selections are pretty handy too.
gg$<C-v>h3jx
You can easily delete any column(s) you want this way.
The other option is to use vertical selecting [ Shift+V ] the last column then just delete it.
If you want to compute the whole buffer, use the regular expression mentionned by thriqon
:%s/ [^ ]*$//
It's also possible to obtain the same result by running an external command in vim
:%! cut -d' ' -f1-3
You can use a macro that removes the last column, then apply that to the entire file.
So go to the first non-header row, and run
qa$bld$^q
Which will start recording in register a a motion that will go to the end of the line, then back a word, right one character until the space, then deletes until the end of the line.
Now undo that change with u.
Now apply that macro to the entire file with
:%normal #a
This is really handy for removing a specific row in a csv file.
For instance, to remove the last row of a csv
qa$F,d$^q
:%normal #a
Or to remove the second to last row
qa$2F,dt,^q
:%normal #a
See :help :normal, :help #, :help q, and :help :% for reference.

vi/vim editor, copy a block (not usual action)

In vi/vim editor, I need to copy a block. There are many ways, but one way is very quick.
label the first line by some way,
then label the end line by some way,
then put some command to copy the labeled lines.
then copy, may using 'p', but not sure.
Anybody know the commands (not yy or 10yy)?
just use V to select lines or v to select chars or Ctrlv to select a block.
When the selection spans the area you'd like to copy just hit y and use p to paste it anywhere you like...
Their Documentation says:
Cut and paste:
Position the cursor where you want to begin cutting.
Press v to select characters (or uppercase V to select whole lines).
Move the cursor to the end of what you want to cut.
Press d to cut (or y to copy).
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.
Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:
d = delete = cut
y = yank = copy
Another option which may be easier to remember would be to place marks on the two lines with ma and mb, then run :'a,'byank.
Many different ways to accomplish this task, just offering another.
I found the below command much more convenient. If you want to copy lines from 6 to 12 and paste from the current cursor position.
:6,12 co .
If you want to copy lines from 6 to 12 and paste from 100th line.
:6,12t100
Source: https://www.reddit.com/r/vim/comments/8i6vbd/efficient_ways_of_copying_few_lines/
It sounds like you want to place marks in the file.
mx places a mark named x under the cursor
y'x yanks everything between the cursor's current position and the line containing mark x.
You can use 'x to simply move the cursor to the line with your mark.
You can use `x (a back-tick) to move to the exact location of the mark.
One thing I do all the time is yank everything between the cursor and mark x into the clipboard.
You can do that like this:
"+y'x
NOTE: In some environments the clipboard buffer is represented by a * in stead of a +.
Similar questions with some good answers:
How to copy/paste text from vi to different applications
How to paste from buffer in ex mode of vim?
Keyboard shortcuts to that are:
For copy: Place cursor on starting of block and press md and then goto end of block and press y'd. This will select the block to paste it press p
For cut: Place cursor on starting of block and press ma and then goto end of block and press d'a. This will select the block to paste it press p
You can do it as you do in vi, for example to yank lines from 3020 to the end, execute this command (write the block to a file):
:3020,$ w /tmp/yank
And to write this block in another line/file, go to the desired position and execute next command (insert file written before):
:r /tmp/yank
(Reminder: don't forget to remove file: /tmp/yank)

Cut and paste multiple lines in vim

I'm running vim 7.3 on a Mac 10.7.2 and I'm having some trouble cutting and pasting several lines.
On my old Linux setup (which was stolen so I don't know versions), I could type "dd" multiple times and then "p" would yank all of them back. For example: type: "dd dd" and two lines would be deleted. Now type "p" and both lines are pasted back into the buffer.
I know I can accomplish what I want by typing "2dd", and then "p" - but I would like to be able to "dd"-out lines without counting the number of lines ahead of time.
Any ideas?
Have you considered using visual mode?
You could just go:
Press V
Select everything you want to cut without counting
Press d
Go to where you want to paste
Press p
This should yield approximately half as many keystrokes as the dd method since you press one key per line rather than two. Bonus points if you use 5j (or similar) to select multiple lines at a time.
You could type:
d<n>d
where <n> is the number of lines that you want to cut, and then you could paste them with:
p
For example, to cut and paste 3 lines:
d3d
p
To cut and paste by line numbers (do :set number to see the line numbers), for lines x to y do:
:x,yd
or if your cursor is already on line x, do
:,yd
Then go to where you want to paste and press p
Not sure if this is close enough to what you're trying, but one thing you could do is use a specific register, and capitalize your register name. That tells vim to append to the register rather than replace it, so if you have the lines:
one
two
three
you can enter
"qdd
"Qdd
"Qdd
and then subsequently if you enter
"qp
it will paste back the original lines
To copy and paste 4 lines:
y4y (with the cursor on the starting line you wanna copy)
p (with cursor on the line you wanna paste after)
I agree with #Ben S. that this is the preferred way to accomplish this but if you are just looking to replicate your old behavior you can remap dd to append to a specified register, and then map p to paste from that register and clear it.
This will have the disadvantage of causing p to only work with things deleted using dd (using d} to delete to the end of the paragraph would not put the text in the correct register to be pasted later).
Add the following to your vimrc
noremap dd "Ddd "Appends the contents of the current line into register d
noremap p "dp:let #d=""<CR> "Pastes from register d and then clears it out
if you don't want pasting to clear out the contents of the register
noremap p "dp "Paste from register d
but this will cause that register to grow without ever clearing it out

How to cut an entire line in vim and paste it?

I know how to use the v command in vim, but I need something which will delete an entire line and it should allow me to paste the same line somewhere else.
dd in command mode (after pressing escape) will cut the line, p in command mode will paste.
Update:
For a bonus, d and then a movement will cut the equivalent of that movement, so dw will cut a word, d<down-arrow> will cut this line and the line below, d50w will cut 50 words.
yy is copy line, and works like dd.
D cuts from cursor to end of line.
If you've used v (visual mode), you should try V (visual line mode) and <ctrl>v (visual block mode).
Pressing Shift+v would select that entire line and pressing d would delete it.
You can also use dd, which is does not require you to enter visual mode.
Delete current line and copy to clipboard:
d + d
Paste After The Cursor
p
Paste Before The Cursor
Shift + p
Select Whole Line (I use this ALL the time)
Shift + v
Then j or k to move down and up respectively
Essentially d + d is the equivalent of Shift + v then d
There are several ways to cut a line, all controlled by the d key in normal mode. If you are using visual mode (the v key) you can just hit the d key once you have highlighted the region you want to cut. Move to the location you would like to paste and hit the p key to paste.
It's also worth mentioning that you can copy/cut/paste from registers. Suppose you aren't sure when or where you want to paste the text. You could save the text to up to 24 registers identified by an alphabetical letter. Just prepend your command with ' (single quote) and the register letter (a thru z). For instance you could use the visual mode (v key) to select some text and then type 'ad to cut the text and store it in register 'a'. Once you navigate to the location where you want to paste the text you would type 'ap to paste the contents of register a.
Let's say that you wanted to cut the line bbb and paste it under the line ---
Before:
aaa
bbb
---
After:
aaa
---
bbb
Put your cursor on the line bbb
Press d+d
Put your cursor on the line ---
Press p
The quickest way I found is through editing mode:
Press yy to copy the line.
Then dd to delete the line.
Then p to paste the line.
press 'V' in normal mode to select the entire line
then press 'y' to copy it
go to the place you want to paste it and press 'p' to paste after cursor or 'P' to paste before it.
Yep, use dd in command line. Also I recommend to print useful image with ViM hotkeys available at http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
Just three steps.
Go to the start of the text, and press v
Go to the end of the text, and press d
Go to the place that you want to paste, and press p
Go to the line, and first press esc, and then Shift + v.
(This would have highlighted the line)
press d
(The line is now deleted)
Go to the location, where you wanted to paste the line, and hit p.
In a nutshell,
Esc -> Shift + v -> d -> p

Resources