Created
February 6, 2024 22:49
-
-
Save eduardoarandah/fa77a67cda6327849f5a71a047bba8f1 to your computer and use it in GitHub Desktop.
Command to duplicate a filename and rename its class name
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
" Duplicate class | |
command! -nargs=1 DuplicateClass :call DuplicateClass(<f-args>) | |
function! DuplicateClass(new_class) | |
let l:original_class = expand('%:t:r') | |
let l:original_extension = expand('%:e') | |
let l:new_full_path = expand('%:h').'/'. a:new_class.'.'.l:original_extension | |
" Copy contents of original file to new file | |
execute 'silent execute "write ' .l:new_full_path.'"' | |
execute 'silent execute "edit ' .l:new_full_path.'"' | |
" Replace class name in new file | |
let l:new_class_escaped = escape(a:new_class, '\/.*$^~[]') | |
execute 'silent! %s/\vclass '.l:original_class.'/class ' . l:new_class_escaped . '/g' | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment