How to programmatically open several files in a particular configuration

I’m working in an environment where I have several branches that I’m working on, and have the need to switch back and forth between branches regularly. To avoid updating things in the wrong branch (burned once too many times), I now shut down vim anytime I’m switching between branches and that means that I have to re-open the 2+ files that I was working on (I use tmux and vim) for the new branch.

I’d like a way to automate this for each different branch, so that sounds to me like maybe a special file in each branch with the commands for vim… or? Any ideas on how to do something like this?

BTW, I tried git-new-workdir which is cool (keep all the branches open simultaneously), but since we use submodules here, I haven’t figured that out yet.


Update: The -On option tells vim to open windows side-by-side (-on = stacked; -pn = tabbed). So if I have two files I want to open, then I just say:

  vim -O2 filea fileb

and I’m off to the races.

Now I’ll just add a little shell script in each review branch and I get the basics easily.

Update-2: There’s even more; you can use the -c (command) option when invoking vim and tell vim exactly where you want things displayed. I have 5 files that I’m regularly opening from the command line when I switch branches, so I now have something like this:

vim -O2 <file-1> <file-2> -c "sp <file-3>" -c "sp <file-4>" -c "wincmd 2" -c "sp <file-5>"

I wind up with two files stacked vertically on the left (file-1 and file-5) and three files stacked vertically on the right (files 2, 3, and 4). Just another way to save time and keystrokes with vim!

Do you just need to reload the open files from disk?

bufdo e! would probably get you by if so.

If you’re saying you want to open different files automatically when you change branches then nothing particularly obvious comes to mind.

I’m not sure if this helps or not, but I have a kind of cool command that I run if I end up quitting vim (usually accidentally) before I’ve committed or finished work on what I was doing to load up more or less where I was. It’s not as good as something like, but it’s good in a pinch.

vim `git status --porcelain | sed -ne 's/^ M //p' -ne 's/^?? //p' | tr '\n' ' '`

I have zsh setup so I can type em (nemonic ‘edit modified’) then press the space bar and it expands to the line shown above, since to my knowledge you can’t make an alias for a command like this. You can also add more auto expanding abbreviations too, which is helpful. Here is the config for that:

typeset -Ag abbreviations

    "em"      "vim \`git status --porcelain | sed -ne 's/^ M //p' -ne 's/^?? //p' | tr '\n' ' '\`"

magic-abbrev-expand() {
     local left prefix
     left=$(echo -nE "$LBUFFER" | sed -e "s/[_a-zA-Z0-9]*$//")
     prefix=$(echo -nE "$LBUFFER" | sed -e "s/.*[^_a-zA-Z0-9]\([_a-zA-Z0-9]*\)$/\1/")
     LBUFFER=$left${abbreviations[$prefix]:-$prefix}" "

no-magic-abbrev-expand() {
    LBUFFER+=' '

zle -N magic-abbrev-expand
zle -N no-magic-abbrev-expand
bindkey   " "    magic-abbrev-expand
bindkey   "^x "  no-magic-abbrev-expand

Once vim loads up all the modified and new files will be loaded into buffers. Then I usually execute :tab sball (I mapped it to <localleader>bt nemonic is ‘buffers to tabs’) to load up all the current buffers into tabs. Though it depends on how many files I had open if I do this or not since tabs can get a bit unwieldy.

Wow… that is way cool! I’ll take a look and see how to adjust that to take the submodules into account – that’s always complicating things because I don’t want to include things from the submodules in the main directory