How to collapse space after cursor in vim - vim

I have my cursor between two lines in vim:
I would do this in a 'normal' editor by pressing ctrl-d to delete the characters after the cursor. What would be the most efficient way to do this in vim?

Your current cursor is at ,:
if you want to remove the linebreak: press J
if you want to remove the ',' : press x
if you want to remove the closing single-quote ': press X

If you want to join lines with no space in beewin them press
gJ
If you want to remove the rest of the line in insert mode:
Ctrl-o D
To map this action, you can put this on your ~/.vimrc
" delete the rest of the current line
inoremap <c-k> <c-o>d
I'm not suggesting maping Ctrld because it is already mapped to decrease indenting, in oposition to Ctrlt

Related

vim how to break new line without entering Insert mode

I have a few lines of codes that are too that I would like to break into 2 lines at certain location.
So instead of moving to the position then presse I to insert mode then Enter to break line then finally ESc back.
Is there way I can do it easier in normal mode only?
Many thanks.
You can define some simple mapping for it:
" <C-Enter> Insert single / [count] newline.
nnoremap <C-CR> i<CR><Esc>
Note that <C-CR> probably only works in GVIM, not in a terminal; choose a different key if necessary.
Here's an additional mapping that keeps the cursor on the original line:
" <C-S-Enter> Append single / [count] newline.
function! s:AppendCRSetPos()
keepjumps call setpos("''", getpos('.'))
return ''
endfunction
nnoremap <expr> <SID>(AppendCRSetPos) <SID>AppendCRSetPos()
nnoremap <script> <C-S-CR> <SID>(AppendCRSetPos)i<CR><Esc>g``
I have the following in my .vimrc
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
Which will split the line on pressing capital S.
Hope this helps
There is a vim key for that:
r<Enter>

Vim exiting insert mode moves cursor to next line

After entering some text at the end of the line and exiting insert mode I have the cursor moved to the beginning of the next line. I would like to prevent this.
It suprisingly was a linefeed character (or maybe some other character) in .vimrc interpreted by vim as command.
I have the following lines to remap mode change to '
:nnoremap ' i
:inoremap ' <esc>
Apparantly CR somehow occured to be after <esc>. Since there is no text after, I presumed it could be some whitespace character. All I had to do is to place the cursor after <esc> and delete all trailing invisible symbols. And it did the trick.

Vim <C-Right> and <C-Left> remaping

When i press <C-Right> in insert mode, vim does the same thing that it would being in normal mode and pressing w. But i would like to remap C-Right and C-Left so the effect would be more like the e key (specially at the end of a line, when i'm just before the last word i don't want vim to skip the line and go to the next line word, i want it to put the cursor just at the end of the line.)
I tried for the insert mode :inoremap <C-Right> <Esc> e a
but it keeps jumping the line, besides <Esc> e a doing exactly what i want.
In normal mode, :nnoremap <C-Right> E does what i want.
What i am missing to make the remap work?
Thank you for your time reading the question.
Don't put spaces inbetween the commands (or after them). All the spaces count as part of the command (which is move the cursor to the right)
:inoremap <C-Right> <Esc> e a
should be
:inoremap <C-Right> <Esc>ea

move the text after the cursor to a new line

I am Vim newbie, and I'm using MacVim on OSX Snow Leopard. One of the most common actions I have to take is to move the cursor to a new line but also move the text after the cursor to the new line. I know that pressing 'o' in normal or visual mode moves the cursor to a new line and switches the mode to insert.
What I'd like to do is move the cursor to a new line, and move the text after the cursor to that new line as well, preferably staying in the normal mode? Is this possible? How can I accomplish this task?
If the cursor is on a <space> as in ([] marks the cursor):
lorem ipsum[ ]dolor sit amet
the simplest is to do r<CR>, that is "replace the current character with a linebreak".
Otherwise, use #knittl's solution.
So you want to move everything in the current line, which comes after the cursor to the next line? Read: insert a line break??
(move cursor)
i (or a)
<return>
<esc> (or ^C)
To map this sequence of keystrokes to a single key, follow #thb's suggestion and use the :map command:
:map <F2> i<CR><ESC>
:map <F2> i<CR>
This keeps vi in insert mode.
As I answered in this post, How do I insert a linebreak where the cursor is without entering into insert mode in Vim?.
Please try Control + j.
The code below achieves the same behavior as "normal" editors (for the lack of better terms on the top of my mind) except that you'd have to press "enter" twice instead of once.
I also wanted to get rid of the space if it's right before my current character.
There might be an easier way and I totally welcome edits :-)
" in ~/.vimrc or ~/.vimrc.after if you're using janus
nnoremap <cr><cr> :call ReturnToNewLine()<cr>
function ReturnToNewLine()
let previous_char = getline(".")[col(".")-2]
" if there's a space before our current position, get rid of it first
if previous_char == ' '
execute "normal! \<bs>\<esc>"
endif
execute "normal! i\<cr>\<esc>"
endfunction
This remaps pressing enter twice to going to insert mode, placing a carriage return and escaping.
The reason I'm using this mapping (enter twice) is because I was used to this functionality with other text editors by pressing a enter; also, typing enter twice is fast.
Another thing that I found useful in this context was allowing vim to move right after the last character (in case I wanted to move the last character to a new line). So I have the following in my ~/.vimrc as well.
set virtualedit=onemore
Note that I'm using nnoremap (normal mode non-recursive) instead of map (which is VERY dangerous) (check this out for more information on the differences http://learnvimscriptthehardway.stevelosh.com/chapters/05.html)
You need to map some keys to do a line break at the cursor,
I found the following mapping easy to use, just go to your vimrc and add this line:
:map <silent> bl i<CR><ESC>
to assign a line break at cursor to "bl" combo

Deleting spaces on multiple lines in VIM

To indent HAML code I usually add or delete 2 spaces. Adding I do:
Enter visual mode ( ctrl + v)
jj to select the lines
shift + i to go into insert
type 2 spaces
ESC
That's it 2 spaces are added. However to remove the spaces, I't does not work, for example doing:
Enter visual mode ( ctrl + v)
jj to select the lines
shift + i to go into insert
Delete 2 spaces ( with backspace or delete)
ESC
This just does not work, other lines spaces are not deleted. How then can I do this ?
Here is an example code:
.module_1
.pricing_details
%h2
Save
The idea is moving everything so it matches 2 space in respecto .module_1 as:
.module_1
.pricing_details
%h2
Save
The propose solution using < > works only for indenting now I'd like to for example:
.module_1
.pricing_details
%h2
Save
move the above to:
.module_1
.pricing_details
%h2
Save
Try < and > commands. You will need :set shiftwidth=2 for them to work in this way.
UPDATE
Considering your last example, changing
.module_1
.pricing_details
%h2
Save
to ⇓
.module_1
.pricing_details
%h2
Save
can be accomplished with moving to .pricing_details line and hitting Vjj<.
Highlight your text and do:
<
Use:
.
To repeat the action multiple times. Note that this will shift the text whatever your shift width is. If it is not 2, you can set it to 2 by doing:
:set sw=2
You can indent text the same way by using ">".
All of this is in the documentation: http://vimdoc.sourceforge.net/htmldoc/usr_25.html#25.3
in the vimrc:
" pressing F5 adds two spaces at beginning of line and goes to next line
inoremap <F5> <ESC>:s/\(.*\)/ \1/e<CR>:set nohlsearch<CR>ji
" also works when not in edit mode
map <F5> i<F5><ESC>
" F6 removes two spaces from the end of whitespace at the beginning of line
inoremap <F6> <ESC>:s/\(^\s*\)/\1/e<CR>:set nohlsearch<CR>ji
map <F6> i<F6><ESC>
To remove 2 spaces from the beginning of every line of a paragraph just press F5 through all its lines.
This is modeled after my keybindings for commenting and uncommenting C code (the difference is in the regex of course)
only drawback is it needs to disable search highlight since the regex matches damn near the entire document all the time.

Resources