Thursday, October 09, 2008

Open new files as tabs in current Vim Console

When programming I like to have a single Vim editor open with all my files as tabs. Until now I was using the ":tabnew" command to open files in the current Vim window as I knew no other way.

Some research led me to the --remote-tab switch that allows to open files as tabs in currently open Vim processes but it seemed to work only with the graphical interface (gvim) and not with the console (vim). But as I made some tests I found this can work with the vim in console mode and more over can be nicely integrated with my favorite file managers (Konqueror/Dolphin).

All I needed to do was configure my bash so it will always start vim in server mode if it is not already and to always use the --remote-tab switch when opening files. To do this I simply added these functions to my ~/.bashrc file:


function v {

   if [ "$#" -eq "0" ]; then

      /usr/bin/vim --servername VIMLOCAL

   else

      if echo "$*" | grep -q -- "--servername" ; then

        # echo "Command already has --servername use it"

        /usr/bin/vim $*

      else

          #echo "Use default VIM server VIMLOCAL"

          /usr/bin/vim --servername VIMLOCAL --remote-tab-silent $*

      fi

   fi

}



function gv {

   if [ "$#" -eq "0" ]; then

      /usr/bin/gvim --servername GVIMLOCAL

   else

      if echo "$*" | grep -q -- "--servername" ; then

        # echo "Command already has --servername use it"

        /usr/bin/gvim $*

      else

          #echo "Use default VIM server VIMLOCAL"

          /usr/bin/gvim --servername GVIMLOCAL --remote-tab-silent $*

      fi

   fi

}



alias vi=v

alias vim=v

alias gvim=gv





With these functions loaded (i.e. source ~/.bashrc) the first time you execute vim/gvim it will start a new process in server mode.


# this command will open vim in the current console
# and display first_file.txt for editing.

vim first_file.txt


The second and all subsequent times you execute vim/gvim the files you set as parameters will be opened as tabs in the already opened vim/gvim instance.


# This command in other console will open the files
# second_file.txt and third_file.txt as tabs in the
# other console with first_file.txt open

vim second_file.txt third_file.txt


The above examples work also with gvim instead of vim. The only drawback is that now it is difficult to open files in a separate console. The best solution I found is to call vim with a different server name:


# This command will create a new instance of vim in the current console window
# and display the other_file.txt file for editing.

vim --servername OTHERNAME other_file.txt


If you prefer not to use tabs and like files to be opened as buffers inside a current running Vim instance then simply replace "--remote-tab-silent" with "--remote-silent" in the above scripts.

4 comments:

  1. This got me quite excited.. :)

    Until I found "servername" is not understood.

    VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Aug 10 2008 10:52:25)
    Unknown option argument: "--servername"
    More info with: "vim -h"

    What version are you using? This is on Cygwin with XP.

    Rob
    :)

    ReplyDelete
  2. This works great with the default vim version 7.1.314 that comes with Kubuntu Ibex (8.10) and Kubuntu Hardy (8.04).

    Sorry I never tried it in Windows, I do all my coding in Linux.

    ReplyDelete
  3. It's ok - I worked it out.

    1) Cygwin's default vim doesn't support server mode because vim is explicitly built without X support, which is required for the clientserver option. I would have to re-compile source to enable this.

    Instead of doing this, I found my own work-around for command line vim: I will use my own search scripts to search for files and open them in Vim.

    2) For Gvim, I your solution works nicely - even better when I use it
    with It's All Text Plugin for Firefox!

    All in all I would say this post has been very helpful indeed. It just
    took me a bit more effort to figure it out for Windows. :)

    Thanks mate!

    Rob
    :)

    ReplyDelete
  4. really cool tips. dont know we could open files in already opened vi.
    very usefull!

    ReplyDelete