Moving efficiently in the CLI
clementc.github.io
clementc.github.io
Keep in mind, most of these work anywhere in OS X, not just in the terminal.
Put this in your .bashrc or .zshrc
# MOTD
function echo_color() {
local color="$1"
printf "${color}$2\033[0m\n"
}
echo_color "\033[0;90m" "c-f Move forward"
echo_color "\033[0;90m" "c-b Move backward"
echo_color "\033[0;90m" "c-p Move up"
echo_color "\033[0;90m" "c-n Move down"
echo_color "\033[0;90m" "c-a Jump to beginning of line"
echo_color "\033[0;90m" "c-e Jump to end of line"
echo_color "\033[0;90m" "c-d Delete forward"
echo_color "\033[0;90m" "c-h Delete backward"
echo_color "\033[0;90m" "c-k Delete forward to end of line"
echo_color "\033[0;90m" "c-u Delete entire line" Keep in mind, most of these work anywhere in OS X,
not just in the terminal.
That's one of my absolute favourite things about Mac OS -- because Cmd exists, and takes over what Ctrl is used for on Windows and Linux, I can use these Ctrl commands and they work as I expect.I wonder if it's possible to get Linux to work the way the Mac does in this regard? Allocate a Meta key to do what Ctrl does move of the time, so that these Ctrl commands became available again? I'd really find it easier to use Linux if that was possible.
https://encrypted.google.com/search?tbm=isch&q=kde+rebind+ke...
Mine too, but I'm on Windows where none of this work out of the box. However, I've found XKeymacs [0] which allows Emacs keys system wide. It is not fool proof, but I've found that the movement keys can be used without much trouble.
Currently I use:
Movement:
C-b C-f backward/forward-char
M-b M-f backward/forward-word
C-p C-n previous/next-line
C-a C-e Home End
C-x h
Capitalisation:
M-l M-c downcase-word, capitalise-word
M-F12 rollup-window
C-z iconify-deiconify-frame
C-q enable-disable xkeymacs
[0] https://github.com/fujieda/xkeymacs http://xkeymacs.osdn.jp/
I also use TouchCursor [1] to get vim like hjkl movement system wide while Space is pressed
[1] https://martin-stone.github.io/touchcursor/overview.html
# MOTD
function echo_color
printf "\033[0;90m$argv[1]\033[0m\n"
end
echo_color "c-f Move forward"
echo_color "c-b Move backward"
echo_color "c-p Move up"
echo_color "c-n Move down"
echo_color "c-a Jump to beginning of line"
echo_color "c-e Jump to end of line"
echo_color "c-d Delete forward"
echo_color "c-h Delete backward"
echo_color "c-k Delete forward to end of line"
echo_color "c-u Delete entire line"
And a slightly optimized bash version (YMMV): # MOTD
function echo_color() {
printf "\033[0;90m$1\033[0m\n"
}
echo_color "c-f Move forward"
echo_color "c-b Move backward"
echo_color "c-p Move up"
echo_color "c-n Move down"
echo_color "c-a Jump to beginning of line"
echo_color "c-e Jump to end of line"
echo_color "c-d Delete forward"
echo_color "c-h Delete backward"
echo_color "c-k Delete forward to end of line"
echo_color "c-u Delete entire line" Keep in mind, most of these work anywhere in OS X, not just in the terminal.
Adding on to this - this works in many places in OS X, since many programs happen use NSTextField, which can have shortcuts configured at the OS level.You can further customize the shortcuts available in a file called DefaultKeyBinding.dict
I use a very slightly modified version of this: https://gist.github.com/cheapRoc/9670905
I highly recommend looking into that, for anyone who is familiar with emacs keybindings and would like them everywhere.
However as a Vim user (mostly motivated by the fact that I can move around pressing single keys which I find more "wrist-ergonomic" - potential fast navigation is a nice plus), I always wonder how heavy emacs users deal with CTRL-acrobatics and movement - do you use c-f/c-b for going forward/backward or the usual cursor keys?
For vi users it makes sense to set $readline (~/.inputrc) to vi-style, so for completion here the vi-style bindings (although probably obvious if you used to Vim).
l "Move forward"
h "Move backward"
k "Move up" (step history backward)
j "Move down" (step history forward)
^ "Jump to beginning of line"
$ "Jump to end of line"
Deleting works as usual with `d` combinations. Furthermore it is convenient to search the history via `/`.I do. With emacs, you usually have a finger near the Ctrl key at all times anyway, so hitting f or b is much more convenient than moving my right hand over to the arrow keys.
That way you can press Control or Alt/Meta (or both at the same time) with the thumb of either hand without a lot of movement. C-f and C-b can be done with either left hand only, or left hand for f/b and right thumb on Control (what used to be Alt, on PC keyboards). Rebinding Caps Lock to Backspace also saves a lot of hand movement. Only a handful of small changes and you get a much better keyboard layout.
Looking at the keys below maybe it was for up/down.
I'm hearing people say that "that's impossible" or "it's weird", but I've used this method for ~2 years now. It has become muscle memory.
I put a little pad over the Ctrl Keys to press it easily with muy palm.
I meant to include it in my original post but I can't edit it now.
c-t Swap chars (useful for correcting typos)Also, for c+h/d there's always a dedicated key/single stroke for at least one of those delete actions.
Unless one is a court typist, where wpm matters fully, maybe moving a little and not keeping one's hands on the home row can help avoid carpal tunnel and RSI?
Historically, many of those keybindings date back to very crude remote terminals where control codes and tty modes were a real concern. For example, I’ve used systems (in the 90’s) where the delete key could freeze your remote connection. Emacs, because of it’s age and ubiquity, has grown up somewhat “around” those traps.
set -o vi
Vi keys in the editor, Emacs on the CLI? This has never made any sense to me.For example I use this:
set editing-mode vi
# Up and down arrows keys search history based on what is already typed.
"\e[A": history-search-backward
"\e[B": history-search-forwardBecause most people used awk/grep/sed/ed/ex/vi the regex format tended that way. But enough people used tcsh or bash in emacs by default, that shell and omnibox (when browsers eventuated) edit defaulted to emacs.
Thats my theory: accidental happenstance and defaults.
This also works the other way around: A few EMACS based shortcuts are the better 'lowest common denominator' than to force vi modes on everyone. Many are not willing to go down the vi/vim path, for various reasons.
Consider a long cURL command split over several lines. If every option is on a separate line, removing one is a `dd` keystroke away. Adding a new option is also very efficient.
Extended Vim bindings will also allow you to delete everything within brackets/delimiters (e.g. `di"`) or delete everything up until a character, etc. These are all simple keystrokes that translate well to the shell.
You could also open the command in your $EDITOR anyway (bound to `V` for me), but I reserve that for really long and unwieldy commands.
But, there is something to be said for meeting people where they are. If folks know the emacs keybindings, let them 'control' away!
Ctrl-[ for escape and when in Insert mode, ctrl-h backspace; ctrl-w delete word; ctrl-u delete line; Ctrl-m return; ctrl-i tab, etc.
I think it's mostly down to the way I twist to reach backspace rather than move my whole hand from the home row. Ctrl-h, Ctrl-w and Ctrl-m really help with that.
The only problem I have is that I don't know how to remap Ctr-m to return in X windows applications. I use Karabiner on a macOS and Gnome Tweak tool on Linux for the others.
It's somewhat frustrating to have different shortcuts in the terminal, where I spend most of my time and the browser where I spend most of the rest.
Edit: (If anyone can suggest a good way of mapping ctrl-m to return in Linux browsers I'd really appreciate it.)
I'm always confused by laptop designers who only include one ctrl key. How are we supposed to properly touch type modifiers otherwise?
Just stating the obvious: you’re not their target consumer.
Apple do make it easy to remap the Alt key though and include a terminal and CLI commands by default so they must have some interest in business from people like me. ;)
(I would like to start a campaign to get the Mac Unix science page back again https://www.macobserver.com/tmo/article/apple-drops-science-...)
Advised by http://xahlee.info/kbd/linux_swap_modifier_keys.html
! -*- coding: utf-8 -*-
! 2013-02-04 swap Alt and Ctrl http://xahlee.info/
! xmodmap -pke | egrep '(Control|Super|Alt|Menu)'
! keycode 37 = Control_L NoSymbol Control_L
! keycode 64 = Alt_L Meta_L Alt_L Meta_L
! keycode 105 = Control_R NoSymbol Control_R
! keycode 108 = Alt_R Meta_R Alt_R Meta_R
! keycode 133 = Super_L NoSymbol Super_L
! keycode 134 = Super_R NoSymbol Super_R
! keycode 135 = Menu NoSymbol Menu
! keycode 147 = XF86MenuKB NoSymbol XF86MenuKB
! keycode 204 = NoSymbol Alt_L NoSymbol Alt_L
! keycode 206 = NoSymbol Super_L NoSymbol Super_L
clear control
clear mod1
keycode 37 = Alt_L Meta_L
keycode 105 = Alt_R Meta_R
keycode 64 = Control_L
keycode 108 = Control_R
add control = Control_L Control_R
add mod1 = Alt_L Meta_L emacs
Why not just use emacs everywhere? I suppose if you use spacemacs (which has vim keybindings) then you might want to 'set -o vi' on the command line.Emacs has Org-mode & Magit, each of which should be reason enough to switch. Then there're notmuch, gnus, slack-mode, eww …
Seriously, emacs really is a more powerful environment[0]. Modern vim tries to be as flexible as emacs, but the language isn't as nice.
E.g. Damian Conway's[1] simple example:
function! ToggleSyntax()
if exists("g:syntax_on")
syntax off
else
syntax enable
endif
endfunction
nmap <silent> ;s :call ToggleSyntax()<CR>
would be: (defun toggle-syntax ()
(syntax (not syntax))
(global-set-key (kbd "M-C-s") toggle-syntax)
in a hypothetical elisp for a vim-like editor[2]. In his article he refers to vimscript a more humane than elisp, but I think this simple example shows he's wrong.His next example is a command to capitalise a line, centre it, then move down:
function! CapitalizeCenterAndMoveDown()
s/\<./\u&/g "Built-in substitution capitalizes each word
center "Built-in center command centers entire line
+1 "Built-in relative motion (+1 line down)
endfunction
nmap <silent> \C :call CapitalizeCenterAndMoveDown()<CR>
I think this is a lot clearer: (defun capitalize-centre-and-move-down ()
(interactive)
(upcase-region (line-beginning-position) (line-end-position))
(center-region (line-beginning-position) (line-end-position))
(forward-line))
[0] https://sites.google.com/site/steveyegge2/effective-emacs[1] https://www.ibm.com/developerworks/library/l-vim-script-1/in...
[2] For the real emacs, it's just font-lock-mode.
I prefer M-x and fuzzy function entry to key bindings and when I'm in a terminal buffer or eshell I always go for M-x which won't work.
Any tips would be appreciated as I really like everything else about Emacs.
set editing-mode viYou may try `rlwrap` which wraps programs without readline support.
It's never PostGRE.
$ cat >~/.inputrc
set editing-mode vi
set keymap vi
$if mode=vi
set keymap vi-insert
"jj": vi-movement-mode
$endif
Obviously you want to change vi-movement-mode to your favorite ESC style. E.g. searching through your last git commands becomes 'jj/git' then (you have to go to movement mode first, as insert mode is the default in the shell - which makes kind of sense). $ sudo -i
$ cd /va<tab> #results in "cd /var"
$ set -o vi
$ cd /va<tab> #results in: "cd /va "I still prefer Vim as my editor, but learned these key combo's because they are just so portable.
C-f -> f for forward
C-b -> b for backward
C-p -> p for previous
C-n -> n for next
C-a -> a for Anfang
C-e -> e for Ende
C-k -> k for kill forward
C-u -> just memorise this one. It's the one you use when you suspect you've mistyped a password in ssh or login shell.
Alt/meta sometimes does what Ctrl does, but bigger.
BONUS TIP: In GNU screen, you can use C-a-a for C-a to jump to the beginning of the line. Now you no longer have a reason to passionately hate the default C-a meta-binding for screen. You're welcome.
Anyway, it's far more intuitive to use arrow keys with modifiers:
* control + left-arrow = alt + b
* control + right-arrow = alt + f
* alt + backspace = control + w
I get super upset (pet peeve) when programs don't support word navigation like this.
What I would like would be for someone to build a web app which allows you to select what modifiers you want for each of the shortcuts, and then has a button which would generate configuration files for various programs (vim, emacs, tmux, gnome-terminal, etc) that you can download or copy/paste. That would be grand.
### Line cuts will be remapped to cmd-remove in OSX, shift-remove in Linux
## OSX: Remap with ControllerMate Cmd+Delete to Ctrl-k
"\e[3;2~": kill-line
## OSX: Remap with Controllermate Cmd+Backspace to Ctrl-u
"\e[9;2~": backward-kill-line
### Word cuts will be remapped to opt-remove in OSX, alt-remove in Linux
## OSX: Remap Alt+Delete with ControllerMate to Ctrl-w
"\e[3;3~": kill-word
## OSX: Remap Alt+Backspace with ControllerMate to Esc-d
"\e[9;3~": backward-kill-word
In other words, this argument topic usually doesn’t end reasonably for either side. :-)
P.S. For extra fun, look up the mouse-vs.-hotkeys discussion. My favorite example is in the research paper for Plan 9’s Acme & 8½ ui.
Exception: ^U and ^W are old unix "cooked" io keystrokes but the relevant Emacs ones also work (e.g. meta-delete instead of ^W).
However the mac text widget doesn't use readline.
edit:s/than/then/
"^u" = "deleteToBeginningOfLine:";
"^w" = "deleteWordBackward:";In Terminal.app on macOS, alt + mouse click will move your cursor anywhere. I am not sure how exactly this works. It also seems to work while I am inside Terminal.app and logged on to a remote Linux system.
Also, simply clicking works while running a shell in Emacs using M-x shell
For example if you had a screensaver with a password prompt, you could type some characters, then: 1. ctrl-a 2. ctrl-k 3. ctrl-y ...repeat steps 1-3 several times and hit enter.
The screensaver would crash to desktop, or on the logon prompt, you'd crash to the console mode available by entering >console as a username.
I think these were largely patched by 10.3.0.[2]
[1] https://www.cnet.com/news/mac-os-x-screensaver-security-cras... [2] https://apple.slashdot.org/story/03/07/05/187203/screensaver...
- alt+arrow to jump words,
- cmd+arrow to jump to start/end of line,
- cmd+z/cmd+shift+z for undo/redo,
- alt+delete to delete word
Just discovered it, it's amazingLearning keyboard shortcuts is very beneficial, at least in cases where you often do the same things. CMD or Ctrl L is one of my favorites (jump to address text box in most browsers).
It's a bit like coffee^h^h code (wow, autocorrect) optimization; a few improvements in a few places can save an immense amount of work or time.
Update: Never mind. Turns out alt+most keys on macOS just types special characters, making the ones I didn't know useless to me anyway :-D
For the update - this is a problem with the built-in terminal. You can use iTerm and get the expected (within a terminal) key bindings, or remap the option binding within its preferences panel.
BTW, if you're using Terminal.app, you have to explicitly enable "Use Option as Meta key" in the preferences.
Bash also has a vim mode which makes sense to use for vim users. This connection with the editor really helps a lot.
xset r rate 160 80
I haven't found how to do this in OSX yet, but there is probably a way.
defaults write -g KeyRepeat -int 0.02
https://github.com/herrbischoff/awesome-macos-command-lineLast thing you want to do is look like an idiot trying to fix your friend's computer because you can't even remember how to get around in their default bash shell.
This gives me an interesting idea, though... Are there any "portable shell" applications that painlessly provide a mechanism for storing your preferred shell, necessary dependencies and configuration files on a thumb drive that's as easy to use as `/mnt/shelldrive/zsh`?
[1] https://github.com/tekezo/Karabiner-Elements [2] https://pqrs.org/osx/karabiner/complex_modifications/#termin...
Run "emacs" and then press C-h t (Control-H then T) to start the tutorial.
Or you can read it [1], but the tutorial is interactive, so open it in Emacs if you have it.
[1] http://www.cs.put.poznan.pl/anstroinski/data/uploads/sop1/ma...
I use the default mac terminal. I was excited because I'm always forward and backing up in previous commands.
But alas... Alt F and Alt B just print characters (ĺ). I tried to see if there was a configuration I could set, but to no avail. Maybe I need a better terminal.
https://gist.github.com/yelinaung/849c78c40784c56e05f5a04dde...
Any tips for i3wm users that have these kinds of keybind conflicts ?
Basically, find a redundant key to use as your wm mod key.
Alt is a common modifier for navigation though, things like qutebrowser use it to navigate tabs and such. It's worthwhile to switch and the "Windows" key hasn't conflicted with anything for me. In my opinion it's a saner default than the current.
I've been using i3wm for years with zero conflict with Emacs itself, let alone the shell's Emacs-like keybindings.
MyModel.objects.filter(UUID=‘123e4567-e89b-12d3-a456-426655440000‘)
The number doesn't have to be a single digit, so Alt-3, Alt-6, Backspace, or Alt-3, Alt-6, Ctrl-H also work (delete 36 characters.)
(Your command would normally be described is Alt-9, Alt-Backspace; or M-9 M-Backspace; to show the Alt/"Meta" key is held for both strokes.)
Seems pretty silly to key CTRL-something when there are dedicated buttons for the functions already.
To match two commands containing <chars>
[somekey] + up
that scrolls through history sorted by frequency of use.
> set -o vi
Well, sure; that's because you've used the screen dimmer.