<p>I know the combination <kbd>Ctrl</kbd>+<kbd>A</kbd> to jump to the beginning of the current command, and <kbd>Ctrl</kbd>+<kbd>E</kbd> to jump to the end. </p>

<p>But is there any way to jump word by word, like <kbd>Alt</kbd>+<kbd>&larr;</kbd>/<kbd>&rarr;</kbd> in Cocoa applications does?</p>

[{'answer_id': 81299, 'author': 'Fil', 'author_id': 12218, 'author_profile': 'https://Stackoverflow.com/users/12218', 'pm_score': 8, 'selected': False, 'text': '<p><a href="http://snipplr.com/view/28113/config-terminal-to-move-wordbyword/" rel="noreferrer">Here\'s how you can do it</a></p>\n\n<p>By default, the Terminal has these shortcuts to move (left and right) word-by-word:</p>\n\n<ul>\n<li><kbd>esc</kbd>+<kbd>B</kbd> (left)</li>\n<li><kbd>esc</kbd>+<kbd>F</kbd> (right)</li>\n</ul>\n\n<p>You can configure <kbd>alt</kbd>+<kbd>&larr;</kbd> and <kbd>&rarr;</kbd> to generate those sequences for you:</p>\n\n<ul>\n<li>Open Terminal preferences (<kbd>cmd</kbd>+<kbd>,</kbd>);</li>\n<li>At Settings tab, select Keyboard and double-click <code>⌥ ←</code> if it\'s there, or add it if it\'s not.</li>\n<li>Set the modifier as desired, and type the shortcut key in the box: <kbd>esc</kbd>+<kbd>B</kbd>, generating the text <code>\\033b</code> (you can\'t type this text manually).</li>\n<li>Repeat for word-right (<kbd>esc</kbd>+<kbd>F</kbd> becomes <code>\\033f</code>) </li>\n</ul>\n\n<p>Alternatively, you can refer to this blog post over at textmate:</p>\n\n<p><a href="http://blog.macromates.com/2006/word-movement-in-terminal/" rel="noreferrer">http://blog.macromates.com/2006/word-movement-in-terminal/</a></p>\n'}, {'answer_id': 81307, 'author': 'Peter Hilton', 'author_id': 2670, 'author_profile': 'https://Stackoverflow.com/users/2670', 'pm_score': 6, 'selected': False, 'text': '<p>I have <kbd>Alt</kbd>+<kbd>&larr;</kbd>/<kbd>&rarr;</kbd> working: open <em>Preferences » Settings » Keyboard</em>, set the entry for <em>option cursor left</em> to <em>send string to shell: \\033b</em>, and set <em>option cursor right</em> to <em>send string to shell: \\033f</em>. You can also use this for other Control key combinations.</p>\n'}, {'answer_id': 81309, 'author': 'Kristian J.', 'author_id': 4588, 'author_profile': 'https://Stackoverflow.com/users/4588', 'pm_score': 10, 'selected': True, 'text': '<p>Out of the box you can use the quite bizarre <kbd>Esc</kbd>+<kbd>F</kbd> to move to the beginning of the next word and <kbd>Esc</kbd>+<kbd>B</kbd> to move to the beginning of the current word.</p>\n'}, {'answer_id': 81333, 'author': 'Andy Lynch', 'author_id': 14338, 'author_profile': 'https://Stackoverflow.com/users/14338', 'pm_score': 3, 'selected': False, 'text': '<p>In Bash, these are bound to <kbd>Esc</kbd>-<kbd>B</kbd> and <kbd>Esc</kbd>-<kbd>F</kbd>.\nBash has many, many more keyboard shortcuts; have a look at the output of <code>bind -p</code> to see what they are.</p>\n'}, {'answer_id': 81343, 'author': 'Matt', 'author_id': 15368, 'author_profile': 'https://Stackoverflow.com/users/15368', 'pm_score': 4, 'selected': False, 'text': '<p>If you check <code>Use option as meta key</code> in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and <code>⌥F</code> (<kbd>Alt</kbd>+<kbd>F</kbd>) and <code>⌥B</code> (<kbd>Alt</kbd>+<kbd>B</kbd>) respectively.</p>\n\n<p>I\'d recommend reading <a href="https://rads.stackoverflow.com/amzn/click/com/1590593766" rel="noreferrer" rel="nofollow noreferrer" title="Amazon.com: From Bash to Z Shell: Conquering the Command Line: Oliver Kiddle, Jerry Peek, Peter Stephenson: Books">From Bash to Z-Shell</a>. If you want to increase your bash/zsh prowess!</p>\n'}, {'answer_id': 87778, 'author': 'Mecki', 'author_id': 15809, 'author_profile': 'https://Stackoverflow.com/users/15809', 'pm_score': 5, 'selected': False, 'text': '<p>Actually it depends on what shell you use, however most shells have similar bindings. The bindings you are referring to (e.g. <kbd>Ctrl</kbd>+<kbd>A</kbd> and <kbd>Ctrl</kbd>+<kbd>E</kbd>) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.</p>\n\n<p>Here\'s a look of default bindings for Bash:</p>\n\n<p><a href="http://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/" rel="noreferrer">Most Important Bash Keyboard Shortcuts</a></p>\n\n<p>Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named <em>".bashrc"</em> that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:</p>\n\n<pre><code>bind -f ~/.bash_key_bindings\n</code></pre>\n\n<p>~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.</p>\n\n<p>Let me show you some excerpts of my .bash_key_bindings file:</p>\n\n<pre><code>set meta-flag on\nset input-meta on\nset output-meta on\nset convert-meta off\nset show-all-if-ambiguous on\nset bell-style none\nset print-completions-horizontally off\n</code></pre>\n\n<p>These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).</p>\n\n<pre><code>"A": self-insert\n"B": self-insert\n"C": self-insert\n"D": self-insert\n"E": self-insert\n"F": self-insert\n"G": self-insert\n"H": self-insert\n"I": self-insert\n"J": self-insert\n</code></pre>\n\n<p>These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).</p>\n\n<pre><code>"\\C-dW": kill-word\n"\\C-dL": kill-line\n"\\C-dw": backward-kill-word\n"\\C-dl": backward-kill-line\n"\\C-da": kill-line\n</code></pre>\n\n<p>This is quite interesting. If I hit <kbd>Ctrl</kbd>+<kbd>D</kbd> alone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).</p>\n\n<p>I placed jumping one word forward on <kbd>Ctrl</kbd>+<kbd>F</kbd> and one word backward on <kbd>Ctrl</kbd>+<kbd>B</kbd></p>\n\n<pre><code>"\\C-f": forward-word\n"\\C-b": backward-word\n</code></pre>\n\n<p>As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.</p>\n\n<p>So if you are not happy with the default bindings, feel free to customize them as you like. Here\'s a <a href="http://www.gnu.org/software/bash/manual/bashref.html#Bindable-Readline-Commands" rel="noreferrer">link to the bash manual</a> for more information.</p>\n'}, {'answer_id': 5542996, 'author': 'Varun Katta', 'author_id': 665170, 'author_profile': 'https://Stackoverflow.com/users/665170', 'pm_score': 9, 'selected': False, 'text': '<p>On macOS (all versions) the following keyboard shortcuts work by default.</p>\n<ul>\n<li><kbd>ALT</kbd>+<kbd>F</kbd> to jump <strong>F</strong>orward by a word.</li>\n<li><kbd>ALT</kbd>+<kbd>B</kbd> to jump <strong>B</strong>ackward by a word.</li>\n</ul>\n<p>Note that you have to make set the <strong>Option</strong> key to act like the <strong>Meta</strong> key. You can do this in Terminal by accessing preferences (CMD+,) and selecting Profiles -&gt; Keyboard. In iTerm2 Pselect rofiles -&gt; Keys -&gt; General and select &quot;Option key as Esc+.&quot;</p>\n<p><a href="https://i.stack.imgur.com/b22Mm.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/b22Mm.jpg" alt="Terminal and iTerm2 Preference Screens" /></a></p>\n<p>Additionally some Emacs-style key bindings for simple text navigation seem to work on bash shells. You can use:</p>\n<ul>\n<li><kbd>CTRL</kbd>+<kbd>F</kbd> to move forward by a char</li>\n<li><kbd>CTRL</kbd>+<kbd>B</kbd> to move backward by a char</li>\n<li><kbd>CTRL</kbd>+<kbd>A</kbd> to jump to start of the line</li>\n<li><kbd>CTRL</kbd>+<kbd>E</kbd> to jump to end of the line</li>\n<li><kbd>CTRL</kbd>+<kbd>K</kbd> to kill the line starting from the cursor position</li>\n<li><kbd>ALT</kbd>+<kbd>D</kbd> to delete a word starting from the current cursor position</li>\n<li><kbd>CTRL</kbd>+<kbd>W</kbd> to remove the word backwards from cursor position</li>\n<li><kbd>CTRL</kbd>+<kbd>Y</kbd> to paste text from the kill buffer</li>\n<li><kbd>CTRL</kbd>+<kbd>R</kbd> to reverse search for commands you typed in the past from your history.</li>\n<li><kbd>CTRL</kbd>+<kbd>S</kbd> to forward search (works in ZSH for me but not bash)</li>\n</ul>\n'}, {'answer_id': 7033964, 'author': 'jches', 'author_id': 223594, 'author_profile': 'https://Stackoverflow.com/users/223594', 'pm_score': 4, 'selected': False, 'text': '<p>If you happen to be a Vim user, you could try bash\'s vim mode. Run this or put it in your <code>~/.bashrc</code> file:</p>\n\n<pre><code>set -o vi\n</code></pre>\n\n<p>By default you\'re in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is <code>w</code> or <code>b</code>, and the <a href="http://tnerual.eriogerg.free.fr/vimqrc.html">usual movement keys also work</a>.</p>\n'}, {'answer_id': 7034637, 'author': 'Chris Page', 'author_id': 754997, 'author_profile': 'https://Stackoverflow.com/users/754997', 'pm_score': 4, 'selected': False, 'text': '<p>As of Mac OS X Lion 10.7, Terminal maps <kbd>Option</kbd>-<kbd>Left/Right Arrow</kbd> to <kbd>Esc</kbd>-<kbd>b/f</kbd> by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.</p>\n'}, {'answer_id': 8250989, 'author': 'cwd', 'author_id': 288032, 'author_profile': 'https://Stackoverflow.com/users/288032', 'pm_score': 8, 'selected': False, 'text': '<p>Switch to <a href="http://www.iterm2.com/" rel="noreferrer">iTerm2</a>. It\'s free and much nicer than plain old terminal. Also it has a lot more options for customization, like keyboard shortcuts.</p>\n\n<p>Also I love that you can use <kbd>cmd</kbd> and 1-9 to switch between tabs. Try it and you will never go back to regular terminal :)</p>\n\n<p><strong>How to set up custom keyboard preferences in iterm2</strong></p>\n\n<ul>\n<li>Install iTerm2</li>\n<li>Launch and then go to preference pane.</li>\n<li>Choose the keyboard profiles tab</li>\n<li>You will either need to copy the profile to something new and then delete the arrow key shortcuts such as <kbd>^</kbd>+ <kbd>Right</kbd>/<kbd>Left</kbd> or if you don\'t care about a backup just delete them from the default profile.</li>\n<li>Next make sure your modified profile is selected (starred) </li>\n</ul>\n\n<p><a href="https://i.stack.imgur.com/cqLnO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cqLnO.png" alt="Picture 1.png"></a></p>\n\n<ul>\n<li>Now choose the keyboard tab (very top row)</li>\n</ul>\n\n<p><a href="https://i.stack.imgur.com/i2o2y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/i2o2y.png" alt="iTerm 2"></a></p>\n\n<ul>\n<li>Click on the plus button to add a new keyboard shortcut</li>\n<li>In the first box type <kbd>CMD</kbd>+<kbd>Left arrow</kbd></li>\n<li>In the second box choose "send escape code"</li>\n<li>In the third box type the letter <kbd>B</kbd></li>\n</ul>\n\n<p><a href="https://i.stack.imgur.com/xFBlY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xFBlY.png" alt="Picture 2.png"></a></p>\n\n<ul>\n<li>Repeat with desired key combinations. <kbd>escape</kbd>+<kbd>B</kbd> moves one word to the left, <kbd>escape</kbd>+<kbd>f</kbd> moves one word to the right.</li>\n<li>you may also wish to set up <kbd>cmd</kbd>+<kbd>d</kbd> to delete the word in front of the cursor with <kbd>escape</kbd>+<kbd>d</kbd></li>\n</ul>\n\n<p>I often hit the wrong button (<kbd>cmd</kbd> / <kbd>control</kbd> / <kbd>alt</kbd>) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.</p>\n'}, {'answer_id': 13780191, 'author': 'mdubez', 'author_id': 1854079, 'author_profile': 'https://Stackoverflow.com/users/1854079', 'pm_score': 7, 'selected': False, 'text': '<p>Actually there is a much better approach.  Hold <kbd>option</kbd> ( <kbd>alt</kbd> on some keyboards) and press the arrow keys left or right to move by word.  Simple as that.</p>\n\n<p><kbd>option</kbd><kbd>&larr;</kbd><br>\n<kbd>option</kbd><kbd>&rarr;</kbd></p>\n\n<p>Also <kbd>ctrl</kbd><kbd>e</kbd> will take you to the end of the line and <kbd>ctrl</kbd><kbd>a</kbd> will take you to the start.</p>\n'}, {'answer_id': 54001064, 'author': 'qix', 'author_id': 954643, 'author_profile': 'https://Stackoverflow.com/users/954643', 'pm_score': 3, 'selected': False, 'text': '<p>Under <code>iterm2</code>\'s <strong>Preferences > Profile > Keys</strong>, you click the <code>+</code> below <strong>Key Mappings</strong> and record a new shortcut. For <strong>Action</strong>, select <code>Send Escape Sequence</code> and type <code>b</code> or <code>f</code> for backwards and forwards respectively. </p>\n\n<p>When I tried to record one for (<kbd>Ctrl</kbd>+<kbd>←</kbd>), I noticed in the <strong>Keyboard Shortcut</strong> field that the arrow never showed up. Turns out I had to disable the default mac\'s <strong>System Preferences > Keyboard > Shortcuts > Mission Control</strong> shorcuts first to get things to work, as they\'ll override iterm2\'s default shortcuts. Should be true for the standard terminal app, too.</p>\n\n<p><a href="https://i.stack.imgur.com/mitIx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mitIx.png" alt="Keyboard system preferences"></a></p>\n'}, {'answer_id': 55639787, 'author': 'Dan Alboteanu', 'author_id': 6204133, 'author_profile': 'https://Stackoverflow.com/users/6204133', 'pm_score': 5, 'selected': False, 'text': "<p>Hold down the Option key and click where you'd like the cursor to move</p>\n"}, {'answer_id': 56026518, 'author': 'Dziamid', 'author_id': 219931, 'author_profile': 'https://Stackoverflow.com/users/219931', 'pm_score': 5, 'selected': False, 'text': '<p>Use <em>Natural Text Editing</em> preset!</p>\n\n<p><a href="https://i.stack.imgur.com/EUuAA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/EUuAA.png" alt="enter image description here"></a></p>\n\n<p>Essentially it binds, among other key sequences, Option + LeftArrow to <code>^[b</code> sequence and Option + RightArrow to <code>^[f</code></p>\n\n<p>This works in fish and bash, as well as in psql terminal.</p>\n'}, {'answer_id': 57968002, 'author': 'Chinmay Chhajed', 'author_id': 7783417, 'author_profile': 'https://Stackoverflow.com/users/7783417', 'pm_score': 1, 'selected': False, 'text': '<p>As answered previously, you can add <code>set -o vi</code> in your <code>~/.bashrc</code> to use <code>vi</code>/<code>vim</code> key bindings, or else you can add following part in <code>.bashrc</code> to move with <code>Ctrl</code> and arrow keys:</p>\n\n<pre><code># bindings to move 1 word left/right with ctrl+left/right in terminal, just some apple stuff!\nbind \'"\\e[5C": forward-word\'\nbind \'"\\e[5D": backward-word\'\n# bindings to move 1 word left/right with ctrl+left/right in iTerm2, just some apple stuff!\nbind \'"\\e[1;5C": forward-word\'\nbind \'"\\e[1;5D": backward-word\'\n</code></pre>\n\n<p>To start effect of these lines of code, either <code>source ~/.bashrc</code> or start a new terminal session.</p>\n'}, {'answer_id': 58190202, 'author': 'Hang', 'author_id': 1581112, 'author_profile': 'https://Stackoverflow.com/users/1581112', 'pm_score': 1, 'selected': False, 'text': '<p>New answer for iTerm2 Build 3.3.4 users:</p>\n\n<p>Step 1: (macOS X) System Preferences > Keyboard > Shortcuts tab > Select Mission Control (left panel) > Uncheck shortcuts that labeled as "Move left a space" and "Move right a space"</p>\n\n<p>Step 2: (iTerm2 Build 3.3.4) Preferences > Profiles > Select * Default (left panel) > Keys tab > Delete both "⌥->" and "⌥&lt;-" entries > Set both "Left Option (⌥) Key:" and "Right Option (⌥) Key:" to Esc+</p>\n\n<p>No messing around with shell profiles, no messing around with inferior masOS (default) Terminal, no awkwards Esc+F/B, rinse &amp; repeat non-sense.</p>\n\n<p>Done deal!!! </p>\n\n<p>Enjoy this tip, my fellow PROGRAMMERS!</p>\n'}, {'answer_id': 63715149, 'author': 'ubershmekel', 'author_id': 177498, 'author_profile': 'https://Stackoverflow.com/users/177498', 'pm_score': 2, 'selected': False, 'text': '<p>For some reason, my terminal\'s option+arrow weren\'t working. To fix this on macOS 10.15.6, I opened the terminal app\'s preferences, and had to set the bindings.</p>\n<pre><code>Option-left = \\033b\nOption-right = \\033e\n</code></pre>\n<p><a href="https://i.stack.imgur.com/fFjC8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fFjC8.png" alt="Keyboard settings in Mac terminal app" /></a></p>\n<p>For some reason, the <code>option-right</code> I had was set up to be <code>\\033f</code>. Now that it\'s fixed, I can freely skip around words in the termianl again.</p>\n'}, {'answer_id': 69114598, 'author': 'conny', 'author_id': 23023, 'author_profile': 'https://Stackoverflow.com/users/23023', 'pm_score': -1, 'selected': False, 'text': '<p>Just check the &quot;<strong>Use Option as meta key</strong>&quot; option in <strong>Terminal &gt; Preferences &gt; Settings &gt; [profile] &gt; Keyboard</strong>, as mentioned here already by @cris-page.</p>\n<p>Note however, that in macOS Catalina (10.15) and newer, <strong>zsh</strong> becomes the default shell for newly added users: its default configuration considers only whitespaces as word-boundaries, whereas the old <strong>bash</strong> makes meta-left/right jump to the nearest <em>non-alphanumerical</em> character (similar to <code>B</code>/<code>W</code> as opposed to <code>b</code>/<code>w</code> for those familiar with <em>vim</em>):</p>\n<pre><code>                  v----v- bash jumps here\n$ vim some-folder/what.txt_&lt;- jump left twice from here\n  ^---^- zsh jumps here by default\n</code></pre>\n<p>(similar motions are true for <em>meta-backspace</em> as well)</p>\n<p>There are more than one ways to make zsh command line editor navigation work similarly to bash\'s - here is one such method:</p>\n<pre class="lang-sh prettyprint-override"><code># Place in your profile init script, e.g. `~/.zshrc`\nautoload -U select-word-style\nselect-word-style bash\n\n</code></pre>\n'}, {'answer_id': 73670596, 'author': 'thiagowfx', 'author_id': 1745064, 'author_profile': 'https://Stackoverflow.com/users/1745064', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s the CLI way to do so, verified it works on <code>bash</code>.</p>\n<p>Add <a href="https://github.com/thiagowfx/.dotfiles/commit/70f60fdc70a02681c263a8d01fc170228c98cee2" rel="nofollow noreferrer">the following</a> to your <code>~/.inputrc</code>:</p>\n<pre><code># macOS Option + Left/Right arrow keys to move the cursor wordwise\n&quot;\\e\\e[C&quot;: forward-word\n&quot;\\e\\e[D&quot;: backward-word\n</code></pre>\n<p>The advantage of this method is that it is terminal application agnostic - doesn\'t matter whether you use Terminal.app, iTerm2, or any other application.</p>\n<p>Inspiration got from <a href="https://unix.stackexchange.com/questions/42629/bash-how-do-i-move-my-cursor-to-the-left-a-word-at-a-time/42635#42635">this other answer</a>.</p>\n'}]
['https://Stackoverflow.com/questions/81272', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']