From bdc0101b9a9a6530ef5b7fb8f11338b0541d4b09 Mon Sep 17 00:00:00 2001 From: Shidong Wang Date: Tue, 21 Sep 2021 22:04:26 +0800 Subject: [PATCH] feat(core): Add `SPC f a` key binding to save as new file Problem: There is no key binding to save current buffer as new file. Solution: Add `SPC f a` key binding close https://github.com/SpaceVim/SpaceVim/issues/4310 --- autoload/SpaceVim/layers/core.vim | 31 +++++++++++++++++++++++++++++++ docs/cn/documentation.md | 1 + docs/documentation.md | 1 + 3 files changed, 33 insertions(+) diff --git a/autoload/SpaceVim/layers/core.vim b/autoload/SpaceVim/layers/core.vim index 0eb516c08..c09c3a103 100644 --- a/autoload/SpaceVim/layers/core.vim +++ b/autoload/SpaceVim/layers/core.vim @@ -149,6 +149,9 @@ function! SpaceVim#layers#core#config() abort call SpaceVim#mapping#space#def('nnoremap', ['f', 's'], 'call call(' \ . string(s:_function('s:save_current_file')) . ', [])', \ 'save-current-file', 1) + call SpaceVim#mapping#space#def('nnoremap', ['f', 'a'], 'call call(' + \ . string(s:_function('s:save_as_new_file')) . ', [])', + \ 'save-as-new-file', 1) call SpaceVim#mapping#space#def('nnoremap', ['f', 'S'], 'wall', 'save-all-files', 1) " help mappings call SpaceVim#mapping#space#def('nnoremap', ['h', 'I'], 'call SpaceVim#issue#report()', 'report-issue-or-bug', 1) @@ -936,6 +939,34 @@ function! s:save_current_file() abort endif endfunction +function! s:save_as_new_file() abort + let current_fname = bufname() + if !empty(current_fname) + let dir = fnamemodify(current_fname, ':h') . s:FILE.separator + else + let dir = getcwd() . s:FILE.separator + endif + let input = input('save as: ', dir, 'file') + " clear cmdline + noautocmd normal! : + if !empty(input) + exe 'silent! write ' . input + exe 'e ' . input + if v:errmsg !=# '' + echohl ErrorMsg + echo v:errmsg + echohl None + else + echohl Delimiter + echo fnamemodify(bufname(), ':.:gs?[\\/]?/?') . ' written' + echohl None + endif + else + echo 'canceled!' + endif + +endfunction + let g:_spacevim_autoclose_filetree = 1 function! s:explore_current_dir(cur) abort if g:spacevim_filemanager ==# 'vimfiler' diff --git a/docs/cn/documentation.md b/docs/cn/documentation.md index 3f1198c1b..01673246c 100644 --- a/docs/cn/documentation.md +++ b/docs/cn/documentation.md @@ -1334,6 +1334,7 @@ SpaceVim 选项 `window_leader` 的值来设为其它按键: | `SPC f o` | 代开文件树,并定位到当前文件 | | `SPC f R` | rename the current file(TODO) | | `SPC f s` / `Ctrl-s` | 保存文件 (:w) | +| `SPC f a` | 另存为新的文件 | | `SPC f W` | 使用管理员模式保存 | | `SPC f S` | 保存所有文件 | | `SPC f r` | 打开文件历史 | diff --git a/docs/documentation.md b/docs/documentation.md index de918473f..149af4887 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -1424,6 +1424,7 @@ Files manipulation commands (start with `f`): | `SPC f o` | Find current file in file tree | | `SPC f R` | rename the current file(TODO) | | `SPC f s` | save a file | +| `SPC f a` | save as new file name | | `SPC f S` | save all files | | `SPC f r` | open a recent file | | `SPC f t` | toggle file tree side bar |