Vim duplicate with same cursor position - vim

I want to duplicate a line with Vim. Every time when I use yyp or any other command, I have to jump the cursor to the position where it was. Is there any trick to duplicate a line and jump the cursor to its position, like maybe using a macro to save current the cursor position, duplicate, and then jump to the required position?

The only solution I'd see to do what you want, given you only use it for yyp (i.e. copy currentline and paste it below), is to record in a macro:
mmyyp`mj
what that does is:
to record a mark m,
copy current line
paste it
jump to col+row location of the mark m
move the cursor one line down
ideally what you'd want is a jump to a column, not changing the row. I'm not sure if it's possible, I never needed that before.

Maybe you are looking for ctrl-o and ctrl-i to jump back and forth.

Related

Delete the current line in Vim regardless of cursor position

How do I delete the current line in Vim regardless of the cursor's position?
I am aware of D and C which delete from the cursor position to the end of line, but how do I delete the entire current line irrespective of the cursor position within the line?
Is there a way to do this without explicitly moving to the start of line by using 0?
I believe dd does what you want.

Vim: cut line from cursor position and paste it to another cursor position [duplicate]

This question already has answers here:
How to yank the text on a line and paste it inline in Vim?
(4 answers)
Closed 8 years ago.
I couldn't find this answer anywhere. I cut some line in Vim with either dd or V+d and I want to paste it inside brackets let's say. How do I do that?
p is paste after, and P is paste before, but I want it to paste at my cursor position.
Edit: I want to cut from certain cursor position (not whole line) to end of line and paste to cursor position in another line.
When you use either of those methods for cutting, you get the newline at the end of the line. With the newline, vim has to either put the line before or after.
To get the behavior you want, you should delete using 0D instead to delete, and then use a normal p to put it into the cursor.
If you want to cut from current cursor position to the end of line and then paste it inside another line at cursor position, then navigate do the first line, do D to cut to end of line, then move your cursor to the place where you want to paste it and use p.
try this, do Y or dd as usually, and create this mapping:
nnoremap <leader>p :let #"=substitute(#","\n","","g")<cr>p
nnoremap <leader>P :let #"=substitute(#","\n","","g")<cr>P
when you press p/P it pastes in default way(with newline). when you press <leader>p /<leader>P it pastes "in-line".
This is not so clean, because it changed the #", next time you press p the newline is not there any longer. I was a bit lazy, put the sub() there, you can make a little function, remove the newline then paste, without touching (or restore after paste) the #". and in mapping call that function.
I don't know if you did a 10dd, what output do you want to have when you do an "in-line" paste. but you can do quite a lot thing in your function, to reach your needs.
EDIT,
I don't know how did you create the mapping, and "didn't work". here I put an animation:
You can do something like this:
v$d
(move to where you want to put what you just cut)
p or P

vim: go to the last character of line plus one

I have a problem when I use the paste command (remapped to C-V) at the end of the line.
Of course it inserts a char before the last one, and not after...
However if I want to insert one after that I have to insert a space, paste the text and delete the space.
Is there a way to make Vim go to the end of the line, plus one char so I can paste quickly?
There are two paste commands in vi; P to paste before the cursor position and p to paste after the cursor position. Make sure you are remapping p.
The default behaviour of put is to do as you require, rather than what Ctrl+V does. Maybe you could remap C-V to "*p.

How to jump to the exact last change point in Vim

'. can jump to the line of last change. But I need to jump to the exactly point.
Use `.. That is, backtick followed by a dot.
As a change has a beginning and an end, `[ and `] are also very helpful:
'[ `[ To the first character of the previously changed
or yanked text.
'] `] To the last character of the previously changed or
yanked text.
After executing an operator the Cursor is put at the beginning of the text
that was operated upon. After a put command ("p" or "P") the cursor is
sometimes placed at the first inserted line and sometimes on the last inserted
character. The four commands above put the cursor at either end. Example:
After yanking 10 lines you want to go to the last one of them: "10Y']". After
inserting several lines with the "p" command you want to jump to the lowest
inserted line: "p']". This also works for text that has been inserted.
answers are kinda old, but for current reference:
:changes show a list of changes
g; jump to last edit
g, jump forward in last edit's
found at vimtricks
I suggest a mapping to jump to the last changing point, as we have:
gv ................... repeats last selection
gi ................... enters insert at the last inserting point
" gl to jump to the last change "exactly"
nnoremap gl `.
Reference: https://twitter.com/dotvimrc/status/191163723065462786

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