How to get VIM to map dd to c-bs - vim

I've tried about 100 combinations of :inoremap and :imap, etc, but I am unable to find one that will allow me to remap to delete the current line and leave the cursor on the line that moves up, i.e. visually, the cursor does not move.
I'd prefer to have this work in input, replace and normal modes. Best if it covered all/most of the modes.
Can someone tell me how to do this?

you need to set ve=all to make sure the cursor staying on the same line, same col after deleting. Because the line below the deleted one could be shorter than the line you want to delete, E.g
foooooooo[I]oooo
bar
[I] is cursor, here if you press dd, the cursor won't go to the same column, because the bar line is shorter than fooo.. line. If you set ve=all, your cursor could be placed to any area in the buffer.
then you can just save the col before dd and restore it after dd. like:
:let c=col('.') |exec 'norm! dd'.c.'|'
without set ve=all, if your cursor column didn't exceed the length of the line below, the above command works as well. but if it exceeded, the cursor would be at the end of "below" line.
I hope I understood you right.

See the accepted answer in this question for an explanation of why I can't remap Ctrl-BackSpace.

Related

Filtering Lines To Display

I've used :v/pattern/d to filter the lines vim displays.
Now with the cursor on one of those lines I would like to undo the filter (show all lines in the file) but leave the cursor on the line where I left it. Does that make sense?
How do I do that?
Thanks
From vim doc:
Note that when text has been inserted or deleted the cursor position might be
a bit different from the position of the change. Especially when lines have
been deleted.
Your problem is exactly in this case. After your v/pat/d, those lines has been removed. the changelist/jumplist won't remember the location on a removed line.
After you undo it, even if you press ctrl-o or double`, you won't go back to the deleted line, instead, you go the closest undelete line of the old cursor position.
If you want to go back to the deleted line after a undo precisely, you may set a mark before your v/../d after your undo, you can go back to the position by `a

Vim - move rest of line to new line above

