
Vim Anywhere - jstrieb
https://github.com/cknadler/vim-anywhere
======
jxy
I have an Automator workflow script, which uses emacsclient to edit any text,
in my `~/Library/Services`. The service receives selected text in any
application and output replaces the selected text. It consists of two parts:
Run AppleScript, and Run Shell Script.

The AppleScript part is:

    
    
        on run {input, parameters}	
        	tell application "EmacsMac" to activate
        	tell application "System Events"
        		repeat until visible of process "Emacs" is true
        			delay 0.5
        		end repeat
        	end tell
        	return input
        end run
    

and the Shell Script part (pass input to stdin) is:

    
    
        tmp=$(mktemp "$HOME/tmp/EditTextinEmacsXXXXXXXX")
        ec=/Applications/MacPorts/EmacsMac.app/Contents/MacOS/bin/emacsclient
        cat > "$tmp"
        until [ $($ec -q -e '(server-running-p)' 2>/dev/null) == t ]; do
          sleep 1
        done
        $ec -q -c "$tmp"
        rv="$?"
        cat "$tmp"
        if [ $rv -ne 0 ]; then
          echo "Something went wrong.  Please find your text in"
          echo "$tmp"
        else
          rm "$tmp"
        fi
    

Getting it to work with any editor should be straightforward.

~~~
d1egoaz
Wow, this is awesome!

Is there any way to make it work without selecting text? (default to all
available text on the textarea)

BTW, I changed a few things: emacsclient path and the tmp folder:

    
    
      tmp=$(mktemp "/tmp/EditTextinEmacsXXXXXXXX")
      ec=/usr/local/bin/emacsclient
      cat > "$tmp"
      until [ $($ec -q -e '(server-running-p)' 2>/dev/null) == t ]; do
        sleep 1
      done
      $ec -q -c "$tmp"
      rv="$?"
      cat "$tmp"
      if [ $rv -ne 0 ]; then
        echo "Something went wrong.  Please find your text in"
        echo "$tmp"
      else
        rm "$tmp"
      fi

~~~
swsieber
Hmm... I'm not very familiar with this, but couldn't you send a key command to
select the entire text (CMD A)?

~~~
d1egoaz
I'm not very familiar either. But the Automator workflow starts with "receives
selected text", so I'm not sure it has access to the current text field

~~~
swsieber
Did not know that. I've used HammerSpoon on Mac, and you can send key
shortcuts with that... you could probably glue them together easily

------
ulkesh
Maybe I'm just not _getting it_ and am too old for this, but why would I ever
want to have my computer open a completely different program simply to input
text into a text box when it already has focus and I can simply type at that
point?

I understand using vim for development, or editing files while already at a
command prompt; I just don't see the point in this use case other than to feel
clever.

I am willing to be enlightened if I'm missing something.

~~~
eadmund
I don't know if it's as applicable for a vim user, but as an emacs user I
really appreciate having access to the same tools I use to edit all of my
other text: the same search, the same dictionary, the same completion engine,
the same Org-mode keybindings (so that I can save myself a note or TODO as I'm
in the middle of writing), the same … everything.

Honestly, every programme other than emacs is a distraction.

~~~
Lio
I’m a Vim user but this exactly nails the point. I mostly use the terminal for
everything I can because I know the shortcuts and they’re consistent across
operating systems.

For all its faults, readline is a godsend for Vi users.

------
simplify
It's not vim, but many terminal-based keyboard shortcuts are already available
across OS X without installing anything. For example:

    
    
        - ctrl+p - move cursor up one line
        - ctrl+n - move cursor down one line
        - ctrl+f - move cursor forward one character
        - ctrl+b - move cursor back one character
    
        - ctrl+a - move cursor to beginning of line
        - ctrl+e - move cursor to end of line
        - ctrl+k - delete all characters from cursor to end of line
    

There are a few missing useful ones (such as move by word), but you can fill
them in by creating this file[1] in a specific place on your machine.

[1]
[https://gist.github.com/gilbert/10fdd692799fc7269b50abbcfb2e...](https://gist.github.com/gilbert/10fdd692799fc7269b50abbcfb2ea410)

~~~
caymanjim
These are Emacs shortcuts, not Vim. They're available in bash (the default
MacOS shell) because bash uses the readline library. You can also put readline
into Vi mode and use Vi/Vim shortcuts, but none of this has anything to do
with editing files.

~~~
fimdomeio
fish shell has a vim mode for anyone interested

~~~
hnzix
Bash and zsh also have vim mode. The bash mode is more like vi though.

~~~
egwynn
Indeed, it’s in fact called “vi mode” in the bash/readline docs.

------
fao_
Ok, but you can do this with a simple shell script without the reliance on
Gnome/GTK:

    
    
      #!/bin/bash
      dir="/tmp/vim-anywhere/"
      fn="$(date -Iseconds)"
      [ ! -d $dir ] && mkdir $dir
      vim $dir$fn && cat $dir$fn | xsel -b -i
    

Then you just need to bind the script to a key inside your window manager.

