12

I am by no means a vim power user, I am just beginning to grasp the advanced motions and commands with vim. But there's one drawback that I can't seem to get around.

I grew up with graphical programs for writing code, but I've just recently started using vim over ssh & screen to work on fairly large projects with many folders.

I'm curious what the vim masters do to work with multiple files quickly and easily. Consider a rails app as an example. Say you want to edit a controller so you run.

vim app/controllers/controller.rb

But then you quickly want to change to editing a view or a db migration, my first instinct is to :wq, return to bash to navigate to that directory and start vim again in a brand new buffer.

This is obviously flat out wrong.

I've learned about several things like:

  • The clientserver (but I don't want to use X over ssh and this seems like the only way)
  • :e to open another file by browsing, but I can't seem to do it very quickly. Tab complete really trips me up.
  • Opening an entire directory and searching through the buffers.
  • NERDTree is promising, but I really want to stay away from any graphical representation to keep forcing me to master command line navigation

I apologize for being naive, but I really want to learn the correct way to go about it even if it's heavily subjective.

What are your methods, and what would you recommend?

1
  • For Ruby on Rails in particular, Tim Pope’s rails.vim plugin has some nice commands (with completion; extensible to new hierarchies) for opening files for models/views/controllers/migrations/tests/… (see rails-type-navigation in the documentation, or :help rails-type-navigation when you have it installed). Jun 11, 2011 at 7:03

5 Answers 5

7

It's possible to set up vim to edit multiple files without having to exit at all. You use multiple buffers and switch between them. Since you're using vim inside screen You can put the following in your ~/.vimrc to enable that.

   set hidden
   syntax enable
   source $VIMRUNTIME/menu.vim
   set wildmenu
   set cpo-=<
   set wcm=<C-Z>
   map <F4> :emenu <C-Z>
   nmap <Esc>[5D :bp<CR>
   nmap <Esc>Od  :bp<CR>
   nmap <Esc>OD  :bp<CR>
   nmap <Esc>[5C :bn<CR>
   nmap <Esc>Oc  :bn<CR>
   nmap <Esc>OC  :bn<CR>
   nmap <Esc>[3~ :bd<CR>
   nmap ZZ :bd<CR>

Those key mappings map to Ctrl-Left and Right arrow keys to switch between buffers. The F4 key invoke the same menu the GUI would show. Pressing tab also now displays a little menu for selecting the completion when your press tab.

1
  • Very good tip, these three answers really helped me feel more comfortable. Good buffer management without plugins is a great skill to have. Love the wildmenu!
    – febs
    Jun 12, 2011 at 1:08
6

Hah! I used to be exactly like you, so I know exactly where you're coming from. Even now that I know what I'm doing to a certain extent (I'm no vim god, but I hope to get there) I'll still occasionally do a :wq and get back to my shell.

Check this out - this might help you. (It'll help me, too, since I never knew about this. That's what's awesome about stackexchange sites - you learn by helping others learn!)

Here are some things that I do.

  1. The :e command. From inside vim, when editing a file, you can do something like :e dir/subdir/otherfile and it will open it in a new buffer, meaning you'll have two files open at once.

  2. Ctrl-Shift-6 (Don't know the actual ascii values). If you have two buffers open, you can switch between them in a flash with this toggle.

  3. bd - buffer delete. Lets you clear a buffer.

  4. This is a really really handy one - :sh. If you have to get back to a shell for any reason (say, to look at a directory and chmod a file), you can invoke vim's shell command do do just that. Exiting the shell takes you right back to where you were in vim.

  5. In the same category as the above - typing :! and a unix command afterwards will execute that command. Try this with ls. (I think it also works with shell builtins, but I'm not sure.)

Those are the ones I use all the time. There's also :n for next, and :p for previous file, since you can have more than two buffers open in vim at once (I don't usually do this, though).

3
  • Awesome thank you for those tips. I also found this: wincent.com/products/command-t . I'm siked to start using it. Only downside is that you need ruby (not so friendly with ssh stuff), but it's exactly what I was looking for. That combined with :sh, :e and proper buffer management, and things get sexy
    – febs
    Jun 11, 2011 at 3:59
  • In most shells (i.e. shells with job-control) Ctrl+Z should suspend the process (fg from the shell to display it again) - which is in alternative to :sh. Jun 11, 2011 at 6:35
  • Another great tip! Excellent! I'll have to compile these in a blog post or something.
    – febs
    Jun 12, 2011 at 1:11
2

That's where you start to use plugins. For file (or buffer) management NERD tree and MiniBufExplorer are common choices. In your case I would google "vim ruby ide" and follow one of the many tutorials that I find. I use Python and the 3 commands in this tutorial configure everything I need out of Vim.

2

I would also recommend using split windows to view multiple files at once. Add some skills with Buffer and Window managment within VIM, and you'll be a VIM ninja in no time.

For the actual documentation, I'll refer you to the VIM Windows Documentation, but I'll try to give you a head start here.

Start VIM with 2 (or more) horizontally split windows, the top window has file1, the next window down has file2, etc.

vim -o <file1> <file2> (file3 file4 ...)

Start VIM with 2 (or more) vertically split windows, the left most window has file1, the next window over has file2, etc.

vim -O <file1> <file2> (file3 file4 ...)

Move cursor to a different window:

CTRL-W <vim direction key> (h=left, j=up, k=down, l=right)

Already editing one file, but want to split the window and edit a different file also:

:split <file2>
:vsplit <file2>

For Horizontal and Vertical split respectively.

There are also commands to move windows around, resize them, etc.

1

Use the vi :split command to open a second edit window. split can do both horizontal and vertical splits and more than two at a time. I have had as many as 10 files open in the same vi session.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .