cknadler / vim-anywhere

Use Vim everywhere you've always wanted to
MIT License
3.67k stars 146 forks source link

Set keybinding for MacVIM #74

Closed jgrasser closed 6 years ago

cknadler commented 6 years ago

Hey Joseph,

Thanks so much for the PR! I'm super excited to try this one out, I never was able to get my attempts to work. I'll get back to you shortly once I test it.

cknadler commented 6 years ago

@jgrasser Ok, this totally works for me in terms of setting the shortcut. The only issue I ran into was that it doesn't take affect right away. After running the install script, hitting ctrl+cmd+v doesn't do anything. If I restart finder, then everything works great.

I think maybe sticking a killall Finder after may do the trick (since finder restarts automatically)? Let me mess around with it and I'll get back to you.

cknadler commented 6 years ago

I can't seem to get the shortcut to work right away. If I manually restart finder (select, cmd+q, select again), the shortcut starts working. If I try to restart finder programmatically however, it doesn't seem to work.

Here's what I've tried so far...

killall Finder
osascript -e "tell application \"Finder\" to quit"
osascript -e "tell application \"Finder\" to activate"

Any ideas?

jgrasser commented 6 years ago

@cknadler I think you've hit on the right idea.

Below I've linked an article that details how the user defaults system works. They claim that using the defaults command will update the cache immediately. That does not appear to be true in macOS High Sierra version 10.13.3. I don't have a solution as of yet, but I think there is value in documenting what didn't work.

None of these methods resulted in the immediate update to the system defaults:

  1. killing Finder and cfprefsd from the cli using killall / pkill / kill -9
  2. using /System/Library/CoreServices/pbs -flush/-update,
  3. restarting the syncdefaultsd service using launchctl.

https://eclecticlight.co/2017/07/06/sticky-preferences-why-trashing-or-editing-them-may-not-change-anything/

jgrasser commented 6 years ago

I played around with the System Preferences method of setting the hotkey and noticed that a macvim process was running in the background. Whether or not System Preferences spawns the process is unknown to me, but I've found via experimentation that starting macvim after installation causes the hotkey config to be loaded. So, I have modified the installation script to run vim-anywhere before exiting.

At this time, macvim opens a blank file. You can modify this to open the readme or some other file.

@cknadler let me know if this works on your side.

cknadler commented 6 years ago

Thanks so much for digging into this! Just ran it.

Installing vim-anywhere...
Cloning into '/Users/kndlrc/.vim-anywhere'...
remote: Counting objects: 378, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 378 (delta 9), reused 18 (delta 7), pack-reused 356
Receiving objects: 100% (378/378), 281.71 KiB | 0 bytes/s, done.
Resolving deltas: 100% (196/196), done.
bash: line 143: ./bin/run: No such file or directory

I think maybe there is a relative path issue? Debugging now.

jgrasser commented 6 years ago

You are correct - when I tested the install script I was in the vim-anywhere directory.

cknadler commented 6 years ago

No worries. I did some digging around too and it seems like you can focus any application (just by clicking on it) and the shortcut starts to work? I'm going to try seeing if simply focusing finder without restarting it makes this work as well. Otherwise, I'm fine with just opening the readme.

jgrasser commented 6 years ago

Cool, I was able to replicate your findings.

cknadler commented 6 years ago
  defaults write pbs NSServicesStatus '{
        "(null) - VimAnywhere - runWorkflowAsService" = {
            "key_equivalent" = "@^v";
        };
        "org.vim.MacVim - MacVim/New MacVim Buffer With Selection - openSelection" = {
            "key_equivalent" = "@^v";
        };
  }'

  osascript -e "activate application \"Finder\""

Awesome! I just threw this together and it works great for me. Mind testing it as well? If it works for you and you update your PR, I'll merge it in right away!

jgrasser commented 6 years ago

Hmm.. the it doesn't look like selected text is making it's way into the buffer.

cknadler commented 6 years ago

Did that ever work? On mainline I believe vim-anywhere just opens a blank MacVim session. Do you mean text isn't making it's way in initially from keyboard shortcut -> MacVim or when closing MacVim from MacVim -> paste buffer?

jgrasser commented 6 years ago

The first one, test from keyboard short does not make it's way into MacVim. If that is expected behavior, then I will clean a few things up in the pr.

cknadler commented 6 years ago

Yeah, thats expected behavior. There are PRs open that add that functionality, but nothing has been merged.

cknadler commented 6 years ago

Also, huge thanks for figuring this out. This was the biggest open issue I've wanted to close by far.

jgrasser commented 6 years ago

Ok, I cleaned up the PR. When you're ready feel free to merge it in.

jgrasser commented 6 years ago

As part of the "clean up" I migrated the defaults settings to a plist file. I regretted doing that and quickly undid those changes. Anyways, PR is now truly ready for merge.

cknadler commented 6 years ago

Awesome, merging. Thanks again!