Bonus: This works anywhere xorg is available.

~~~
zipperhead
I use something like this but I initialize the file with the contents of the
clipboard. I use xclip but I think xsel can do the same. Here's a snippet:

    
    
        xclip -o > "$tmpfile"    # initialize file with clipboard contents
        gvim --nofork "$tmpfile"
        xclip -i "$tmpfile"      # push file to clipboard
    

Bind it to a global hotkey and you can use it from anywhere.

------
bfred_it
Relevant self-plug: I co-wrote an extension that does this between browsers
and several text editors, it’s called GhostText

[https://github.com/GhostText/GhostText](https://github.com/GhostText/GhostText)

~~~
rpdillon
It's fantastic! I use this every day and it works great. Thanks for the effort
you put into it!

------
davidbanham
I love the idea! I often open up vim to edit some stuff, save it in a temp
file, then copy it. I never realised I needed a more streamlined version but
after seeing this it hit me like a brick.

I don't think I need that much scaffolding, though. I just knocked this
together:

    
    
        #!/bin/bash
        vim /tmp/everyvim && cat /tmp/everyvim | xclip -sel clip -i

------
Theodores
No Windows version, where I need it.

If you are used to Vim then regular editors are dangerous, you find things
like '=aB' or 'jjjjj' or 'ZZ' or ':w!' embedded in one's files, breaking
everythng.

On Windows you can't just copy and paste from something edited in a terminal,
or copy paste to a terminal to do your editing/saving as there is no 'vi'.

I note this software could be upgraded to work with Windows now there is that
Ubuntu subsystem in there somewhere. Perhaps a better way would be to modify
the accessibility tools - StickyKeys etc. - to have vim be the universal text
entry arrangement even for things like username text boxes, you ':wn' to get
to the password field and then type 'i' to start entering a password. Much
easier.

~~~
marcoperaza
There are vim builds for Windows, both graphical and terminal. You don't need
to use the linux subsystem for that.

------
amelius
Meanwhile, the Escape key on popular keyboards is getting smaller and smaller
:(

~~~
platinumrad
You can rebind caps lock to escape.

~~~
tetraodonpuffer
or rebind it to ctrl if held, escape if tapped, making it even better for RSI
purposes

~~~
mkl
What do you use to do this? On what operating systems?

~~~
farresito
If you are on Linux, try xcape; on Windows, you can do it with autohotkey.

~~~
fencepost
Remapping CapsLock to Ctrl on Windows is easy with KeyTweak (what I have is
2.3.0 by Travis Krumsick), though I had to select the CapsLock key from the
displayed keyboard rather than simply hitting the key. All it appears to be
doing is updating the Registry's key mapping values based on the keyboard scan
codes. Easily found with a simple search, and it does work on Windows 10 even
though it hasn't been updated for 8+ years.

I suspect you'd have to switch to something like Autohotkey for the Esc/Ctrl
mapping.

------
mikerathbun
Very cool. I am using it now to write this comment!

exit

quit

exit()

ctrlC

ctrlx

stop

please exit

help exit

~~~
applecrazy
ESC :wq should do the trick. (Yes, I know it's a joke)

~~~
ufo
Is there a way to exit without using the ":" key?

I ask because the other day I saw a friend of mine legitimately get stuck
inside vim because he coundn't type ":". The keyboard language was set to the
wrong language and none of the keys would output ":" when pressed. We had to
reboot the machine and find a different keyboard.

~~~
gowld
Ctrl-Z to suspend and then you can kill the job

~~~
adtac
Yanking the power cord usually does the trick for me too.

------
dbolgheroni
Vimperator did that. Just miss it.

------
leephillips
I like the idea, as I also want to write everything in Vim. But I (obviously)
always have Vim running, and can get to it from anywhere with one keystroke.
Once there, opening a buffer and copying its contents after writing stuff is
just a few more keystrokes; so I don't see the need for this, at least for my
setup--and I'm thinking that other hardcore Vim lovers have things set up
similarly.

~~~
eadmund
> But I (obviously) always have Vim running, and can get to it from anywhere
> with one keystroke. Once there, opening a buffer and copying its contents
> after writing stuff is just a few more keystrokes …

As an emacs user, I just love seeing vim-users discover the utility of an
always-there, complete text-editing environment. If only theirs had an
extension language worth its salt …

~~~
super_mario
You can write vim extensions in python, Perl, ruby, lua and of course viml.

And vim launches in milliseconds, so no need to keep it running in the
background as some text editing server.

~~~
leephillips
Both true, but since I'm always editing something, I have it open in server
mode so I can send a file to it for editing from the terminal.

------
weinzierl
Similar but only for the browser there is the "It's All Text!" Firefox Add-on.
I used it for years, but unfortunately it stopped working recently when I
upgraded to Firefox Quantum, so "Vim Anywhere" is a welcome alternative.
Certainly will try it.

~~~
rpdillon
Check out GhostText: [https://addons.mozilla.org/en-
US/firefox/addon/ghosttext/](https://addons.mozilla.org/en-
US/firefox/addon/ghosttext/)

I use it with Atomic Chrome for Emacs, but it supports Vim, Sublime, and Atom
as well.

------
humbly
Here is the Emacs version for OS X (Emacs Anywhere, if you will). Just made
it. Check it out at [https://github.com/zachcurry/emacs-
anywhere](https://github.com/zachcurry/emacs-anywhere)

------
michaelsbradley
A sort of similar effect can be achieved with iTerm's hotkey window.

I usually run an emacsclient inside tmux inside the hotkey window, with a
scratch text-mode buffer active.

If I want to use emacs to work on some text from a non-emacs app, the steps
are something like: select text, cmd-c, press hotkey (mine is option+space),
ctrl-y, edit the text, select what I need, opt-w, press hotkey to switch back
to the app, then cmd-v.

Since the hotkey window is running tmux, it's easy to create or switch to
another tmux window and operate on the text with the help of pbpaste/pbcopy
and good old pipe.

------
yjftsjthsd-h
Does anybody know of a way to do something like this in X11? I appreciate that
macs have very nice built-in automation framework, but I'm not abandoning
Linux just for that. There's xdotool for typing, but I don't know of any way
to grab text boxes.

~~~
fao_
I wrote a five line script for it here >>
[https://news.ycombinator.com/item?id=16398356](https://news.ycombinator.com/item?id=16398356)

~~~
yjftsjthsd-h
Not what I was thinking, but that works. Thanks

------
sevensor
Perhaps I've missed the point here. Couldn't you set VISUAL to your editor of
choice?

~~~
throw_away
It's for things like web forms or other GUI apps, for whom $VISUAL is not
meaningful.

~~~
bch
A different tact (for some cases) to the same end: use elinks[0] and in an
active <textarea>, press shift-ctrl-T to pop up your favourite editor[1]. This
works for gmail, for example.

[0]
[https://en.wikipedia.org/wiki/ELinks](https://en.wikipedia.org/wiki/ELinks)

[1] [https://en.wikipedia.org/wiki/Nvi](https://en.wikipedia.org/wiki/Nvi)

~~~
jamesgeck0
I can't recommend that. Elinks doesn't verify SSL certs when connecting to
HTTPS sites. It hasn't had a release in five years. It also has worse support
for web standards than IE6.

~~~
bch
Huh, TIL. I think I actually don’t care about any of that except for the cert
verification, which is a Big Deal. Thx for the reply!

~~~
shakna
Lynx doesn't have as good standard support, but it is still semi-active
(2017-07-10 (2.8.9dev.16)) over at InvisibleIsland[0], and can be compiled
with OpenSSL support. (Windows installers require OpenSSL by default).

[0] [http://invisible-island.net/lynx/lynx-develop.html](http://invisible-
island.net/lynx/lynx-develop.html)

------
smaddox
I'm on mobile right now, so not able to test it, but if you start with
selected text, does it fill the buffer with it? If not, perhaps this feature
could be added? It would be helpful for quickly dropping into to vim for
advanced editing.

~~~
ivarv
There's an outstanding PR that does precisely that -
[https://github.com/cknadler/vim-
anywhere/pull/52](https://github.com/cknadler/vim-anywhere/pull/52)

~~~
deecewan
It's just become apparent to me that outstanding has two very different
meanings depending on context, and this is one of the first times I've seen it
used where it could be ambiguous.

------
tyingq
Somewhat related for vi enthusiasts:
[https://news.ycombinator.com/item?id=9055523](https://news.ycombinator.com/item?id=9055523)

The comment about rlwrap is especially handy.

------
numbers
does this work on High Sierra for anyone? I'm having trouble getting the
shortcut to trigger when in a text input on Chrome. I tried a couple different
shortcuts already :/

------
staticvar
Clever architecture! Loving it!

------
orliesaurus
As someone who recently switched to vim and neovim - wow that's badass!

------
jcranberry
.

------
greatamerican
vim needs to be used in fewer places, not more.

~~~
ibejoeb
Why? I use vi/vim for its ubiquity. Isn't it nice knowing you can show up at
practically any terminal and have it at your disposal?

~~~
nategri
Exactly why I switched to vim from emacs. I also never let my bashrc or vimrc
files grow too baroque.

~~~
gkya
Knowing both is not hard. Also if you don't change too much defaults it's not
hard to downgrade to vanilla programs. My init.el is above 3000 lines, but I
can easily use vanilla Emacs any time if I need to (or vi/vim/ex/ed for that
matter). Same for bash, my .profile and .bashrc combined count ~300 lines,
both POSIX compatible, and I'd hardly have a hard time at a foreign shell
prompt. That's because I use configuration to extend, configure and
personalise a program, instead of trying to recreate it.

(Edit: 300 not 900)

------
moistoreos
while I love Vim and enjoy using Vim..... _facepalm_

~~~
aidos
Why?

------
myrandomcomment
testing that this works as comment....cool!

------
kilon
Looks like a title of a horror movie

