Wikia

Vim Tips Wiki

Changes: Visual selection of indent block

Edit

Back to page

(Adjust previous/next navigation + minor manual clean)
(Format comment.)
 
(One intermediate revision by one user not shown)
Line 34: Line 34:
   
 
----
 
----
Currently, I just set foldmethod=indent and select the folded area.
+
Currently, I just <code>:set foldmethod=indent</code> and select the folded area.
   
 
----
 
----
  +
I believe this function might be better.
  +
<pre>
  +
function SelectIndent()
  +
let cur_line = line(".")
  +
let cur_ind = indent(cur_line)
  +
let line = cur_line
  +
while indent(line - 1) >= cur_ind
  +
let line = line - 1
  +
endw
  +
exe "normal " . line . "G"
  +
exe "normal V"
  +
let line = cur_line
  +
while indent(line + 1) >= cur_ind
  +
let line = line + 1
  +
endw
  +
exe "normal " . line . "G"
  +
endfunction
  +
nnoremap vip :call SelectIndent()<CR>
  +
</pre>
  +
--February 23, 2013
  +
:Thanks, but any chance of mentioning ''why'' it is better? [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 23:42, February 23, 2013 (UTC)

Latest revision as of 23:42, February 23, 2013

Tip 1014 Printable Monobook Previous Next

created 2005 · complexity basic · author Robert Schols · version 5.7


This command will make a visual selection of the lines that have the same indent level or more as the current line.

:exe "normal V" | let temp_var=indent(line(".")) | while indent(line(".")+1) >= temp_var | exe "normal j" | endwhile

CommentsEdit

A slight modification, to select the area above the cursor position as well, and the whole thing wrapped in a function (and nmapped to <Space>):

function! SelectIndent ()
  let temp_var=indent(line("."))
  while indent(line(".")-1) >= temp_var
    exe "normal k"
  endwhile
  exe "normal V"
  while indent(line(".")+1) >= temp_var
    exe "normal j"
  endwhile
endfun
nmap <Space> :call SelectIndent()<CR>

Currently, I just :set foldmethod=indent and select the folded area.


I believe this function might be better.

function SelectIndent()
  let cur_line = line(".")
  let cur_ind = indent(cur_line)
  let line = cur_line
  while indent(line - 1) >= cur_ind
    let line = line - 1
  endw
  exe "normal " . line . "G"
  exe "normal V"
  let line = cur_line
  while indent(line + 1) >= cur_ind
    let line = line + 1
  endw
  exe "normal " . line . "G"
endfunction
nnoremap vip :call SelectIndent()<CR>

--February 23, 2013

Thanks, but any chance of mentioning why it is better? JohnBeckett (talk) 23:42, February 23, 2013 (UTC)

Around Wikia's network

Random Wiki