Creating directories and files with a single command

From Vifm Wiki
Jump to navigation Jump to search

Use this command to create directories when the last character entered is "/", and create files otherwise:

command create :
\|  let $last_char = expand(system("str=\"%a\"; echo \"${str: -1}\""))
\|  if $last_char == "/"
\|    mkdir "%a"
\|  else
\|    touch "%a"
\|  endif

nnoremap a :create<space>


To create a file named test_file:

:create test_file

To create a directory named test_dir:

:create test_dir/

Variant #2[edit]

This variant will allow for relatively consistent file/dir/sym/hard creation alà jarun/nnn. This hasn't been sanitized so ymmv.

Add the following to your .vifmrc

" create file
\| command createFile :
\|   let $creature = input('filename: ')
\|   if $creature != ''
\|     execute 'touch' fnameescape($creature)
\|   else
\|     echo 'file creation failed'
\|   end

" create directory
\| command createDir :
\|   let $creature = input('directory: ')
\|   if $creature != ''
\|     execute 'mkdir' fnameescape($creature)
\|   else
\|     echo 'directory creation failed'
\|   end

" create symlink
command createSymlink :
\|   let $creature = input('symlink path: ')
\|   if $creature != ''
\|     execute '!ln -s %c:p' fnameescape($creature)
\|   else
\|     echo 'symlink creation failed'
\|   end

" create hardlink
\| command createHardlink :
\|   let $creature = input('hardlink path: ')
\|   if $creature != ''
\|     execute '!ln %c:p' fnameescape($creature)
\|   else
\|     echo 'hardlink creation failed'
\|   end

Adjust the mappings to your preferences

nnoremap <wait> of :createFile<cr>
nnoremap <wait> od :createDir<cr>
nnoremap <wait> os :createSymlink<cr>
nnoremap <wait> oh :createHardlink<cr>

Variant #3[edit]

See also Bulk file/directory creation.