Vim Tips Wiki
Register
No edit summary
(Insert TipProposed template + minor manual clean)
Line 1: Line 1:
  +
{{TipProposed
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=September 9, 2010
  +
|complexity=basic
  +
|author=jeet
  +
|version=7.0
  +
|subpage=/201009
  +
|category1=
  +
|category2=
  +
}}
 
When trying to "squeeze" code between existing code, the "o" and "O" commands are useful to add a blank line below/above the current one and drop into insert mode. However, sometimes it would be nice to have one (or more) extra lines inserted at the same time, so that, for example, there is an extra line between the line you are currently working on and the code above or below.
 
When trying to "squeeze" code between existing code, the "o" and "O" commands are useful to add a blank line below/above the current one and drop into insert mode. However, sometimes it would be nice to have one (or more) extra lines inserted at the same time, so that, for example, there is an extra line between the line you are currently working on and the code above or below.
   
 
The following functions and key maps will do this.
 
The following functions and key maps will do this.
  +
<pre>
 
" '\o' : open multiple lines below and drop into insert mode [count]\o will
 
" insert [count] lines below current line, and then drop into insert mode one
 
" line below the current line (i.e., second line from the top of stack of the
 
" newly inserted lines).
  +
function! OpenLinesBelow()
 
let num_lines = v:count1
 
if num_lines < 2
 
let num_lines = 2
 
endif
 
let linesave = line('.')
 
let colsave = col('.')
 
let new_lines = 2
 
while num_lines > new_lines
 
put=''
 
let new_lines = new_lines + 1
 
endwhile
 
call cursor(linesave, colsave)
 
endfunction
 
nnoremap <Leader>o :<C-u>call OpenLinesBelow()<CR>o<CR>
   
" '\o' : open multiple lines below and drop into insert mode [count]\o will
+
" '\O' : open multiple lines above and drop into insert mode [count]\O will
" insert [count] lines below current line, and then drop into insert mode one
+
" insert [count] lines above current line, and then drop into insert mode one
" line below the current line (i.e., second line from the top of stack of the
+
" line above current line (i.e., second line from the bottom of the stack of
" newly inserted lines).
+
" the newly inserted lines).
function! OpenLinesBelow()
+
function! OpenLinesAbove()
let l:num_lines = v:count1
+
let num_lines = v:count1
if (l:num_lines < 2)
+
if (num_lines < 2)
let l:num_lines = 2
+
let num_lines = 2
endif
+
endif
let l:linesave = line(".")
+
let linesave = line('.')
let l:colsave = col(".")
+
let colsave = col('.')
let l:new_lines = 2
+
let new_lines = 1
while l:num_lines > l:new_lines
+
while num_lines > new_lines
put=''
+
put!=''
let l:new_lines = l:new_lines + 1
+
let new_lines = new_lines + 1
endwhile
+
endwhile
call cursor(l:linesave, l:colsave)
+
call cursor(linesave+num_lines-2, colsave)
endfunction
+
endfunction
nnoremap <Leader>o :<C-u>call OpenLinesBelow()<CR>o<CR>
+
nnoremap <Leader>O :<C-u>call OpenLinesAbove()<CR>O<ESC>o
  +
</pre>
 
" '\O' : open multiple lines above and drop into insert mode [count]\O will
 
" insert [count] lines above current line, and then drop into insert mode one
 
" line above current line (i.e., second line from the bottom of the stack of
 
" the newly inserted lines).
 
function! OpenLinesAbove()
 
let l:num_lines = v:count1
 
if (l:num_lines < 2)
 
let l:num_lines = 2
 
endif
 
let l:linesave = line(".")
 
let l:colsave = col(".")
 
let l:new_lines = 1
 
while l:num_lines > l:new_lines
 
put!=''
 
let l:new_lines = l:new_lines + 1
 
endwhile
 
call cursor(l:linesave+l:num_lines-2, l:colsave)
 
endfunction
 
nnoremap <Leader>O :<C-u>call OpenLinesAbove()<CR>O<ESC>o
 
 
[[Special:Contributions/68.95.117.66|68.95.117.66]] 15:06, September 9, 2010 (UTC)jeet
 
 
 
 
==References==
 
<!-- Put any help links you want here in a list as follows: -->
 
*{{help|toc}}
 
   
 
==Comments==
 
==Comments==

Revision as of 08:22, 20 September 2010

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created September 9, 2010 · complexity basic · author jeet · version 7.0

When trying to "squeeze" code between existing code, the "o" and "O" commands are useful to add a blank line below/above the current one and drop into insert mode. However, sometimes it would be nice to have one (or more) extra lines inserted at the same time, so that, for example, there is an extra line between the line you are currently working on and the code above or below.

The following functions and key maps will do this.

" '\o' : open multiple lines below and drop into insert mode [count]\o will
" insert [count] lines below current line, and then drop into insert mode one
" line below the current line (i.e., second line from the top of stack of the
" newly inserted lines).
function! OpenLinesBelow()
  let num_lines = v:count1
  if num_lines < 2
    let num_lines = 2
  endif
  let linesave = line('.')
  let colsave = col('.')
  let new_lines = 2
  while num_lines > new_lines
    put=''
    let new_lines = new_lines + 1
  endwhile
  call cursor(linesave, colsave)
endfunction
nnoremap <Leader>o :<C-u>call OpenLinesBelow()<CR>o<CR>

" '\O' : open multiple lines above and drop into insert mode [count]\O will
" insert [count] lines above current line, and then drop into insert mode one
" line above current line (i.e., second line from the bottom of the stack of
" the newly inserted lines).
function! OpenLinesAbove()
  let num_lines = v:count1
  if (num_lines < 2)
    let num_lines = 2
  endif
  let linesave = line('.')
  let colsave = col('.')
  let new_lines = 1
  while num_lines > new_lines
    put!=''
    let new_lines = new_lines + 1
  endwhile
  call cursor(linesave+num_lines-2, colsave)
endfunction
nnoremap <Leader>O :<C-u>call OpenLinesAbove()<CR>O<ESC>o

Comments