Skip to content

Instantly share code, notes, and snippets.

@stechz
Created September 12, 2012 00:50
Show Gist options
  • Save stechz/3703356 to your computer and use it in GitHub Desktop.
Save stechz/3703356 to your computer and use it in GitHub Desktop.
fun! FactorEvalInput(string)
execute "normal a" . a:string
normal `[v`]hd
return @"
endfun
fun! FactorWrite() range
if s:factorWrite
iunmap <ESC>
augroup factor
au! InsertLeave <buffer>
augroup end
let cursor = getpos(".")
let s:factorWrite = 0
let stmt = @"
let varName = FactorEvalInput(@.)
if match(varName, "\\s*[a-zA-Z_][a-zA-Z0-9]*") == 0
let varName = substitute(varName, "\\s+", "", "")
let varName = substitute(varName, "[^a-zA-Z0-9_].*", "", "")
execute "normal Ovar " . varName . " = " . stmt . ";"
endif
let cursor[1] = cursor[1] + 1
call setpos(".", cursor)
endif
endfun
fun! Factor() range
normal gvd
normal `<
let s:factorWrite = 1
inoremap <silent> <ESC> <ESC>:call FactorWrite()<CR>
augroup factor
autocmd!
autocmd InsertLeave <buffer> call FactorWrite()
augroup end
startinsert
endfun
fun! FactorSelect() range
execute "normal /,\\|)\<CR>hv?,\\|(\<CR>/\\S\<CR>"
endfun
nmap ,f :call FactorSelect()<CR>
vmap ,f :call Factor()<CR>
@stechz
Copy link
Author

stechz commented Sep 12, 2012

Use this to break down dense or long lines of Javascript by replacing inline expressions with a variable set to that expression.

  • When writing a function call, you can type ,f and it will try to select the argument your cursor is on. This just uses a regex, so complex expressions may not be selected properly.
  • When in visual mode (meaning you have selected some text), you can type ,f and you will be in insert mode. Type in the name of your new variable, exit insert mode, and above the current line you will now magically have "var MYVAR = STATEMENT;"

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment