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
Related
I have a mapping nnoremap <leader>l i<space><esc> that inserts a space while staying in a normal mode, and the mapping is dot-repeatable. However, the cursor stays where it was and the space "expands" to the right of the cursor.
I want to have a mapping that does the same but moves the cursor along, e.g.
nnoremap <leader>h i<space><esc>l
I love breaking sequences of characters with a space from normal mode and I want to decide at will if the cursor should stay or move (using either h to move the cursor along or l to move the text to the left while leaving the cursor where it is).
None of the tricks with i or a or <C-o> seems to work and this is an expected behavior.
Is there any clever hack to accomplish dot-repeatability using nnoremap only? It should also work if I am at the end of the line.
I can probably re-phrase it like this: I want to have functionality similar to X and x that deletes a single character before or at the cursor, but instead of deleting a character I want to insert a <space> before or at the cursor (not after the cursor as with a). And it should be dot-repeatable and take counts. Preferably a mapping that does not use more than a single line in the .vimrc and does not require any plugins. And I don't want to change the behaviour of <esc> (or i, whichever is responsible for moving the cursor when one leaves the insert mode) that moves a cursor one character to the left.
Edit 1:
Dot-repeatable nnoremap <leader>l i<space><esc> does this:
<leader>l...
AAAAAAAAAA[B]BBBBBBBB
AAAAAAAAAA[ ]BBBBBBBBB
AAAAAAAAAA[ ] BBBBBBBBB
AAAAAAAAAA[ ] BBBBBBBBBB
And I want a dot-repeatable <leader>h that would do this:
<leader>h...
AAAAAAAAAA[B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBB
AAAAAAAAAA [B]BBBBBBBBB
Edit 2:
The workaround mentioned in reply by #romainl does the trick:
function! s:insspace(...)
if a:0
" perform operation
execute 'normal' v:count1.'i '."\<esc>".'l'
else
" set up
let &operatorfunc = matchstr(expand('<sfile>'), '[^. ]*$')
return "g#\<space>"
endif
endfunction
nnoremap <silent><expr> <leader>h <sid>insspace()
Can someone explain how it works? I am a beginner in vim...
Given the string below and assuming the cursor is on the -:
lorem-ipsum
^
There are various easy and repeatable ways to insert a space after the - and leave the cursor on the space:
a <Esc>
s<C-r>" <Esc>
lorem- ipsum
^
The two macros above…
are "dot-repeatable", because there is no motion involved,
leave the cursor on the space because, after an insertion, Vim places the cursor on the last inserted character.
But this is precisely the latter behaviour that prevents us from doing the same in the other direction without a motion either before or after the insertion.
Without motion, the operation is "dot-repeatable" but the cursor is left on the -:
i <Esc>
a <Esc>
s <C-r>"<Esc>
lorem -ipsum
^
With motion, the cursor is left on the space but the operation is not "dot-repeatable":
i <Esc><Left>
<Left>a <Esc>
s <C-r>"<Esc><Left>
lorem -ipsum
^
The workaround is a bit contrived but nifty.
Maybe I did not understand your question well enough, but for me this does the trick:
:nnoremap <leader>h i<space><esc>w
Rationale: Insert space, after <ESC> go one left, so the cursor stays on the space, use w to get to the next word boundary.
Is that what you want? I can now do 5\h and it stays at the current position while inserting spaces to the left.
Is there a way to jump to the next word when in the insert mode. Something similar to w when in non-insert mode or Ctrl+Left or Ctrl+Right in windows?
There's a very useful table of insert mode motions at :h ins-special-special.
<S-Left> cursor one word back (like "b" command)
<C-Left> cursor one word back (like "b" command)
<S-Right> cursor one word forward (like "w" command)
<C-Right> cursor one word forward (like "w" command)
You'll find that Shift-Left/Right and Ctrl-Left/Right will do the trick.
Ctrl/Shift with cursor keys isn't guaranteed to work flawlessly in all terminals, however. You can avoid any problems entirely by using a mapping. Here's one right on to the home row:
:inoremap <C-H> <C-\><C-O>b
:inoremap <C-L> <C-\><C-O>w
Now use CTRL-H and CTRL-L to move by word in insert mode.
However, please be aware that many Vimmers prefer not to move at all in insert mode. That's because once you have moved in insert mode, the . command loses its utility, as does CTRL-A and maybe some other commands.
For me, the Vim way of jumping to the next word in insert mode is <C-[>wi and it's become completely automatic.
With <CTRL-O>, you can execute one command without exiting insert mode.
So you can try <CTRL-O>w, <CTRL-O>3w, etc.
You can move one word forwards/backwards in Insert mode by holding down either Shift or Control and pressing the right or left arrow key.
I know that I can use either:
Home in insert mode
Esc + i to exit insert mode and enter it again, effectively going to the beginning of line.
But neither satisfies me. In first case I have to tilt my head to hit Home, because I can't blindly hit it. In second case my left arm has to leave the home row to hit Esc, which is annoying too.
Any thoughts?
Ctrl+O whilst in insert mode puts you in command mode for one key press only. Therefore Ctrl+O then Shift+I should accomplish what you're looking for.
You could enter insert mode using I (capital i).
It will put the cursor at the beginning of the line.
Similarly you can use A to add something at the end of the line.
Though, it does not really solve the problem of moving while already being in Insert mode.
I have just checked help on Insert mode, there is no key combination in insert mode to move at the beginning of the line.
Other idea :
Remap a new command only in insert mode
inoremap <C-i> <Home>
I've got Ctrl+a and Ctrl+e mapped to beginning and end of line, respectively. This matches the behavior of most bash command lines. Works well for me.
inoremap <C-e> <Esc>A
inoremap <C-a> <Esc>I
If you are using MacOS Terminal go to Preferences...>Settings>Keyboard and map the end key to Ctrl-O$ (it is displayed as \017$) and then use fn+left to simulate the end key. Do the same for the home key. Escape sequence \033[H also works for home.
Your best course of action is to remap the action to a different key (see How to remap <Ctrl-Home> to go to first line in file? for ideas)
I'd think of how often I use this "feature" and map it to a keystroke accordinly
You can map the keys to this:
inoremap II <Esc>I
ref: http://vim.wikia.com/wiki/Quick_command_in_insert_mode
A shortcut that has worked for me (both muscle memory and intuitiveness) is to map __ (which is a double _) to "insert at start of current line".
Rationale:
_ already goes to the start of line
in vim, doubling anything is a very common way of doing that "to this line"
double _ doesn't conflict with any motions (you're already at the start of line)
your hand is already in the right place if you went to the beginning of the line and now want to insert.
vimscript:
"insert at start of current line by typing in __ (two underscores)
function DoubleUnderscore()
if v:count == 0 && getcurpos()[2] == 1
:silent call feedkeys('I', 'n')
else
:silent call feedkeys('^', v:count + 'n')
endif
endfunction
nnoremap <silent> _ :call DoubleUnderscore()<CR>
It's this complicated because the easy alternative nnoremap __ _I causes vim to delay on pressing _ to distinguish between _ and __.
ctrl+o then 0
| |
letter number
i use this command to go to end of line without leaving insert mode
inoremap jl <esc><S-a>
Similarly to go to beginning of line will be:
inoremap jl <esc><S-i>
Is there a simple way (i.e. without writing a script or elaborate keymap sequence) to Yank a group of lines and leave the cursor wherever the Yank was performed, as opposed to at the start of the block?
According to VIM's help: "Note that after a characterwise yank command, Vim leaves the cursor on the first yanked character that is closest to the start of the buffer." Line-wise seems to behave similarly.
This is a bit annoying for me since I tend to select a large region from top to bottom, Yank, and then paste near or below the bottom of the selected region. Today I'm setting a mark (m-x) just before Yank and then jumping back, but I suspect there may be a different Yank sequence that will do what I need.
I've searched SO and the web for this numerous times. There is so much existing "VIM shortcuts" material to wade through yet I've not found a solution to this one yet.
Thanks in advance.
Not quite answering your question, but perhaps '] would solve your problem?
'] `] To the last character of the previously changed or
yanked text. {not in Vi}
If you're using visual blocks (v), then after yanking the block you can use gv to re-select the same block (which also moves your cursor position back to where it was before yanking). If you then press Esc, the block is un-selected without moving the cursor.
Also of interest might be the ctrl-o command in visual block mode, which jumps between the start and end of the selected block.
If yanking in visual mode, you could also use '> or `> to go to the last line/character of the just yanked visual selection. In this context this is essentially the same as '] and `] which is apparently not supported e.g. in VsVim.
:y3 will yank three whole lines from current current line, If you know exactly how many line to yank, this command is very handy. :help :yank for details.
:%y will select the whole buffer without moving the cursor,
like ggvG$y, without the flash of selection highlight and modifying the "* register.
I use this insert mode map:
function! SelectAll()
%y*
endfun
imap <expr> <F3> SelectAll()
ps: if you prefer <C-V> to paste(outside vim), use %y+
check https://stackoverflow.com/a/1620030/2247746
I'm not sure sure if YankRing has changed since the vmap ygv<Esc> solution was posted but that didn't persist for me after adding it to my .vimrc. YankRing actually overwrote it.
Here's my solution in .vimrc.
function! YRRunAfterMaps()
vmap y ygv<Esc>
endfunction
I don't know what happened in the meantime but in IdeaVim the accepted answers don't work as I'd like them to. (Don't move the cursor on yank)
For me what did the trick was just setting a mark before yank and go to that afterwards.
vmap y mxy`x
I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
Shortly after :help p it says:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as
new lines.
:[line]pu[t]! [x] Put the text [from register x] before [line]
(default current line).
Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even p):
:nmap p :pu<CR>
Options:
1) Use yy to yank the whole line (including the end of line character). p will then paste the line on a new line after the current one and P (Shift-P) will paste above the current line.
2) Make a mapping: then it's only one or two keys:
:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p
3) The function version of the mapping (unnecessary really, but just for completeness):
:nmap <F4> :call append(line('.'), #")<CR>
" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(#", '\n$', '', ''))<CR>
:help let-register
:help :call
:help append()
:help line()
:help nmap
You can paste a buffer in insert mode using <C-R> followed by the name of the buffer to paste. The default buffer is ", so you would do
o<C-R>"
I found that I use <C-R>" very often and bound that to <C-F> in my vimrc:
inoremap <C-F> <C-R>"
This still uses three keystrokes, but I find it easier than Esc:
o<Alt-p>
Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.
Using this plugin: https://github.com/tpope/vim-unimpaired
]p pastes on the line below
[p pastes on the line above
advantages:
works on all yanked text (word, line, character, etc)
indents the pasted text to match the indentation of the text
around it
2 keystrokes instead of 3 and much "easier" strokes
fast
Personally I've nmapped Enter (CR) like this:
nmap <CR> o<Esc>k
...based on this Vim Wikia article.
This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:
<CR>jp
You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be <CR>p.
I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.
Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.
If you wanted to stay in the insert mode, you can do o ctrl+o p
o – insert mode and go to the new line
ctrl+o – run a single command
like in normal mode
p – paste
It's three keystrokes but you stay in insert mode and also o ctrl+o is quite fast so I personally treat it as 2.5 keystrokes.
If you're copying a whole line then pasting a whole line, use Y to yank the line or lines, including line break, in the first place, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.
I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.
so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.
I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with
set clipboard+=unnamed
than you can do o<Ctl-v>.
Besides being fewer strokes, this improves on both o<Esc>p and :pu because it preserves indenting: both of the other options start you at character zero on the new line.
Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.
If you want to paste in a new line and still keep indentation, create this mapping:
nnoremap <leader>p oq<BS><Esc>p
Prerequisite: you have leader mapped and you have set autoindent in your .vimrc.
Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.
If you also want to end in insert mode, it is possible to paste while in insert mode using CTRL-R ". https://stackoverflow.com/a/2861909/461834
Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.
I use the following mapping in my Neovim config:
nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``
A little explanation:
m`: set a mark in the current cursor position.
o<Esc>p: create a new line below and paste the text in this line
O<Esc>P: create a new line above and paste the text in this line
``: put the cursor in the original position
See :h mark for more information about marks in Vim.
This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.