├── LICENSE ├── README.md └── plugin └── ExtractToVariable.vim /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Franco Victorio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-extract-variable 2 | 3 | Extract an expression to a variable. 4 | 5 | ![vim-extract-variable demo](https://i.imgur.com/cHbl0xk.gif) 6 | 7 | ## Installation 8 | 9 | Use one of the hundred of methods that exist to install a vim plugin. I recommend [vim-plug](https://github.com/junegunn/vim-plug): 10 | 11 | ``` 12 | Plug 'fvictorio/vim-extract-variable' 13 | ``` 14 | 15 | ## Usage 16 | 17 | A normal and visual mapping is set: `ev`. If you use it in visual mode, it extracts the selected text to a variable. The name of the variable will be prompted to you. If you use it in normal mode, the text between parentheses is used. 18 | 19 | The declaration is created in the previous line, using `O`, so it should preserve the indentation if you have `autoindent` on. 20 | 21 | ## Supported languages 22 | 23 | The following languages are supported: 24 | 25 | | Language | Result | 26 | | ----------------------| ------------------------ | 27 | | Elixir | `foo = 42` | 28 | | Go | `foo := 42` | 29 | | JavaScript | `const foo = 42` | 30 | | R | `foo <- 42` | 31 | | Ruby | `foo = 42` | 32 | | TypeScript | `const foo = 42` | 33 | | Python | `foo = 42` | 34 | | C# | `var foo = 42;` | 35 | | C++ | `auto foo = 42;` | 36 | -------------------------------------------------------------------------------- /plugin/ExtractToVariable.vim: -------------------------------------------------------------------------------- 1 | if exists("g:loaded_extract_variable") || &cp 2 | finish 3 | endif 4 | let g:loaded_extract_variable = 1 5 | 6 | function! s:ExtractToVariable(visual_mode) 7 | " Check if 'filetype' is set 8 | if &filetype == '' 9 | echo "'filetype' is not set" 10 | return 11 | endif 12 | 13 | " Check if language is supported 14 | let l:supported_languages = ['elixir', 'go', 'javascript', 'r', 'typescript', 'typescriptreact', 'javascriptreact', 'python', 'ruby', 'rust', 'julia', 'cs', 'cpp'] 15 | let l:filetype = split(&filetype, '\.')[0] 16 | 17 | if index(l:supported_languages, l:filetype) == -1 18 | echo l:filetype . ' is not supported. Please open an issue at https://github.com/fvictorio/vim-extract-variable/issues/new' 19 | return 20 | endif 21 | 22 | " Yank expression to z register 23 | let saved_z = @z 24 | if a:visual_mode ==# 'v' 25 | execute "normal! `\"zy" 26 | else 27 | execute "normal! vib\"zy" 28 | endif 29 | 30 | " Ask for variable name 31 | let varname = input('Variable name? ') 32 | 33 | if varname != '' 34 | execute "normal! `s".varname."\" 35 | 36 | 37 | if l:filetype ==# 'cs' 38 | execute "normal! Ovar ".varname." = ".@z.";\" 39 | elseif l:filetype ==# 'cpp' 40 | execute "normal! Oauto ".varname." = ".@z.";\" 41 | elseif l:filetype ==# 'javascript' || l:filetype ==# 'typescript' || l:filetype ==# 'typescriptreact' || l:filetype ==# 'javascriptreact' 42 | execute "normal! Oconst ".varname." = ".@z."\" 43 | elseif l:filetype ==# 'go' 44 | execute "normal! O".varname." := ".@z."\" 45 | elseif l:filetype ==# 'rust' 46 | execute "normal! Olet ".varname." = ".@z."\" 47 | elseif l:filetype ==# 'elixir' || l:filetype ==# 'python' || l:filetype ==# 'ruby' || l:filetype ==# 'julia' 48 | execute "normal! O".varname." = ".@z."\" 49 | elseif l:filetype ==# 'r' 50 | execute "normal! O".varname." <- ".@z."\" 51 | endif 52 | else 53 | redraw 54 | echo 'Empty variable name, doing nothing' 55 | endif 56 | 57 | let @z = saved_z 58 | endfunction 59 | 60 | nnoremap ev :call ExtractToVariable('') 61 | vnoremap ev :call ExtractToVariable(visualmode()) 62 | --------------------------------------------------------------------------------