copy partial lines in VI - vim

There are a lot of different ways in which one can yank complete single/multiple lines. Is there a way in which we can copy partial lines in vi, like just 10 characters of the line.

I would guess the most common partial yanks are:
yaw: yank the word the cursor is currently in
2yaw: yank the word the cursor in currently in and the next (2 words total)
ya(: yank the matched parentheses containing the cursor
yf.: yank from the cursor to the next .
y$: yank from the cursor to the end of the line
Any movement keys can be used.

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
Resource:
vim.wikia.com: Copy, cut and paste

You can do yMovement, so for 10 characters: y10l yanks 10 characters from (and including) the current cursor position

I prefer just pressing the v key, then using the cursor keys to move your selection. Then press the y key when you satisfied to yank the selection.

you can also do
yt<char> - yank 'till char - i use that a lot
or y/<pattern>/ - yank until pattern

Sure, with the cursor at the beginning of the line, type:
y10l
This yanks 10 characters to the right. If you need to do this repeatedly for some reason, just add this temporary kep mapping:
:noremap ,m ^y10l
Which will yank the first 10 chars of any line every time you press ,m

If you have multiple lines to copy, try visual block mode,
" beginning of line
C-v
" up-down move 10j or 5k
10l
" copy & paste
y
p
more detail, see wiki

This is one place mouse may actually beat keyboard, especially if the current mouse cursor is far from your copy target, or if you want to select multiple lines with partial start line or end line.
Use :set mouse=a to enable mouse support. Then select whatever irregular text blocks with mouse, and then press y

Related

vim - multiple commands on same line mapping failure

I am pretty new to vim/gvim. This is my first attempt at modifying the ~/.vimrc file.
I am trying to map F11 to multiple commands.
What I am trying is
inoremap jj <ESC>
map <F11> h e a , jj && l && x
While manually entering h e a , jj l x is working for me, using F11 key is not.
What I am trying to achieve is-
1. get to the end of the current word
2. append ','
3. move right one place and delete that space between ',' and the start of next word.
I do not understand what's wrong with my mapping.
Please help me here.
First, let's build a proper representation of your macro
get to the end of the current word
e
append ','
ea,<Esc>
move right one place…
ea,<Esc>l
… and delete that space between ',' and the start of next word.
ea,<Esc>lx
Second, let's use it in the right-hand part of a proper non-recursive mapping:
nnoremap <F11> ea,<Esc>lx
Of note:
<Space> is a legitimate normal mode command so any <Space> in your macro is interpreted as "move the cursor one cell to the right", see :help l.
& is also a legitimate normal mode command, see :help &.
In a macro, every character is meaningful so h e a , jj && l && x is interpreted as:
move the cursor one cell to the left,
move the cursor one cell to the right,
move the cursor to the end of the current word,
move the cursor one cell to the right,
append a space, a comma, a space,
move the cursor one cell to the right,
repeat last substitution,
repeat last substitution,
move the cursor one cell to the right,
move the cursor one cell to the right,
move the cursor one cell to the right,
repeat last substitution,
repeat last substitution,
move the cursor one cell to the right,
cut the character under the cursor.
Your jj insert mode mapping provides zero value over plain <Esc> so there is no reason to use it in any mapping.
Your mapping thus becomes a non-recursive mapping so nmap becomes nnoremap.

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)

How can I highlight multiple lines in gVim without using the mouse?

Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)
using text-objects
determine what text-object would map to what you want to yank:
inner/outer word, iw/aw
inner/outer pair, i'"([{</a'"([{<
inner/outer html tag, it/at
sentence, s
paragraph, p
"block", ]
…
yank that text-object with, say, yip
using other motions
yank to end of function: y]}
yank to end of file: yG
all of the above solutions with visual mode
V'ay
V/foo<cr>y
V?bar<cr>y
Vipy, etc.
V]}y
VGy
:h motion.txt will hopefully blow your mind, like it did to mine.
You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.
Then, you can paste them using p.
You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.
Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.
Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.

Delete n lines in the up direction in vim

Is there a command in vim which will delete n lines in the up direction.
I know I can use 4dd which will delete 4 lines downwards.
In VIM, 3dk would delete 4 lines in the upward direction. Further documentation can be found at http://www.vim.org/docs.php
V3kd would do it.
Thats "V" to enter visual line select mode, "3k" to move up 3 lines, and then "d" to delete the 4 lines you have selected.
You can do it with a backwards range.
:-4,.d
Deletes from minus 4 lines to current. But this is an ex mode command.
Position the cursor where you want to begin cutting.
Press v (or upper case V if you want to cut whole lines).
Move the cursor to the end of what you want to cut.
Press d.
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.
http://vim.wikia.com/wiki/Copy,_cut_and_paste
stand at the end of the last line
hold Backspace and wait until the last character of the first line is deleted

How do I select a chunk of text and paste it to the current cursor position w/o using mouse in vim?

I want to give up using mouse for selecting and pasting chunks of text within a buffer. Whats the most efficient way to do this with just kb? I mean navigate to arbitrary line, copy the substring, return to the previous position and paste.
Very simple method:
Select the lines with Shift-V
"Yank" (=copy) the text with y
Paste the text with p at the position you want to.
There are of course many other ways to copy and paste, yy copies the current line for example.
Do the some VIM tutorials, it is better than learning everything bit by bit.
If you want to go quickly to a line use the search by typing
/SUBSTRING and then Enter after you have found the correct substring.
Make sure to use hlsearch and incsearch
:set incsearch and :set hlsearch
When you are at the correct line, yank the whole line with yy or the whole word with yaw.
Then go back to where you started the search by typing two backticks ``
Then you can paste your yanked line/string with p
Mark your current position by typing ma (you can use any other letter instead of a, this is just a "named position register".
navigate to the line and substring for example by using a / search
yank text with y<movement> or mark it with shift/ctrl-v and then y
move back to your previously marked position with ```a`` (backtick)
paste your buffer with p or P
My normal method would be:
Use visual mode to select the text with v, V, or Ctrl+v
Yank using y
Go to the line you want to be on using 123G or :123
Navigate where I want to be within that line with t or f
Put the text with p or P
If you need to jump back and forth between the spots, I'd cycle through jumps using g, and g;.
Use "p" to paste after the current line, and "P" to paste above the current line.
Not sure what you mean by 'the substring'. If you want to copy line 50 to the current position, use:
:50t.
If you want to move line 50 to the current cursor position, use:
:50m.

Resources