57

One thing i miss from the Linux shell is the Alt+Dot shortcut. What it does is insert the last argument of the previous command. It appears to be a trivial technique, but it's really useful.

i.e. if you type this:

$ ls /Applications/MAMP/conf/apache/

The next time you just have to type a command + shortcut and the argument will be completed for you.

$ cd 'alt+dot'

On Mac i know 2 alternatives; pressing Esc+Dot or typing !$, !* with the magic space setup.

However while both alternatives do insert the last argument(s) you've used, they can't scroll back in command history like typing Alt+Dot repeatedly does.

I can live with this options for now, but i'd love to know if someone knows a better way to do it.

6 Answers 6

37

You can press Escape+. many times, it does the same thing as Alt+..
But, like you, I find it more cumbersome to type than Alt+..

3
  • 6
    I can press it many times, but it just works the first time
    – pablasso
    Dec 18, 2009 at 16:00
  • 3
    Nevermind, you're right. You just have to release esc to press it again, Then it works as expected scrolling through history. Being that treating the option key as meta messes up some other shortcuts, this is the cleanest solution.
    – pablasso
    Dec 18, 2009 at 19:22
  • 2
    The easiest way to do this is to check "Use Option Key as Meta" in Terminal preferences, as described in Richard Hoskins' answer.
    – Debajit
    Aug 21, 2014 at 13:39
51

In Terminal.app, Preferences->Settings, select the Keyboard tab. Ensure "Use Option Key as Meta" is checked.

4
  • 6
    btw, i should mention that while this solves the alt+dot problem, it messes up another keyboard combinations like ], } or |. My keyboard (spanish) uses option+somekey to type them. Well, you can't have it all.
    – pablasso
    Dec 18, 2009 at 19:09
  • 12
    For iTerm, open Preferences, Profiles > Keys, Choose option key acts as +Esc at the bottom.
    – Ivan Chau
    Aug 18, 2014 at 7:19
  • 2
    For Terminal inside VSCode, set "terminal.integrated.macOptionIsMeta" to true.
    – Eki
    Oct 31, 2021 at 11:54
  • Regarding the conflict of key combinations @pablasso mentioned: stackoverflow.com/a/25183276/648271 describes a way of re-mapping needed combinations via .inputrc so you can re-enable needed combinations for your keyboard (in my case i.e for ~,@).
    – Holgzn
    Aug 2, 2023 at 13:24
7

Use option+.

Note that this is a feature of bash and not linux. Macs have bash on them as well. Alt-. is the shortcut for a builtin bash function insert-last-argument. Read the READLINE section of the bash man page for how you can bind this to a different key combination.


Pasting a few relevant parts:

   Readline Initialization
       Readline  is customized by putting commands in an initialization file (the
       inputrc file).
       The  default key-bindings may be changed with an inputrc file.  Other pro-
       grams that use this library may add their own commands and bindings.

       For example, placing

              M-Control-u: universal-argument
       or
              C-Meta-u: universal-argument
       into the inputrc would make M-C-u execute  the  readline  command  univer-
       sal-argument.

   Readline Key Bindings
       The  syntax  for  controlling  key bindings in the inputrc file is simple.
       All that is required is the name of the command or the text of a macro and
       a  key  sequence to which it should be bound. The name may be specified in
       one of two ways: as a symbolic key name, possibly with Meta-  or  Control-
       prefixes, or as a key sequence.

       When using the form keyname:function-name or macro, keyname is the name of
       a key spelled out in English.  For example:

              Control-u: universal-argument
              Meta-Rubout: backward-kill-word
              Control-o: "> output"

       In the above example, C-u is bound  to  the  function  universal-argument,
       M-DEL is bound to the function backward-kill-word, and C-o is bound to run
       the macro expressed on the right hand side (that is, to  insert  the  text
       ``> output'' into the line).

       In  the  second form, "keyseq":function-name or macro, keyseq differs from
       keyname above in that strings denoting an entire key sequence may be spec-
       ified  by placing the sequence within double quotes.  Some GNU Emacs style
       key escapes can be used, as in the following  example,  but  the  symbolic
       character names are not recognized.

              "\C-u": universal-argument
              "\C-x\C-r": re-read-init-file
              "\e[11~": "Function Key 1"


   Commands for Manipulating the History
       insert-last-argument (M-., M-_)
              A synonym for yank-last-arg.
1
  • 1
    Thanks, i know is a bash feature but the shortcut is not enabled by default, at least not in Terminal.app since there's no meta key by default. See Richard's answer.
    – pablasso
    Dec 18, 2009 at 16:13
2

If you open Terminal.app and enter preferences, and go to Settings > Keyboard and check “Use option as meta key”, you can use the bash shortcuts that use alt, for example:

Alt+B: Skip word backwards

Alt+F: Skip word forwards

Source

1
  • 1
    Welcome to Super User! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. For now, I edited the answer for you. Aug 15, 2012 at 19:30
2

If you're using iTerm2, do the following:

  1. Select Preferences -> Profiles -> (select the currently active profile, or Default) -> Keys
  2. Under the Key Mappings menu press the small '+' button
  3. Click on Keyboard Shortcut
  4. Press Option+. (or whatever other key combination you want to use as your Alt+Dot)
  5. In Action select Send Hex Code
  6. In the small text field that appears underneath type: 0x1b 0x2e
  7. Press the OK button to create the new key mapping
  8. Enjoy ;-)
1
  • yeah, who's using the default terminal these day? Thanks May 6, 2021 at 8:23
1

In my preferred terminal (iTerm2) there is an option on the Preferences dialog's Profiles tab in the Keys sub tab to set one or both Option keys to "Esc+" enter image description here

Setting the option to Meta did not give me the expected result but setting it to Esc+ gave me the exact behavior I expect. Hold the left option key and press period as many times as I need to to get the text I want from my history!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .