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.
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.
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.
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.
If you're not a heavy Vim user you may not understand but the only way I can describe it is that Vim is different enough that it makes you forget how to edit text the "normal" way. One example is that Vim doesn't require arrow keys, so having to use arrows to navigate text in a normal editor is a huge pain.
And hjkl is not the arrow keys' replacement, as many non-users think.
There are a hundred other ways to move around a file, each of which are more efficient than hitting a button ten times. Those kinds of things are what makes Vim convenient (though not having to move your hand to the arrow keys for going one line down is pretty convenient, too).
I probably wouldn't use this either, but I can see how it can be helpful. If you do a lot of posts on Stackoverflow, you can get some markdown+code highlighting. People use to Vim can use all it's fancy searching and jumping to specific points of text. You can do regex replaces.
Basically if you want a powerful editor just in case when you're filling out a form or something else, here you go?
I'm a sublime user but I find myself accidentally using keyboard shortcuts when doing things such as:
- Writing emails in gmail
- Replying to instant messages
- Writing answers on stack overflow
- Filling out web forms
This is mostly for multiple cursors which are now ingrained in how I type. I'll often copy text to sublime and then copy it back so having a automated way of doing this doesn't seem crazy to me anymore (it would have done 5 years ago).
Some keybindings are ingrained. I use Quail and Electric quotes all the time in Emacs (modes which modify the letters you input), and I write lots of prose in Emacs, so using Ctrl-q <char> to "quote" a character to input it literally is something I frequently do. It happens that sometimes I do that out of muscle memory in Firefox, and Ctrl-q quits in Firefox. Or you press Ctrl-n for next line a couple times but you get some new blank windows instead. I confuse Ctrl-f and Ctrl-s all the time. Many other bindings are easily confusable.
I'd actually like Firefox to be customisable instead of a hack like this. I'll probably just switch to the Next browser [1] in the future though.
The reason I don't see the benefit is that, if a piece of text is long enough that I prefer using vim over just typing in the text box, then I probably want to actually save a copy to disk in case clicking "submit" doesn't go through. So I might as well actually use vim at that point.
That's how it works (worked) with It's All Text! extension for Firefox: you
set an editor (`gvim -f' in my case) and on a keybinding it's started with
a file as its argument. Text area gets updated on write to the file.
Normally you don't need to see or worry about the intermediate files, but in
case anything goes awry, you still have the option.
I'm looking forward to using this for writing JIRA tickets. Their text boxes override some mac key bindings that are deep in my muscle memory. Want move back a space? Oops, ctrl-b in this text box makes text bold. Jump to the start of the line, well ctrl-a now inserts a link. There's something just so annoying about that, it makes me bristle at using jira in general.
Vim (and other editors) offer powerful editing macros that ease bulk text operations. Simple things like text substitution across hundreds of lines save so much time. I still use https://github.com/docwhat/itsalltext running on https://www.waterfoxproject.org/ which allows textarea editing from the terminal.
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.
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.
> but none of this has anything to do with editing files.
Why not? You can use these shortcuts across OS X. That includes your web browser and any other GUI you have on your machine (I use them heavily in notational velocity).
This is actually a readline thing, so you can edit your $HOME/.inputrc with
set editing-mode vi
and get the vi bindings in everything that supports readline, e.g. the Python shell etc. There's tons more that can be customized here. You can even have it put the current vi mode in your prompt and stuff like that.
Right, I just thought it was interesting enough to point out how there are many navigation shortcuts that are built into OS X. Useful even when you're using someone else's computer.
> I find using vim bindings on the commandline to be infuriating.
Some folks find vim bindings to be just as bad to edit text grin
I don't actually agree: I think that vim's text-editing language is actually nicer than emacs's is. But I use emacs — with its bindings, not vim's — because after almost thirty years it's what I'm used to.
Haha, I've never been interested in the whole vim vs. emacs thing. I just started with vim and a little too invested in it now. It definitely drives me nuts that it loads entire files into memory and that is the one thing that has made me tempted to switch. It rarely affects me but every once in a while when I accidentally open a massive file in vim and have to wait, I dream of emacs. I would use evil mode, OF COURSE :)
Oh, if you like those bindings in insert mode, you are probably already aware of Tim Pope's vim-rsi [1] plugin. If not, check it out, it might have some interesting bindings you aren't using yet.
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:
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:
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.
Fun fact, Ctrl+[ also escapes. Had to retrain my muscle memory to use Ctrl+[ when I got my Touch Bar mac, but I think I actually prefer it to ESC. It's guaranteed to work on vim on any system (unlike caps lock), and it keeps my fingers closer to home row.
Hm... I was able to 'feel' the ESC key on my touch bar. But I then remapped it to Caps Lock using Karabiner. I also remapped it on my Ubuntu laptop (with Xmodmap) since it's so nice. What systems have you not been able to get it to work on?
I stil use ESC for visual mode, but as @Pete_D points out you could use jk there too, however it's a much more common input in visual mode so it would probably not work as well.
Personally I prefer to use Karabiner-Elements to remap the right alt-key to ctrl. Been doing that since the first MacBook Pro when the key in that position was
“Enter”.
I find I don’t really miss the right alt key at all.
And what a nightmare it is to jump on a macbook because of that. I go crazy when I need to use an Apple keyboard. I can't begin to understand that particular ui choice.
as the two other sibling posters said, I use karabiner elements when on os/x and xcape+xmodmap on Linux with this in my .xsession
xcape -e 'Control_L=Escape'
which is run after remapping caps lock to Control_L via xmodmap (just xmodmap -pke to create your xmodmap file, xev to figure out which keycode your caps is, and edit the file after to make that keycode Control_L.
For example my xmodmap file, which also sets up meta/super and other overrides is
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.
I've gotten used to typing CTRL-[ instead of reaching for the Escape key because of the inconsistencies in its size and placement on various keyboards I use. Yes it's two fingers but you have to use CTRL in vim to get any benefit anyway.
Have been using a US keyboard and never looked back to the Swiss one. Much better for programming and accents and umlauts are still easy to type. As I also occasionally type in Portuguese, German, Spanish, and French, I find the US keyboard to be the neutral middle ground.
On linux, I don't use ESC at all anymore, I just press alt + [a normal mode key], while in insert mode. Like `alt + j`, which will leave insert mode and move one line down.
Some alt based sequences are really useful:
* alt + e, a : leave current parenthesis and continue inserting (eg, while typing parameters for a function)
* alt + shift + a : insert mode at the end of current line
* alt + shift + i : insert mode at the beginning of current line, with indentation
* alt + p, a : paste before current position, and continue inserting
Maybe there's a modifier key with a similar behavior on macosx?
>On linux, I don't use ESC at all anymore, I just press alt + [a normal mode key], while in insert mode. Like `alt + j`, which will leave insert mode and move one line down.
Why does that work? Is it because Alt + some_key combos generate an escape sequence starting with the actual ESC character? I know that happens with many function key combos, don't remember or not sure about Alt combos.
This sounds plausible, but I have really no idea of why it works :) I found it by doing a mistype (like it often happens in vim), and didn't see it mentioned in any article that would try to get to the bottom of it yet.
>I found it by doing a mistype (like it often happens in vim)
Ha, I probably found it by some similar way, maybe in vim or maybe in DOS / Windows / Linux at the command prompt or in some curses or terminal application.
>* alt + shift + a : insert mode at the end of current line
Is that different from just Shift-a or capital A, which puts you into append mode at the end of the current line (no matter where you are on the line when you press it)?
yep, it does a 'A' in normal mode, after leaving the insert mode (thanks to alt). The different thing is that you do it (alt+shift+a) in insert mode. So the equivalent would be `ESC, A`.
Then I guess that the Alt key generates an ESC char (ASCII code 27 [1]). Seems to tie in with the fact that Emacs docs say that you can use either Alt or ESC for the Meta key -
which is used a lot in Emacs commands (IIRC - I am not an Emacs user, just had tried it out earlier and think I remember reading read that).
Just use Ctrl-C instead of Escape. Often less hand-motion than going all the way to the upper-left, and it works everywhere, without custom remapping of Caps Lock, etc.
I love the idea of a mechanical keyboard but I worry about productivity when I’m on the move.
I find it easier to stick to the laptop keyboard and trackpad even when at a desk plugged into a big monitor.
Everytime I try to switch to a mechanical keyboard and mouse my wrists start hurting. I guess this would go if I got used to the position but then what would happen when I have to use the laptop keyboard?
(So far I’ve tried an IBM model-m and a Pok3r keyboard).
From my experience, my wrists start hurting if I don't use any wrist rests (despite the name, you're supposed to rest your palms on them)
Also, tilted or high profile keyboards are less ergonomic IMHO.
Don't worry about switching back to the laptop keyboard on the go. You won't be less productive, you will just enjoy it less :)
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.
Ha ha, I don't know about nowadays on Windows/Linux/MacOS (maybe someone else can comment for that), but on DOS, you could get around such issues (like a key damaged or not present) by holding down the Alt key while entering the 1- to 3-digit ASCII code for the key you wanted to enter (0-255). That was used for all kinds of tricks and jokes in those days, like entering a fake space kind of key - some ASCII code while pressing Alt - I forget which one, maybe 255. It would look like the space char on the screen - a blank - but was not the same char. Could be used to create "hidden" files that those not in the know could not access, although the file was otherwise in plain sight in DIR listings and so on :)
Even after 1.5 years of using vi at work, I still constantly get stuck trying to exit. Like I hit Ctrl+C or something, or ZZ with caps lock off, I don't even know. Then it's like "Type :quit to exit" or "Entering video mode" or "Press Shift-something to capture" and I do this random button mashing of Escape and q and ZZ that eventually gets me out, but I never know what I did.
:x is not only a short for :wq - the latter saves (unconditionally, even if you have saved earlier and not edited since), and then quits (the file). I was going to say that :x is also a shortcut for :q (just quit) but it is of course not a shortcut for that, since it is the same length :)
I can better describe it like this:
:x is a shortcut for telling vi/vim: save the file if not saved after last change, then quit (in either case, whether a save was needed and done just now or not) - IIRC.
Also, as others said, Shift-ZZ (when Caps Lock is off), also saves and quits (and saves one keystroke over :x - because you have to hit Enter after :x, but not after Shift-ZZ :) It is also (slightly) faster to type, since the (left) Shift key is just next to the Z key (at least on some keyboards, like mine at present).
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.
> 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 …
i3 and ratpoison are tiled window managers for Linux that make your whole computer behave like vim does. If you really are interested in simplifying the amount of work you do- you should consider abandoning the normal "stacked cluttered windows" paradigm in exchange for something that requires less pointless mouse work to achieve the same amount of work faster.
Most monitors have enough real estate to where alt-tabbing is not only unecessary, but counter productive and distracting. Its like using your foot to operate the gas pedal 100% of the time instead of using cruise control to drive hours in a straight line.
Linux (ubuntu) using xfce. Always have one workspace devoted to a full-screen Vim window, so can get there with a keyboard shortcut. In my .vimrc, I have
set clipboard=unnamedplus
so that yanked text is copied to the X11 clipboard. Yank buffer, switch back to browser (or what have you), paste.
With Vimium, the whole procedure is mouseless.
Agreed. This is a clever little hack that I never would have thought of. But I usually have a "temp" or "scratch" buffer running for this kind of thing anyways.
It is cool that apple has the ability to script like this though. It's pretty handy.
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.
This is why I'm still on Firefox 55. Fuck quantum. I used it a little on a different system and the slight speedup (main bottleneck is still WiFi/uplink) is nowhere near worth breaking all addons without even providing APIs to make replacements.
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.
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.
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.
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.
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).
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.
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.
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 :/
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.
The AppleScript part is:
and the Shell Script part (pass input to stdin) is: Getting it to work with any editor should be straightforward.