sponsor Vim development Vim logo Vim Book Ad

basic Tip #809: GNU\Linux - gvim as an external editor

 tip karma   Rating 4/1, Viewed by 2133 

created:   October 24, 2004 17:12      complexity:   basic
author:   Ben Collerson ( benc [at] bur [dot] st )      as of Vim:   5.7

The script, external-edit, provides the windows only functionality of vimtip#805 to GNU/Linux (or any *nix) systems with X Windows.

This script does not rely on clipbrd.vim script -- it uses a temporary file to pass the clipboard to gvim. This tip is actually not gvim specific but can be configured to use any external editor.

This script is still under development. New versions will be available from http://bur.st/~benc/external-edit.html.

----------external-edit----------
#!/bin/sh
##################################################
#
# external-edit: launches an external editor from within an X11 Application.
#
# Author: Ben Collerson { benc [at] bur [dot] st }
# Copyright: 2004 Ben Collerson
# Lastchange: 2004 Oct 24
# License: GPL version 2.0 or later (http://www.gnu.org/copyleft/gpl.html)
#
# Install:
# =======
# NOTE: These installation instructions are targeted towards a Debian
# system. To install on a non-Debian system you will need to reinterpret
# these instructions.
#
# First you will need to install the required packages some of which
# are part of Debian so become root and do the following:
#
#   apt-get install xbase-clients xautomation
#
# You will also need to install wmctrl. This is not currently a Debian
# Package. The source is available from http://sweb.cz/tripie/utils/wmctrl/
#
# Wmctrl is a \"command line tool to interact with an EWMH/NetWM
# compatible X Window Manager\". This means that this tip will not work
# if you are using a particularly old window manager, however most modern
# WMs should be okay -- including the default Gnome and KDE Window Managers.
#
# More info is available at the wmctrl link provided above.
#
# Once you have the source tarball for wmctrl extract it and do a
#
#   ./configure && make && su -c make install
#
# Once wmctrl is installed your system put this script somewhere handy
# such as ~/bin/external-edit and chmod +x ~/bin/external-edit
#
# Usage:
# =====
# using your favourite keygrabber/window manager bind a key combination
# (eg: Ctrl-Alt-V) to a command like the following:
#
#   external-edit /usr/bin/gvim -f
#
# For my fluxbox/bbkeys configuration I have the following in my .bbkeysrc:
#
#   KeyToGrab(V), WithModifier(Mod1+Control), WithAction(ExecCommand),
#                 DoThis(/home/ben/bin/external-edit gvim -f)
#
# Note: the editor you specify must be executed so it does not fork
# (ie. gvim -f). When executed the editor must allow the script to wait
# until the editor has finished executing.
#
###############################################################3


# editor plus arguments (except filename) passed on command line
editor=$*

# this gets the window id of the window with focus
winid=`xdpyinfo | sed -ne 's/^focus:.*\\(0x[^,]\\+\\).*/\\1/p'`
# and this gets window title
wintitle=`xwininfo -id $winid | sed -ne 's/xwininfo: .*\"\\([^\"]\\+\\)\"/\\1/p'`

mytemp=`mktemp`

# copy text using application keyboard short-cuts
case $wintitle in
  *Mozilla*)
  xte << EOM
sleep 1
keydown Alt_L
key a
keyup Alt_L
keydown Control_L
key c
sleep 1
key Home
keyup Control_L
EOM
  ;;
  *)
  xte << EOM
sleep 1
keydown Control_L
key a
key c
sleep 1
key Home
keyup Control_L
EOM
esac

xsel -b -o > $mytemp
# xclip could also be used eg:
# xclip -selection clipboard -o > $mytemp

# invoke the editor, wait for it to finish
#/usr/bin/gvim -f $mytemp
$editor $mytemp

# applications should be using the clipboard to paste!
xsel -b -i < $mytemp

# some applications have incorrect behaviour and use the primary
# selection to paste
xsel -p -i < $mytemp

# activates the original window
wmctrl -ia $winid

# paste text using standard keyboard short-cuts
case $wintitle in
  *Mozilla*)
  xte << EOM
keydown Alt_L
key a
keyup Alt_L
keydown Control_L
key v
sleep 1
key Home
keyup Control_L
EOM
  ;;
  *)
  xte << EOM
keydown Control_L
key a
key v
sleep 1
key Home
keyup Control_L
EOM
esac

# cleanup temporary file
rm $mytemp

# vim:ft=sh:sw=2
----------external-edit----------

 rate this tip  Life Changing Helpful Unfulfilling 

<<how to overwrite a visual-block of text with another such block | Highlighting Text Beyond 80 Columns >>

Additional Notes

benc [at] bur [dot] st, October 24, 2004 17:14
This tip should not be marked as "basic", it is an "advanced" tip - sorry.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo