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.
Related
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.
In Vim, is there a key moving cursor to the beginning of the next line?
j moves the cursor to the corresponding position of the current position in the next line. I'm looking for a single key that could move the cursor to position 0 irrespective of the cursor position in the current line.
The + motion moves to the first non-blank character in the next ([count]) line. That fulfills your requirement if there's no indent. With indent, you can use +0 or j0.
You can move to the beginning of a line by pressing +
That just moves to the first non-blank character at the beginning of the line, which is fine if there is no indentation. If you want to go to the first character in the line regardless of white space, you can do j0
If you need any more help with movement keys, this is a good resource:
http://vim.wikia.com/wiki/Moving_around
I've added these shortcuts to duplicate a line based on the :t command, where :t. duplicates the current line onto the next line (keeps cursor on the second of the pair of lines), and :t-1 duplicates the current line onto the previous line (keeps cursor on the first of the pair of lines).
nmap <leader>D :t-1<cr>
nmap <leader>d :t.<cr>
In both cases the command moves the cursor position to the beginning of the line that has been created. How can I keep the cursor in the same position it was on (e.g. 20 characters in from the start of the line) on the new line?
Easiest solution is to use
:set nostartofline
Makes ex commands retain cursor column when possible, see h:nosol
Here is a solution:
nmap <leader>D mayyp`a
nmap <leader>d mayyP`a
It starts by placing a mark called a (ma) on the current cursor position, then copy the current line (yy) and paste it below (p). After that, it goes back to the original cursor position with `a. The second mapping uses P instead of p to paste the line above the current one.
This should do what you’re expecting.
When I cut and paste in VIM by pressing v, and go to the end of the line using $, and press d, the next line gets moved up to the same line I'm cutting.
How do I stop this?
It moves up because you have removed all the characters including line return/feed.
There are multiple solutions as usual with Vim. There is no "one true way" but you can try the following commands.
You can use D (capital) in normal mode which will erase everything until the end of line.
See :help D
Using another motion
What you could do instead of using $ to move to the end of the line, use g_. It will move to the last non blank character of the line and won't select line return.
See :help g_
So vg_d should work as you want.
Using Replace
Alternatively, what you could do instead of cutting, you could replace the erased character by a blank using the space character.
So v$rSPACE should work to erase but it will not save the replaced characters in register (for pasting later for example).
To cut everything from current cursor position until the end, use C.
:he C will help you:
Delete from the cursor position to the end of the
line and [count]-1 more lines [into register x], and
start insert. Synonym for c$ (not |linewise|).
Doing so will cause the current line (assuming you are on the start of the line when hitting C) to become empty and the content is (by default) yanked into register "
Edit:
As Xavier notes in his comment (and his answer), the same could be achieved with D. It also cuts everything from current cursor position until the end of the line but doesn't go in insert mode after doing it.
If you use these keystroke sequence then next line would not move up.
v $ h d
It is moving up because EOL character $ is also getting deleted without moving cursor 1 character back.
Just skip the visual mode and swap the other two commands, ie. press d $.
This is shorter than your starting one and doesn't break your tradition introducing other keystrokes you may not be familiar with.
Observe a line in a Vim instance:
Now I hit $:
Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!
Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.
Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.
If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.
Use a to append a character after the current.
Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.
Similarly shift-I to insert at the beginning of the line without first having to press ^.
The cursor can't be between two characters, it is always on a character.
If you press $ then x, you will correctly delete the last printable character of the current line.
What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.
In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".
mnemonic for a : a for "append".