Vim Tips Wiki
m (Inserting a new-line without entering insert mode moved to Insert newline without entering insert mode: Page moved by JohnBot to improve title)
(Undo revision 38356 by Zumalifeguard (talk) old method works fine for me (in a Vim with no configuration) and it's better anyway since it leaves the cursor on the top line)
Tag: sourceedit
(25 intermediate revisions by 13 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=982
 
|id=982
  +
|previous=981
|title=Inserting a new-line without entering insert mode
 
  +
|next=983
|created=September 3, 2005 8:49
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Christopher Auer
 
|author=Christopher Auer
 
|version=5.7
 
|version=5.7
 
|rating=26/20
 
|rating=26/20
  +
|category1=Usage
|text=
 
  +
|category2=
As you all know with 'o' or 'O' you can insert a new line before/after the current line. But both commands enter the insert mode, which may sometimes not what you want. I put this in my vimrc-file to insert a new-line after the current line by pressing <ENTER> (<SHIFT+ENTER> for inserting a line _before_ the current line):
 
 
}}
 
As you all may know with 'o' or 'O' you can insert a new line after/before the current line. But both commands enter the insert mode, which may sometimes not be what you want. I put this in my vimrc-file to insert a new-line after the current line by pressing Enter (Shift-Enter for inserting a line before the current line):
  +
<pre>
 
nmap &lt;S-Enter> O<Esc>
  +
nmap <CR> o<Esc>
  +
</pre>
   
 
If you want to stay in the line where you have been before use the following maps:
  +
<pre>
 
nmap &lt;S-Enter> O<Esc>j
  +
nmap <CR> o<Esc>k
  +
</pre>
   
  +
==See also==
  +
*[[Use Return and Delete keys in normal mode like in insert mode]]
   
 
==Comments==
map &lt;S-Enter&gt; O&lt;ESC&gt;
 
 
It's also a nice idea to map something like
  +
<pre>
  +
nnoremap <C-J> a<CR><Esc>k$
  +
</pre>
   
 
because it's the opposite of Shift-J. A more sophisticated solution would be
map &lt;Enter&gt; o&lt;ESC&gt;
 
  +
<pre>
 
nnoremap <C-J> ciW<CR><Esc>:if match( @", "^\\s*$") < 0<Bar>exec "norm P-$diw+"<Bar>endif<CR>
  +
</pre>
   
  +
----
  +
For whatever reason, I was unable to get the suggest mapping with &lt;S-Enter&gt; and &lt;CR&gt; to work properly (both ended up inserting below). So I did:
  +
<pre>
  +
nmap <F8> o<Esc>
  +
nmap <F9> O<Esc>
  +
</pre>
   
  +
Which works fine for me.
   
  +
----
You should not map this to &lt;ENTER&gt; if you use &lt;ENTER&gt; to navigate (obivously).
 
  +
&lt;S-Enter&gt; and &lt;CR&gt; will work separately in gvim but not in urxvt. See http://stackoverflow.com/questions/598113/can-terminals-detect-shift-enter-or-control-enter
   
  +
----
  +
Referencing the above, the default terminal on MacOS returns &#94;M with all modifier keys.
   
  +
If you don't use return to navigate in normal mode, you can remap two returns to insert a new line:
  +
<pre>:nmap <CR><CR> o<ESC></pre>
   
  +
----
If you want to stay in the line where you have been before use the following maps:
 
  +
Since I have <code>inoremap jj <Esc></code> in my <code>.vimrc</code>, it take not too much to type <code>ojj</code> or <code>Ojj</code>.
   
 
 
map &lt;S-Enter&gt; O&lt;ESC&gt;j
 
 
map &lt;Enter&gt; o&lt;ESC&gt;k
 
 
 
 
This is nothing great compared to other useful tips here, but I found it nifty (especially to structure program code etc.)
 
 
 
 
[I also hope that this has not been posted before as this tip seems very obvious to me...]
 
}}
 
 
== Comments ==
 
It's also a nice idea to map something like
 
 
nnoremap &lt;C-J&gt; a&lt;cr&gt;&lt;esc&gt;k$
 
 
because it's the opposite of Shift-J.
 
A more sophisticated solution would be
 
 
nnoremap &lt;C-J&gt; :exec ".s/\\s*\\(\\S*\\)\\%".col(".")."c\\s*/\\r\\1/e"&lt;cr&gt;k$
 
 
 
vim at bertram-scharpf dot de
 
, September 5, 2005 2:02
 
 
----
 
----
  +
To add a blank line below or above and keep the cursor in place, I think this is easiest:
Sorry, that doesn't work when you're on the first space after a word. Better is
 
  +
<pre>
  +
