views:

640

answers:

6

By making use of the remote feature in vim, is it possible to reuse an instance of vim to load up multiple files as needed.

It will be nice to have that facility from within the same terminal instance.

I am more keen to have a tab based interface, which available in vim 7+ The scenario should be

  1. Open terminal
  2. vim file1.cpp
  3. Edit - Save - Ctrl+Z to get to prompt
  4. open another file
    • vim file2.cpp
  5. Now we have file1.cpp and file2.cpp open in the same editor

Is that possible?!

+3  A: 

You can split the current screen and open two (or more) files in the following way:

For a horizontal split, do:

:sp 'filename'

and for a vertical split, do:

:vsp 'filename'

To tab between the two, hit ctrl+w, then use an arrow key to navigate to whichever file you want to edit.

Also, if you want to just switch files (and only have one open), you can do this:

:e 'filename'
samoz
Jack M.s answer certainly is correct, but this is a much simpler solution.
Andrew Sledge
+1  A: 

You can set the hidden feature on vim, to allow it to have multiple files open:

:set hidden

Then you can open as many files as you want, without bothering to save them every time you want to switch a "buffer":

:e 'filename'

You have several commands to navigate the buffers:

:bnext
:bprev
:buffers

Of course, as mentioned by samoz, you can split your screen to see multiple buffers on the same window.

I'd suggest to read this for a good introduction about vim, it will save you a lot of time.

Good luck!

Igor
+1  A: 

G'day,

Or if you want to have multiple files but use the whole vim window for one file at a time you can just enter

:e 'filename'

to open the new file. You can do this multiple times. To see what you've currently got open, enter

:ls

To bounce between the files you've got open you can use cntl-^ (shift-cnt-6) and that will alternate between the main and secondary files (shown with a % and # in the file list)

Or you can enter

:n b

where n is the number at the beginning of the file you want in the list shown by the 'ls'command.

HTH

cheers,

Rob Wells
+3  A: 

Hi CodeMedic.

I'm not sure if this can be done in exactly the manner that you're specifying, but something very similar can probably be done using a vim server running on your local machine.

Look into the :help remote.txt in Vim.

If your version of vim was compiled with +clientserver you can use vim to create a vim server, and then execute commands on it, e.g. opening another file.
The --servername switch can create a new server, and the --remote switch can send additional commands or files to it.

e.g.

vim --servername test file1.txt
vim --servername test --remote file2.txt

I've had a look, and the vim I'm using as standard on xubuntu on one of my computers doesn't have it, but there are some instructions here that may help if yours has it compiled. If it isn't, installing gvim and symlinking is apparently an option (as gvim has it included by default), or compiling the binaries from source.

Edit:
I've had more of a play with gvim and this doesn't look possible to do this within the terminal. Control-Z suspends the job at the process level. I thought it might work with screen, but no communication seems to take place unless gvim has launched in a graphical window,

Andy
In response to your edit, does it work if, after C-Z, you bg the process so that it is not suspended?
Robert Gowland
+2  A: 

I have a couple suggestions for you, though neither is exactly what you're talking about. The first is NERD Tree, which gives you a nice tree-based file browser for opening other files. Very handy. I also set up a hot key (ctrl+o) to open NERD Tree for me. I keep an alias of whatever project I'm on in ~/curr/trunk, so this always works for me:

map <C-O> :NERDTreeToggle ~/curr/trunk/<CR>

The other thing that I would suggest is to do away with ctrl+z. It's somewhat clunky, and everyone I know who uses that method tends to get lost and end up with 3 or 4 vim's running in the background. Take a look at how to open a shell for yourself. I use a map for ;s to execute a shell script:

map ;s :!~/.vim/shell.sh<CR>

Which executes:

#!/bin/sh
/usr/local/bin/bash -l;

I also have a bit of magic in my .profile for making it obvious I'm in VIM:

if [ "$VIMRUNTIME" != "" ] ; then
    export PS1="\u@\h \W \t$ vim> "
fi

</2 cents>

Jack M.
Brilliant! This one works much better than what I was after.I did make some scripts that will emulate what I wanted, but found it aweful in certain scenario. I couldnt debug it very well either.Taking a step back and thinking in JachM's shoes, helped me with a better solution.! Cheers Jack!
CodeMedic
I added in a hot key which I use to toggle NERD Tree. Might be useful.
Jack M.
+2  A: 

This is easy to do if you compiled VIM with +clientserver, as Andy suggested in a previous answer.

I did the following:

I started up VIM as a server:

vim --servername abc

I suspended it with CTRL+Z and did:

vim --servername abc --remote ~/.cshrc
fg

Now VIM had ~/.cshrc open.

Then I did:

vim --servername abc --remote ~/.aliases
fg

Now VIM had one buffer with ~/.cshrc and another with ~/.aliases.

Then I did:

vim --servername abc --remote-tab ~/foo_bar
fg

And now I VIM had one tab with the two previous buffers open and another tab with ~/foo_bar open.

In call cases VIM was running in the terminal, not as a GUI.

Nathan Fellman