Skip to content

Instantly share code, notes, and snippets.

@ivanperez-keera
Created September 17, 2017 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanperez-keera/ff5f74317216fddc2172619d5c73dde2 to your computer and use it in GitHub Desktop.
Save ivanperez-keera/ff5f74317216fddc2172619d5c73dde2 to your computer and use it in GitHub Desktop.
A wrapper around vim to open files in existing vim sessions using ConqueTerm
#!/bin/bash
# matryoshka-vim
# ==============
#
# A wrapper around vim to open files in existing vim sessions using ConqueTerm.
#
# AUTHOR: Ivan Perez Dominguez
# LICENCE: GPL-3
#
# Conque is a wonderful vim plugin to open terminals inside vim buffers.
# However, if you call vim from inside ConqueTerm, what you get is vim running
# inside vim, which is almost certainly not what you want.
#
# What my program does is to detect if vi(m) is being called from inside vi(m)
# and, if so, it opens files in a new tab in the existing vim session.
#
# Most likely, I'll accept your pull requests if they fix something/help in any
# way. So, if you made a change and you think it can help others, please, send
# me a pull request on github.
#
# About Conque:
# http://code.google.com/p/conque
#
# Even if you don't want to use my script, I'm sure you'll want to use ConqueTerm
# if you use vim.
#
# Installation
# ============
#
# All you need to do is copy the file vi from the repository to a path in your
# system, and add it to the front of your $PATH variable definition.
#
# If you do not know what that means, I suggest you do the following:
#
# * Step one: Downloading
#
# ```
# $ cd
# $ mkdir .bin
# $ cd .bin
# $ wget https://raw.github.com/ivanperez-keera/matryoshka-vim/master/vi
# ```
#
# * Step two: Adding this new vi to your PATH
#
# ```
# $ echo 'export PATH=$HOME/.bin/:$PATH' >> ~/.bashrc
# ```
#
# * Step three: Reload your session.
#
# You can either close your terminal and open a new one, or simply run:
#
# ```
# $ . ~/.bashrc
# ```
#
# * Step four: Close all your vim sessions.
#
# From that point on, use the command 'vi' to open ConqueTerm sessions from
# inside (g)vi(m). Note that you also need to use that command to open your first
# vim session. Feel free to rename it gvim/vim/whatever if that helps you.
#
# Sample usage
# ============
#
# * Open a terminal and execute vi
# * Execute :ConqueTerm bash
# * Go to a different folder with cd
# * Open an existing file there with: vi <filename>
#
# Get PID of the caller's parent
L="\$2 ~ /\<$PPID\>/ { print \$3; }"
P=$(ps -ef | awk --source "$L")
L2="\$2 ~ /\<$P\>/ { print \$8; }"
P2=$(ps -ef | awk --source "$L2")
# You should be able to inspect those here if something doesn't work as expected
# echo $P # This is the PID
# echo $P2 # This is the parent's
# echo $L # This is the program's name
# echo $L2 # This is the parent's name
if [ "$P2" == "vi" -o "$P2" == "vim" -o "$P2" == "gvim" -o "$P2" == "/usr/bin/vi" -o "$P2" == "/usr/bin/vim" ]; then
/usr/bin/vi --remote-tab-silent "$@" ; # Open in tab if called from inside vim
else
/usr/bin/vi --servername VI "$@" ; # Open a server with name VI otherwise
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment