Setting up the Text Editor in My Computing Ecosystem

Wildhorsesowyhee

Image from WikiMedia Commons

Basic Setup of Shell to Support My Text Editor Preferences

By "text editor", I mean Vim, of course. There are pseudo-operating systems that include rudimentary text-editing capabilities (e.g. Emacs), and integrated development environments that allow for editing of text, but there really is only one text editor that deserves the title of "text editor": Vim, that magical mind-reading mustang that carries out textual mogrifications with surgical precision and zen-like elegance.

However, while Vim is the only true text editor, there is more than one flavor of Vim. There is the original, universal one, i.e., console Vim. And then there are various lines of graphical Vim's specific to various operating systems. On my primary development box, a Mac, I prefer MacVim over straight-up console Vim, for many, many, many reasons, even though I practically live in the shell otherwise. For working with files on remote machines, I prefer using SSH to login and editing with console Vim, even though I can also edit these files through my own local MacVim instance. My computing ecosystem is shared between all my accounts, and this includes a collection of scripts as well as resources for various applications (e.g., not only my entire '~/.vimrc', but the entire accompanying '~/.vim' directory).

I have the following line in my '~/.bashrc':

if [[ "$HOSTNAME" = $PRIMARYHOST ]]
then
    export EDITOR='default-edittool.sh -f'
else
    export EDITOR='default-edittool.sh'
fi
alias e='default-edittool.sh'

This invokes the following script, 'default-edittool.sh' which is in the '$PATH' of all my accounts:

#! /bin/bash

if [[ -f /Applications/MacVim.app/Contents/MacOS/Vim ]]
then
    # bypass mvim for speed
    VIMPATH='/Applications/MacVim.app/Contents/MacOS/Vim -g'
elif [[ -f /usr/local/bin/mvim ]]
then
    # fall back to mvim
    VIMPATH='mvim'
else
    # fall back to original vim
    VIMPATH='vim'
fi

$VIMPATH "$@"

Easy Opening of Multiple Project Files

I have FuzzySnake installed on my system. My totally unbiased opinion as the author of FuzzySnake is that it is absolutely awesome, and that you should all be using it as well. In addition to the default behavior of opening the selected targets in my '$EDITOR' (which is, as shown above, 'default-edittool.sh'), I also use the nifty '--L/--list-and-exit' facility of FuzzySnake to discover files of particular types and pass them to Vim. I could define a set of aliases like 'alias epy="e $(fz -L --python)"', but this approach makes it impossible to pass options to the editor. Instead, I have a set of convenience functions like:

# open all C/C++ and header files
function ecc() {
    e $(fz -L --cpp --exclude-file-patterns="config.h" "$@")
}
# open all C/C++ and header files, as well as autotools files
function ecca() {
    e $(fz -L --cpp --autotools --exclude-file-patterns="config.h" "$@")
}
function ecm() {
    e $(fz --cmake "$@")
}
function eccm() {
    e $(fz -L --cpp --cmake --exclude-file-patterns="config.h" "$@")
}
# open all Python files
function epy() {
    e $(fz -L --py "$@")
}

Of course, for on-the-fly use-cases, I usually just do something like:

$ e $(fz -L --rst)

Allowing for Opening of Files in an Existing MacVim Instance

In MacVim's Preferences, I make sure that 'Open files from applications' is set to 'in the current window'. Then I define the following alias in my '~/.bashrc':

if [[ "$HOSTNAME" = $PRIMARYHOST ]]
then
    # alias ee="open \"mvim://open?url=file://$1\""
    alias ee="open -a MacVim"
fi

With this, while typing

$ e foo bar

will open a new Vim session (either MacVim or console vim, depending on whether I am editing a file on my local machine or remotely through SSH), with files 'foo' and 'bar' loaded into buffers, typing

$ ee foo bar

will open 'foo' and 'bar' in an existing MacVim session.

Opening Remote Files in My Local (Mac)Vim Session, with Auto-Completion of Paths/Filenames

Sometimes I do prefer to edit remote files in my desktop MacVim (one might wonder why I do not always prefer this ...). Lack of path completion when invoking the command has alway been an irritation for me, until I defined the following function:

function edit-remote() {
    if [[ -z $1 ]]
    then
        echo "usage: remote-edit [[user@]host1:]file1 ... [[user@]host2:]file2"
        return
    fi
    declare -a targs=()
    for iarg in $@
    do
        targ="scp://$(echo $iarg | sed -e 's@:/@//@' | sed -e 's@:@/@')"
        targs=("${targs[@]}" $targ)
    done
    $EDITOR ${targs[@]}
}
complete -F _scp -o nospace remote-edit

Leave a Reply

Your email address will not be published. Required fields are marked *