4

I currently use Vim for Java development. I use maven and for a small project I typically begin by running

vim . or vim pom.xml

from terminal at project root and

:argadd **/*.java

to add all the java files to the arguments list.

Then I edit away. It works very well, but there is some headache, like my statusline not being composed well for showing deeply nested filepaths, etc., but my question is specifically about editing a file in the same directory as the file I'm editing in the current buffer.

For example, say I am editing a Java interface src/main/java/com/fancy/restclient/Client.java. I am happy with the interface and want to create an implementation in the same folder. I type

:spl %:p:h/ClientImpl.java

to edit ClientImpl.java, in the same folder, in a horizontal split.

Is there a faster way to do this? I type this very slowly and it breaks my train of thought (choo choo!) Should I just suck it up and get used to it, or am I missing out on a more glorious editing experience? How would you edit a different file in the same directory as the current file?

  • When I want to edit a file in the same directory which already exists I usually have it in my args list and :spl | b Cli<Tab> is convenient. If it exists but is not in my args list I can use :find and friends to get at it quickly. So my question is primarily about editing new files.
  • I don't want to use autochdir because I have a compiler set up that expects the working directory to be the project root (and it also would make it more complicated to edit files in a different directory branch of the project).
3

:split file is a shortcut for :new followed by :edit file. Since file is already in your argument list, using a file-related command like :edit doesn't make much sense; an argument-related command or a buffer-related command like :sbuffer is a more logical command, here:

:sb %:p:h/ClientImpl.java

But this doesn't solve the "same directory" aspect of your problem. What you need is a way to insert the path to the directory of the current file in your command-line. Something like:

cnoremap :: <C-r>=expand('%:p:h')<CR>

which allows you to do:

:sb ::

and have it expanded like so:

:sb src/main/java/com/fancy/restclient

… leaving the rest up to you.

You could also do all of the above in a single mapping:

nnoremap <key> :sbuffer <C-r>=expand('%:p:h').'/*'<CR>

And if you want that for files that don't belong to your arglist:

nnoremap <key> :split <C-r>=expand('%:p:h').'/*'<CR>

Now, what exactly is your rationale for filling up the argument list with all the files in your project? Given the depth and breadth of all the Java project I've seen that means you end up with a potentially huge argument list and just as much navigation issues as with a mostly empty argument list.


FWIW, here are a few relevant snippets from my vimrc:

"""""""""""""""""""""""
" JUGGLING WITH FILES "
"""""""""""""""""""""""
nnoremap ,f :find *
nnoremap ,F :find <C-R>=expand('%:p:h').'/**/*'<CR>
nnoremap ,s :sfind *
nnoremap ,S :sfind <C-R>=expand('%:p:h').'/**/*'<CR>
nnoremap ,v :vert sfind *
nnoremap ,V :vert sfind <C-R>=expand('%:p:h').'/**/*'<CR>
nnoremap ,t :tabfind *
nnoremap ,T :tabfind <C-R>=expand('%:p:h').'/**/*'<CR>

"""""""""""""""""""""""""
" JUGGLING WITH BUFFERS "
"""""""""""""""""""""""""
nnoremap ,b :buffer *
nnoremap ,B :sbuffer *

As you can see, I'm a big fan of :find ;-)

  • Thanks for :sbuffer, that's the kind of thing I was hoping for–and I notice that it works with :vertical as well. I added your 'juggling files' snippet recently and have since stopped using "CmdtrolPT"; the 'buffers' ones are new to me. – jjaderberg Sep 11 '15 at 19:39
  • I started this workflow because of frequent :argdo operations, and for projects with less than 30 or so files it has been pretty convenient. Maybe once :find becomes a habit, or I work on bigger projects, I'll change the flow more. – jjaderberg Sep 11 '15 at 19:41
  • "CmdtrolPT"… I like it ;-) – romainl Sep 11 '15 at 19:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.