Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a directory in vimscript

Tags:

vim

I want to create a directory to store Vim's swap files in my .vimrc. I've found a few ways to do it, e.g.:

call mkdir($HOME . "/.vim/swap", "p", 0700)
silent! call system('install -dm 700 ~/.vim/swap')

Which way is better and why?

like image 600
planetp Avatar asked Mar 02 '18 19:03

planetp


People also ask

How do you create a directory in Linux?

With the help of mkdir command, you can create a new directory wherever you want in your system. Just type "mkdir <dir name> , in place of <dir name> type the name of new directory, you want to create and then press enter. Example: mkdir created.


1 Answers

The former:

call mkdir($HOME . "/.vim/swap", "p", 0700)

Is better than the latter:

call system('install -dm 700 ~/.vim/swap')

Some of the reasons why the built-in mkdir() is better:

  • More Efficient: Calling the built-in mkdir() function from Vim will have Vim simply create the directory directly by using mkdir() system calls. That is pretty straightforward and inexpensive. Calling system(), on the other hand, means Vim will have to spawn a separate process, in most cases a shell, which will then have to interpret the command-line, then exec() the external binary (in this case, install) so that it can make the directories for you.

  • More Portable: The Vim built-in mkdir() will be available on systems that are not Unix/Linux or Unix-like, in particular, Windows. (In this particular case, you might have trouble with Windows due to the .vim name on your directory, but the point stands, the Vim built-in function will work on non-Unix/Linux platforms.) Another point related to portability is that the latter depends on an install binary being available, which might not be the case on a bare-bones system. Also, some systems have binaries such as install which take different non-standard command-line options and might not recognize the ones you have used. It is quite difficult to test for that, given the variety of different platforms on which Vim can run.

  • Safer: Running external shell commands requires proper escaping of arguments, which can be hard to get right since the normal case (no spaces or special meta-characters) often works without special precautions. That is not an issue with the mkdir() built-in, which takes the directory name as a single argument, and won't have trouble with spaces or meta-characters.

  • Better Error Handling: In your example you already used silent! for the case spawning an external command (presumably to skip any "Press Enter" prompts while running the command)... But that means if something goes wrong (for example, permissions are set in a way that prevent the directory from being created), you won't see an error message letting you know of that. On the other hand, if the built-in mkdir() fails, you should have a clear error message indicating what happened and why.

like image 139
filbranden Avatar answered Sep 17 '22 16:09

filbranden