0

Every coding configuration need I have for Vim works just fine if I put it all in the .vimrc file, but I'm curious: let's just say that I wanted to make my own scripts or plugins, and my .vimrc file was becoming too cluttered.

According to google, the format for plugins is ~/. vim/pack/vendor/start/plugin_name, and that's exactly what my tpope surround plugin uses, but I'm wondering how somebody would have a separate folder for their vim scripts, especially considering that linux wouldn't let you have two pack directories in the same directory for good reason.

How else (other than the .vimrc file in your home folder) do Vim programmers get Vim to read external files that allow the user to have separate VimScripts?

2
  • 1
    I"m not sure I understand your question. I believe it would help me if you could illustrate it. Could you tell us which .vimrc setting you would like to depend on the script you are editing? Or do you only want to split your .vimrc into multiple files for the sake a readability? Commented Jul 8, 2023 at 15:19
  • @Vivian: it's a pretty straightforward question, I'm only trying to figure how you could put vim settings in separate files and folders. Nothing I have tried so far has worked except the .vimrc file.
    – user8919
    Commented Jul 8, 2023 at 15:24

3 Answers 3

3

For simple things, I would just create a single file per "project" and place them directly into ~/.vim/plugin/<file>.vim. Create the plugin folder if it does not exist. Vim will automatically source those. For even more sophisticated things, you can make use of autoloading, see (the unhelpful help tag) :h 52.3

That's how we did it before pack where a thing and when we did not have plugin manager plugins. Good old times :)

1
  • yeah it does appear that this method works, and from now on i will use a top-level "plugin" folder inside of .vim for all my custom scripts.
    – user8919
    Commented Jul 8, 2023 at 18:13
3

In ~/.vim/pack/vendor/start/plugin_name, the variable parts are: ~/.vim/pack/{vendor}/start/{plugin_name}, where:

  • vendor and plugin_name are totally arbitrary names,
  • the package name vendor can be anything that a) makes sense to you and b) doesn't break your filesystem,
  • plugin_name can also be anything but it kind of makes sense to use the plugin's name and not some silly crap.

Basically, Surround will work just as well if you put it here:

~/.vim/pack/vendor/start/surround

or there:

~/.vim/pack/LOL/start/badaboum

as will any competently written plugin.

Now, you can have more than one package under pack/. That's, like, the whole point of that scheme. It lets you keep your own stuff and third-party stuff separate, in packages:

~/.vim/pack/mine/…
~/.vim/pack/theirs/…

FWIW, this is what I currently have:

~/.vim/pack/bundle/    " for QOF plugins like surround
~/.vim/pack/lab/       " for my own experimental stuff
~/.vim/pack/lang/      " for language-specific plugins, custom and third-party alike

:help startup is a good entry point about this. See also :help usr_51 and :help package.

If the relative complexity of packages is too much, you can still use the old way, with ~/.vim/plugin/, ~/.vim/ftplugin/, etc. It still works and the new way, with packages, is really just the same, with an additional segregation layer that makes things cleaner.

This article covers the old way very well.

13
  • That's a great answer to the question for beginners, but i it's still not clear to me whether "plugin" and "pack". By default, tim pope's "pack" was installed with "pack" being located inside of the .vim directory. That works, but putting my scripts inside of his directory doesn't appear to work.
    – user8919
    Commented Jul 8, 2023 at 18:26
  • Why would you put your scripts inside of "his directory"? And what do you call "his directory" to begin with?
    – romainl
    Commented Jul 8, 2023 at 19:11
  • Uhm, I'm more or less concerned with how vim reads from "pack", is vim instructed by default to read from both both "pack" and "plugin" inside of the .vim directory?
    – user8919
    Commented Jul 8, 2023 at 19:21
  • Everything is explained in the provided help sections.
    – romainl
    Commented Jul 8, 2023 at 20:00
  • that's nice, but your answer doesn't answer my question. You've shown you've put some effort into this, but my concern has more to do with how vim itself reads your changes to the configuration, and how exactly plugin writers add their directories to runtimepath rather than the naming conventions. I tried adding my scripts to his directories to see if vim would recognize them there but clearly there's something more going on here that isn't being explained in a straightforward manner.
    – user8919
    Commented Jul 8, 2023 at 21:21
0

I would create an additional file and source it into the .vimrc.

._ ~
  |_ .vimrc
  |
  |_ vimfiles
    |_ settings.vim

E.g. create a ~/vimfiles/settings.vim and within ~/.vimrc add

source ~/vimfiles/settings.vim

If you want to execute a settings.vim next to your .vimrc wherever it is.

._ myfolder
  |_ .myvimrc
  |_ settings.vim

You could do:

execute 'source' expand('<sfile>:h') . '/settings.vim'
3
  • 1
    That's almost exactly the same as putting files ~/.vim/plugin, except that plugin files are loaded after a vimrc (and presumably in some kind of sorted order). Unless you need some very specific ordering, ~/.vim/plugin is much easier.
    – D. Ben Knoble
    Commented Jul 8, 2023 at 17:14
  • 1
    @Vivian, thank you for answering in a straightforward way, that's a lot better than cyberbullying strangers online.
    – user8919
    Commented Jul 9, 2023 at 18:41
  • 1
    We try to establish a warmful atmosphere around Vim but some of our best experts have a particular sense of humor that may spoil the cosyness feeling we try to create :-D Be sure we are a number to protect you from the misunderstanding ;-) Welcome to Vim :-) Commented Jul 9, 2023 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.