templatesvifmpcmanfm

How can I add a new file/dir from a template in vifm?


In pcmanfm you can use your ~/Templates folder, to create new files using them. it's so easy then to make a template file and use it many times, now VIFM as a great file manager, how it can do this?


Solution

  • The best thing about vifm is the sheer amount of things which can be achieved using configuration language. So while there seems to be no built-in means to handle user templates, it's possible to program the behavior you describe with two lines in vifmrc configuration:

    " add command named 'newt' ('new from template') - or any other command name
    command newt !ls ~/Templates %m
    
    " map 'C' key in menu mode to paste selected menu entry as command input
    mnoremap C c"<c-a><c-f>cp -- ~/Templates/"<c-e> .
    

    Execution of newly added :newt command will open the list of templates available in your ~/Templates directory as custom menu. Template entry can be selected using regular navigation (j, k, g, / etc.), and pressing C on selected entry will open interactive input allowing to enter the name of new file (default is . which will use original template name, placing it in current directory).

    A bit of explanation on what these options do

    command newt !ls ~/Templates %m
    

    First option is pretty straightforward — it adds the newt command which calls ls command in ~/Templates directory with %m flag - the output of the command will be piped to create the custom interactive menu.

    mnoremap C c"<c-a><c-f>cp -- ~/Templates/"<c-e> .
    

    Second option (mnoremap) is a bit more tricky: it adds a keymap to C key (any different key can be assigned here) which expands into the following sequence:

    The keymap (C) leaves interactive input with cp command open, so instead of . is's possible to enter a different name for a file created from the selected template.