Last active
August 29, 2015 14:19
-
-
Save IsoLinearCHiP/ea503ed9a32b903408f7 to your computer and use it in GitHub Desktop.
Vim functions for indentation conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " Functions to convert between tab indenting and space indenting | |
| " in contrast to :retab! it only replaces indentation at the line start. | |
| " | |
| " adapted from http://stackoverflow.com/a/5173322/1673337 | |
| " | |
| " Example: | |
| " set tabstop=1 | call RetabToTab() | set tabstop=2 | RetabToSpace() | |
| " | |
| " would solve my common problem of reindenting a file from 1 space to 2 spaces per level | |
| command! RetabToTab call RetabToTab() | |
| command! RetabToSpace call RetabToSpace() | |
| func! RetabToTab() | |
| let saved_view = winsaveview() | |
| execute '%s@^\(\ \{'.&ts.'\}\)\+@\=repeat("\t", len(submatch(0))/'.&ts.')@e' | |
| call winrestview(saved_view) | |
| endfunc | |
| func! RetabToSpace() | |
| let saved_view = winsaveview() | |
| execute '%s@^\(\t\)\+@\=repeat(" ", len(submatch(0))*'.&ts.')@e' | |
| call winrestview(saved_view) | |
| endfunc | |
| " Function to visually select all code having the same or greater indent level | |
| " | |
| " from http://vim.wikia.com/wiki/VimTip1014 | |
| " bind to Tab in visual select mode | |
| vnoremap <tab> :call SelectIndent()<CR> | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment