vim new file

This commit is contained in:
2026-09-21 19:25:52 +03:00
parent e1f9a3b08b
commit a1a711465d
4 changed files with 165 additions and 4 deletions
+90 -4
View File
@@ -22,6 +22,7 @@ syntax enable
filetype plugin indent on
set number
set relativenumber
set cursorline
set ruler
set showcmd
@@ -195,7 +196,7 @@ function! ShowKeymapHelp()
setlocal noswapfile
setlocal nobuflisted
setlocal nomodified
setlocal nowrap
setlocal wrap
setlocal nonumber
setlocal norelativenumber
setlocal nocursorline
@@ -204,20 +205,18 @@ function! ShowKeymapHelp()
\ 'VIM KEYMAPS',
\ '===========',
\ '',
\ 'Leader key: Space',
\ '',
\ 'FILES AND BROWSING',
\ ' Space e Toggle file browser',
\ ' Space f Find project file',
\ ' Space o Recently opened files',
\ ' Space b List open buffers',
\ ' Space a Create new parent directories and files',
\ '',
\ 'Inside the file browser:',
\ ' Enter Open file or directory',
\ ' t Open file in new tab',
\ ' - Parent directory',
\ ' % Create file',
\ ' d Create directory',
\ ' R Rename file',
\ ' D Delete file',
\ '',
@@ -284,3 +283,90 @@ augroup keymap_help_startup
\ call ShowKeymapHelp() |
\ endif
augroup END
" ---------------------------------------------------------------------------
" Space+a inside filemanager create parent directories and files
" ---------------------------------------------------------------------------
function! NetrwAddPath()
let l:base = b:netrw_curdir
let l:name = input('New path (end with / for directory): ')
if empty(l:name)
return
endif
let l:path = simplify(l:base . '/' . l:name)
" A trailing slash means: create directories only.
if l:name =~# '/$'
try
call mkdir(l:path, 'p')
silent execute 'keepalt edit! ' . fnameescape(l:base)
"silent execute "normal \<C-L>"
echo 'Created directory: ' . l:path
catch
echohl ErrorMsg
echomsg 'Cannot create directory: ' . v:exception
echohl None
endtry
return
endif
" Create all missing parent directories.
let l:parent = fnamemodify(l:path, ':h')
try
call mkdir(l:parent, 'p')
" Create only when the file does not already exist.
if getftype(l:path) ==# ''
call writefile([], l:path, 'b')
elseif isdirectory(l:path)
throw 'Path is a directory: ' . l:path
endif
catch
echohl ErrorMsg
echomsg 'Cannot create file: ' . v:exception
echohl None
return
endtry
" Refresh the Netrw file listing.
"silent execute "normal \<C-L>"
silent execute 'keepalt edit! ' . fnameescape(l:base)
" Find the main editing window.
let l:target_window = winnr('#')
" Find the main editing window.
let l:target_window = winnr('#')
if l:target_window == 0
\ || getbufvar(winbufnr(l:target_window), '&filetype') ==# 'netrw'
let l:target_window = 0
for l:window in range(1, winnr('$'))
if getbufvar(winbufnr(l:window), '&filetype') !=# 'netrw'
let l:target_window = l:window
break
endif
endfor
endif
" Open the new file in the main editing area.
if l:target_window > 0
execute l:target_window . 'wincmd w'
endif
execute 'edit ' . fnameescape(l:path)
endfunction
augroup netrw_custom_mappings
autocmd!
autocmd FileType netrw nnoremap <buffer> <leader>a :call NetrwAddPath()<CR>
augroup END