In Vim, can I move a line to current line without leaving the current line? - vim

The title says it all: is there a way in Vi(m) to move a line, by number, to my current line?
I could move to my source line first, then use the :m command to move that line. But that involves leaving my target line. Or, I could :Nd, but that also moves me off of my current line and on to the deleted line number N.
It's frustrating looking at a line and thinking "move here" and not knowing how :D.

:3m . moves line 3 to your current line.

Related

Annoying vim behavior in visual mode

Let's say I have the following three lines and cursor is where ▐ is:
1. This is a slightly longer line.▐I want to delete this and the next line.
2. This is a shorter line
3. This is the third line
I want to delete the rest of the line from the cursor and line 2, so I do vjd, but that leaves my text like:
# This is a slightly longer line.# This is the third line
This is because when selecting text in visual mode, vim selects an additional virtual character at the end of the line. I've played around with virtualedit but that didn't seem to help.
Any clues on how I can get the original behavior that I wanted?
Try three keystrokes - sh-d, sh-j, sh-d
shift-d (delete to end-of-line)
shift-j (merge next line with this)
shift-d (delete the merged, i.e., second line
That will do the trick for you.
That's because if you don't have virtualedit set, vjd will delete everything including the new line character. So you just have to do vjhd instead, to keep the new line intact.
Otherwise with virtualedit=all, I don't face this problem.

How to move to the end of the continuous line by pressing keys in command line in vim editor

My file contains some content.
This file is just for testing and it may contain no about about any related data.
But used for copy of move the content to one file to another.
The given line is continuation of the same line. How to move to the cursor to end of the first line by pressing single key.
My _cursor is waiting here
_This file is just for testing and it may contain no about about any related data.
But used for copy of move the content to one file to another.
By pressing singe command or key I need to move to end of first line of my cursor_.
This file is just for testing and it may contain no about about any related data._
But used for copy of move the content to one file to another.
If there is any way, let me know.
You can move to the end of lines in vim using $ sign.
You may use a number in front $ to move n ends down. For instance, 2$ moves to the end of the second line from your cursor.
When "wrap" is turned "on", then g$ moves to the end of SCREEN line. So,
it differs in functionality from a $. For example: If you wanted to move 2 screen lines down use 2g$, if you wanted to move 2 lines down (when wrap is on) use 2$.
For more details you can do :help $ and :help g$.

Easier line referencing

I was wondering if anyone knows of a plugin to enable easier line determination.
I have issues quickly scanning to see what line I was to reference in commands such as t and m
See this screenshot:
If I wanted to quickly reference line 5 (I do have line numbers switched on, I just accidentally cut it out in this screenshot) I find I have to look rather hard to find the correct line number.
so: Is there a plugin which makes referencing lines less eye-straining?
I guess your problem is with those deeper indented lines. sometime it is not easy to "connect" the line number and the line text. If this is the case, you may try followings:
set listchars=tab:>-
(see :h 'listchars' for detail) this line will show the <tab> with certain chars. for example, following screenshot is a formatted maven pom.xml, with relative deeper indent lines. I think it would be ok to read the line numbers of them. E.g. the line 1180-1184.
I hope it helps.
with plugin
If the above doesn't help, e.g. you have spaces not <tab>, you could try a plugin: indentLine, with this you could set a variable g:indentLine_char with the char you like. e.g. > to show indent level clearly.
The link of the plugin: https://github.com/Yggdroot/indentLine
:move and :copy are not limited to line numbers (absolute or relative) only, either as source or as target.
You can use search patterns too:
:m?foo
would move the current line just under the first line matching foo going upward,
:t/bar
would copy the current line just under the first line matching bar going downward,
:?foo?t/bar
would copy the first line matching foo above the current line to just below the first line matching bar going downward, and so on.
You can also use marks:
:'at'b
would copy the line marked a to below the line marked b,
:m''
would move the current line to just below the line you were before the last jump, and so on.

How to yank the current line and the line above it in Vim?

What I need is to yank the current line and the line just above it.
For instance, in the following example:
3 My test line
4 Line above current line
5 My current line |(cursor)
6 Line below current line
How do I yank lines 5 and 4 when my cursor is located on line 5?
yk should do it, as in Yank in the direction of up one line, since y will accept the next keystroke as a motion, and k alone represents motion up one line.
If you need your cursor to return to its original position, just add a j as ykj. You will probably see the cursor move inelegantly on screen, but it gets the job done.
For this simple case, yk will do the trick. This is yank followed by a motion of up one line.
Generally, use yNk, e.g. y3k to yank the current line and the preceding 3 lines.
If you need to return to the cursor position after the yank, set a mark and return to the mark after the yk:
mmyk`m
If you need only remain on the same line where you began the yank, not the same cursor position, ykj is shorter.
In addition to the Normal mode commands already mentioned in other answers,
one can use the :yank Ex command on a corresponding range of lines. For
example, to copy the current line along with the line above it (without moving
the cursor) run
:-,y

move cursor to next line after input from filter command in vim

In vim I filter, say the current single line, using !! through a Unix command. To achieve this I defined the following shortcut in .vimrc
:map <Enter> !!mycommand<CR>:,+1<CR>
Pressing <Enter> this takes me to the line below the current if mycommand replaces my single input line be exactly one output line. If the output has more lines (number of lines unknown before command execution) it will still take me to the line below the current.
Now, I would like to know how I can always get to the first line below the inserted output of mycommand.
The modified shortcut would then allow me to 'execute' the text file line by line using just <Enter> displaying the output each time.
If there is no way to do this without any previous knowledge of the output of mycommand, maybe there is one knowing say the first character of each output line.
Thanks a lot!
You can use the special marks '[ and '], which mark the start and end of the last changed (or yanked) text. Change your map to:
:map <Enter> !!mycommand<CR>']+
Note that I'm using + in place of your ex command. This will jump to the first non-blank character in next line. If that's not what you want, you may try simply j or, use a shorter version of your original map:
:map <Enter> !!mycommand<CR>']:+1<CR>
You don't really need the comma, to make this a range. This command is just a simplified :#, where # is a line number to jump. Here you can use . meaning "current line", and then :.+1 moves to the next line. But you can omit the dot, and that's why :+1 does the same.

Resources