For example, there are some text like this:
001 aaaaa
002 bbbbbbbb
003 ccc
I want to select
aaaaa
bbbbbbbb
ccc
and yank it. What should I do?
The following sequence should do the trick
ggw<C-v>G$
where
gg -- Goes to top of file
w -- skips one word
<C-v> -- starts visual block select
G -- selects until end of file
$ -- selects to end of each line
Switch to Visual Mode in Vim by pressing v in prompt
place the cursor in the column to select
Use ctrl+v to select a block to copy
Then the column alone will be selected and copy needed text alone and can yank it:)
Please refer the link here
Related
I have this problem where I try to yank/cut and paste a block of text with Vim and it has inconsistent behavior.
What happens is this:
In this text bellow, I what to cut the middle column and paste after the third. So I block-select (ctrl-v) the text, cut it (x), take the cursor to the end of the third column (now 2nd) and paste (p)
A 1 a
B 2 b
C 3 c
D 4 d
Sometimes Vim does it right and the result is this:
A a 1
B b 2
C c 3
D d 4
and some times, the result is this:
A a
1
2
3
4
B b
C c
D d
I need to control that behavior instead of being a hostage of it... How do I control this behavior?
Well, one answer is the UnconditionalPaste plugin (https://vimawesome.com/plugin/unconditionalpaste)
with a 'gbp' you paste a block inline (I remapped to [LEADER]pb)
it works...
I recommend you reading the help topics for visual selection,
:h visual.txt. The section on visual-operators and its notes might
be particularly useful. Selection behavior is pretty consistent and easy
to understand though. If you cut or copy something, expect it to be
placed in the same mode: characters, lines, or blocks.
I what to cut the middl ecolumn and paste after the third. So I
block-select (ctrl-v) the text, yank it (y), take the cursor to the
end of the third column (now 2nd) and paste (p)
That's not how you should do it. To "cut" the middle column, as you
describe, you came to use x and not y. Yanking will just copy it and
leave it there.
Regarding your last example where content was pasted in a linewise
fashion, that should not happen when using p since it conserves the
blockwise information. The only possible explanation that comes to my
mind is you deleted a column and used :put to paste it. That will
convert it to a linewise paste. Alternatively:
You yanked something likewise (say from V) and you are trying to
paste it blockwise
Your example does not translate well your problem
There is some sort of plugin or configuration affecting this
Random unknown keys were slammed in the process
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
Every now and then I run into this type of editing problem in Vim.
I have text formatting in a table that I want to turn into a list.
Before:
AAA BBB
AAA BBB
AAA BBB
CCC DDD
CCC DDD
CCC DDD
After:
AAA
AAA
AAA
BBB
BBB
BBB
CCC
CCC
CCC
DDD
DDD
DDD
Of course not as trivial as this example. The blocks can have hairier contents, larger and inconsistent numbers of lines, etc.
The way I do it now seems to be a bit of a hack:
Select a block from the right column in block mode:
CTRL-q in the Windows version to select the top-left corner.
d to cut the block.
Note how many lines were in the block, then manually add that many blank lines.
Go to the top-left of the new blank area I created. SHIFT+p to paste the block into this area.
Step is the rough part.
Doing a normal non-block cut or copy will always paste into "new" lines. A kind of "insert" or "append" operation. Doing a block cut or copy will normally paste in a kind of "overwrite" mode.
Is there a better way to copy or cut in "block mode" but paste in "insert / append mode"?
The ex command :put will always paste a register line wise.
Cut the block of text with visual block mode like you have before then execute :put instead of p.
If you want to "cast" pastes in more ways then use #Ingo Karkat's plugin.
For more information see:
:h pu
you can block wise select the BBB part as how you did, and after cut by d for example, you run this command:
call setreg('"',#",'V')
then you can paste to target line, it will turn your block-wise "yank" into line-wise.
You can create a mapping if you do need to do this often.
My UnconditionalPaste plugin has a glp (go linewise paste) command that will paste the register contents as new lines, regardless of how they were yanked (e.g. in blockwise mode).
It also has several other helpful commands that affect the way the register is pasted.
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)
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