nnoremap <C-J> m`o<Esc>``
  +
nnoremap <C-K> m`O<Esc>``
  +
</pre>
   
  +
It uses the jump list to remember the cursor position, so it does clutter up that list. But I don't mind.
nnoremap &lt;C-J&gt; ciW&lt;cr&gt;&lt;esc&gt;:if match( --AT--", "^\\s*$") &lt; 0&lt;Bar&gt;exec "norm P-$diw+"&lt;Bar&gt;endif&lt;cr&gt;
 
  +
You could use any other mark, like so (uses mark a):
  +
<pre>
  +
nnoremap <C-J> mao<Esc>`a
  +
nnoremap <C-K> maO<Esc>`a
  +
</pre>
   
 
vim at bertram scharpf dot de
 
, September 16, 2005 2:18
 
 
----
 
----
At OP: Thanks. It's a simple tip, but I found it useful, since I don't really use the Enter key for anything.
 
 
   
bmintern at gmail dot com
 
, August 23, 2007 12:00
 
 
----
 
----
  +
'''This mapping interferes with command execution in the command-line window.'''
<!-- parsed by vimtips.py in 0.459360 seconds-->
 
  +
The command-line window can operate in normal or insert mode and in both <code><CR></code> executes the command from current line. Using <code>nmap</code> instead of <code>map</code> in the mapping declaration partially fixes the problem. Then, to execute the command, you must enter the insert mode and press <code><CR></code>. While this is a feasible workaround, I prefer to dispense with this mapping and type <code>oii</code> to insert a new line (<code>ii</code> being my map for <code><Esc></code>).
  +
:Excellent point! <code>:map</code> applies to too many modes, I went ahead and made it <code>:nmap</code>. For the other problem, we should edit the mapping to detect the command-line window, like in [[Syntax folding of Vim scripts]]. Probably this will need an <code><expr></code> form of the mapping to do easily. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 15:01, June 27, 2013 (UTC)

Revision as of 15:42, 19 May 2015

Tip 982 Printable Monobook Previous Next

created 2005 · complexity basic · author Christopher Auer · version 5.7


As you all may know with 'o' or 'O' you can insert a new line after/before the current line. But both commands enter the insert mode, which may sometimes not be what you want. I put this in my vimrc-file to insert a new-line after the current line by pressing Enter (Shift-Enter for inserting a line before the current line):

nmap <S-Enter> O<Esc>
nmap <CR> o<Esc>

If you want to stay in the line where you have been before use the following maps:

nmap <S-Enter> O<Esc>j
nmap <CR> o<Esc>k

See also

Comments

It's also a nice idea to map something like

nnoremap <C-J> a<CR><Esc>k$

because it's the opposite of Shift-J. A more sophisticated solution would be

nnoremap <C-J> ciW<CR><Esc>:if match( @", "^\\s*$") < 0<Bar>exec "norm P-$diw+"<Bar>endif<CR>

For whatever reason, I was unable to get the suggest mapping with <S-Enter> and <CR> to work properly (both ended up inserting below). So I did:

nmap <F8> o<Esc>
nmap <F9> O<Esc>

Which works fine for me.


<S-Enter> and <CR> will work separately in gvim but not in urxvt. See http://stackoverflow.com/questions/598113/can-terminals-detect-shift-enter-or-control-enter


Referencing the above, the default terminal on MacOS returns ^M with all modifier keys.

If you don't use return to navigate in normal mode, you can remap two returns to insert a new line:

:nmap <CR><CR> o<ESC>

Since I have inoremap jj <Esc> in my .vimrc, it take not too much to type ojj or Ojj.


To add a blank line below or above and keep the cursor in place, I think this is easiest:

nnoremap <C-J> m`o<Esc>``
nnoremap <C-K> m`O<Esc>``

It uses the jump list to remember the cursor position, so it does clutter up that list. But I don't mind. You could use any other mark, like so (uses mark a):

nnoremap <C-J> mao<Esc>`a
nnoremap <C-K> maO<Esc>`a


This mapping interferes with command execution in the command-line window. The command-line window can operate in normal or insert mode and in both <CR> executes the command from current line. Using nmap instead of map in the mapping declaration partially fixes the problem. Then, to execute the command, you must enter the insert mode and press <CR>. While this is a feasible workaround, I prefer to dispense with this mapping and type oii to insert a new line (ii being my map for <Esc>).

Excellent point! :map applies to too many modes, I went ahead and made it :nmap. For the other problem, we should edit the mapping to detect the command-line window, like in Syntax folding of Vim scripts. Probably this will need an <expr> form of the mapping to do easily. --Fritzophrenic (talk) 15:01, June 27, 2013 (UTC)