Developing in nodejs I have a lot of custom module require statements in my javascript source files, like this one:

// my nodejs program: 
// path/to/projecthome/lib/someModule.js
const EMOJI = require('../lib/emoji')
const {rand} = require('../lib/random')
const {search} = require('../lib/search')

In .vimrc, I have these settings (a TAB completion plugin and some set complete settings):

" ~/.vimrc
" TAB autocompletion
Plug 'ervandew/supertab'

set complete=.,w,b,u,t,i,k

I usually edit all files from project home (root) directory:

$ cd path/to/projecthome/
$ vim lib/someModule.js

When in inserting mode on above mentioned file, I would like to be assisted by an autocompletion of required filenames (possibly, with TAB completion, or with everything else). By example, inserting (during a file update) this line:

// path/to/projecthome/lib/someModule.js

                                     +---- cursor position, in insert mode
 const {search} = require('../lib/sea|')

hitting TAB after the word sea, I would like to be completed in existing file search.js ( with reference to: ../lib/search.js), but it's not working.

BTW, I know that is possible to have file name omni completion, using:


but also the omni completion seems to me not working in the above case (maybe because the relative pathnames?).

My question is: In general, TAB plugin a part, there is any way to have a file auto completion for (relative path) filenames? Maybe does exist a nodejs require completion plugin for that ?

I just discovered that with Ctrl-XCtrl-F mode, relative paths are autocompleted! They will use the directory that Vim was launched from (PWD) as the root. So in the example above, autocompletion works if I put in vim command mode:

:cd lib
  • 1
    If the update you posted solved your problem, you can post that as an answer. I suspect that was confusing supertab, too, as I believe supertab just makes tab act like <C-x> variants
    – D. Ben Knoble
    Apr 20 '20 at 16:35
  • Thx. You are right about tab as C-x variant. Honestly I'm not satisfied with the "change working directory and C-x C-f" , so I'd wait other suggestions/reply Apr 20 '20 at 17:30
  • 2
    Yeah, <C-x><C-f> only completes relative paths from the current directory unfortunately... If you consider changing directories, then you can use :lcd for that to be valid for the current buffer only. There's also 'autochdir' which you can use to automatically change to a file's directory whenever you open it. Agree that this solution doesn't sound as satisfying as one that would work only within the completion system...
    – filbranden
    Apr 20 '20 at 22:30
  • The more i look at this, the more i think I’m bothered that the require expressions use ../lib—it doesnt make any sense from the root directory or from the lib directory, only from a subdirectory of the project thats not lib... absolute imports would have been a better design decision imo (but idk node well enough to say whats normal and doable etc)
    – D. Ben Knoble
    Apr 21 '20 at 13:10
  • that's another story and ... you are right :) Apr 21 '20 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.