How does one get from
int lel = 123; // this is a comment
^
to
// this is a comment
int lel = 123;
preferably when starting in insert mode, and with the right indentation?
My current way of doing it is C-c l d$ O C-c p, but as my auto-indent isn't perfect, the inserted line in not indented at all.
I think some editors use space+enter or something for this (at least I think I've seen it).
Is this possible in vim by default?
Still not a very pretty answer, but assuming the cursor is where the "^" is above, another option would be:
d0=:puEnter
d0 deletes til the beginning of the line.
= reindents over the next motion.
:pu short for :put
Enter to run the command.
I would probably use
DO<c-r>"
D deletes to the end of line. O opens the line above in insert mode (with the correct indentation). <c-r>" pastes the part that was deleted with D.
(This ends in insert mode)
New answer:
d^o<c-u><esc>p
Figured it out. This was one of my early attempts, but in the form <esc>d^o<esc>p, which has the problem that the inserted line gets a comment leader. <c-u> fixes that.
d^o<c-u><c-o>p
is of course useful if one wants to stay in Insert mode.
Earlier answer:
This solution works, but the answer by Randy Morris is better.
Suggested key sequence:
<esc>mpa<cr><esc>dd`pP`pa
(Where p can be replaced with any other mark.)
This means <esc> enter Normal mode, mp mark current position as p, a enter Insert mode, <cr> break the line and put the comment on the next line at the correct indentation, <esc> go to Normal mode, dd delete line, `p go to the marked position, P put the deleted line before the current line, `p go to the marked position, a go to Insert mode. To avoid wearing your fingers out, map it:
:inoremap <F2> <esc>mpa<cr><esc>dd`pP`pa
The indentation will not be correct if the comment is at the end of a line that increases or decreases indentation.
Simpler sequence that will not work on the last line in the buffer
To perform this operation on a line that isn't the last line in the buffer, the following will do. With the cursor on the first slash, in Insert mode: <cr><esc>ddkPjA.
The <cr> breaks the line and puts the comment at the correct indentation, <esc> go to Normal mode, dd delete line, k go to previous line, P put the deleted line before the current line, jA to end up in Insert mode where you were when you started.
To map it:
:inoremap <F2> <cr><esc>ddkPjA

Is there something in VIM that behaves like ^E + j?

Is there a key-combination that behaves as though I'd press ctrl-E followed by a j, that is the text scrolls up a line but the cursor keeps where it is, relativ to the screen.
I am aware that I could achieve what I want with a :map but before I do I thought I'd rather want to know if there is already some "built-in" functionality
Yes, use CTRL-D with a count of 1 (not that that saves you anything, really).
The CTRL-D command does the same as CTRL-E, but also moves the cursor down the same number of lines
There is the z command
z. Redraw, line [count] at center of window (default
cursor line). Put cursor at first non-blank in the
line.
zz Like "z.", but leave the cursor in the same column.
Careful: If caps-lock is on, this commands becomes
"ZZ": write buffer and exit! {not in Vi}
These mappings makes it possible to scroll up and down one line with focus on center line (hard to describe so that it sound correct, try it instead)
"scroll with line in center
map <C-Up> <ESC>0kzz
map <C-Down> <ESC>0jzz

Cursor positioning when entering insert mode

When I switch to command mode in Vim, the cursor seems to move one character back when it's at the end of or on a word, and when I go to the end-of-line with $, it does not go to the actual end of line but one character before the end of the last word, and l ("el") does not move it forward and I have to use the arrow key to get there.
I haven't been able to find documentation of this behavior, but this seems strange to me. What's the reasoning behind this (for my own curiosity), and how can I get around it (or deal with it)?
it is a little more clear if you use gvim, where the cursor changes.
insert mode in gvim has the cursor as an I-beam, since the next letter you type will be inserted after the |. normal mode has the block cursor, because the next thing you type may just effect the letter that is currently highlighted (like if you use x, s, etc). So insert mode is actually adding text, but normal mode is modifying text in some way.
So in normal mode, jumping to the end of the line really just means the last character, since that is the last thing that is possible to be modified. in insert mode, the cursor goes passed the last character, since it is possible to add things afterwards.
One thing to keep in mind is that you can control which side of the block you end up on going from normal mode to insert mode
([] means that the block cursor is over that h)
Let's say you have t[h]is text
if you pressed i at this point, the cursor would look like this (in gvim)
(| being the insert mode cursor)
Let's say you have t|his text
if you pressed a instead of i, it would look like this
Let's say you have th|is text
Another thing to keep in mind (as pavanlimo mentioned), from normal mode you can go to insert mode with your cursor just before the first character of the line, or just after the last character, with shift-I or shift-A.
I'm not quite sure of the reasoning behind it, but you can work around it by pressing:
Shift + a
You might be interested in the option virtualedit and the following value:
set virtualedit=onemore
With this option you can move the cursor one character over the end of the line and then press i to insert mode after the last character.
This solves the issue in a way but personally I find this behavior a bit odd. Only in a few cases you encounter the problem so it might be worth ignoring it ;-)
That's because all commands you use affect the letter the cursor is over. If wouldn't make sense to press x (delete 1 letter) behind the actual letter.
There's actually no need to move the cursor in command mode behind the last letter, if you want to e.g. append you can press a which puts the cursor behind the letter in insertion mode.
It is implementation-dependent on whether the cursor can move past the end of the line (at least it is an option in my editor's VIM emulation mode). You can press a to enter insert mode and start editing after the current character in this situation (rather than i).
pressing i will enter the insert mode before the cursor
a after the cursor
I before the first non empty character in the line
A at the end of the line.
So, use A to quickly start typing at the end of the line.
I suggest
:set virtualedit=onemore
:inoremap <Esc> <Esc>`^

Vim keep cursor location while scrolling

Is there a way to keep the cusror location off-screen in Vim / gVim while scrolling? Similar to many Windows editors.
I know about marks, and do use them. I also know the '.' mark (last edit location), But looking for other ideas.
I'm asking this because sometimes i want to keep the cursor at some location, scroll to another place using the mouse-wheel, and then just press an arow key or something to get me back to that location.
No. vim is a console application, so it doesn't really make sense to have the cursour off-screen (it's possible, but would just be confusing)
An alternative solution, to paraphrase posts from this thread from comp.editors:
Ctrl+o goes to the previous cursor location, Ctrl+i goes to the next (like undo/redo for motions)
Marks seem like the other solution..
Also, use marks. Marks are named by letters. For instance typing ma remembers
the current location under mark a. To jump to the line containing mark a,
type 'a. To the exact location use `a.
Lower-case-letter marks are per-file. Upper-case-letter marks are global;
`A will switch to the file containing mark A, to the exact location.
Basically ma, move around, then `a to jump back.
Another option which Paul suggested,
gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Why don't you split the window, look at what you wanted to look at, and then close the split?
:split
or
:vsplit (if you want to split vertically)
The only similar behavior that I've found in Vim:
zt or zENTER "scroll the screen down as far as possible without moving the cursor"
zb "scroll as far up as possible".
Ctrl+E "scroll one line down, if possible"
Ctrl+Y"scroll one line up, if possible"
Sometimes you can avoid jumping to marks before entering text — gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Google says that the cursor (and therefore current line) must be visible in Vi, so you'll have to use marks.
Also very useful are the '' (2x single quotes) and `` (2x back quotes).
The former jumps back to the line you were prior to the last jump (for instance, a page down).
The latter jumps back to the line and column you were prior to the last jump.

Resources