when I type commands on a terminal, I usually find myself trying to use it like a text editor, I know this is not how it is supposed to be used, but I recently was watching a talk on youtube and the presenter did something interesting at 17:25 https://youtu.be/f5phsX4VUOU?t=17m25s. It seems like a mini text editor popped out --> he edited the text --> closed it and the changes were reflected back on terminal. How do I do that? is it some sort of plugin or a shortcut?
1 Answer
If you want to edit a command from your history, the fc
command can help.
First, set the editor to one of your choice via the FCEDIT
variable. To use TextEdit.app, do this:
$ FCEDIT='open -nW'
Then run some commands:
$ echo "this command neets editing"
Later, run fc
:
$ fc
TextEdit should open up, with echo "this command neets editing"
in a new window. Change "neets" to "needs" then save the file and quit the editor. Once the editor quits, the modified command will run, displaying the corrected text.
If you want to edit the active command, invoke edit-and-execute-command
. In my shell, this is bound to Ctrl-x Ctrl-e
. It depends on the value of the EDITOR
environment variable.
Find the key binding in your shell:
$ bind -q edit-and-execute-command
edit-and-execute-command can be invoked via "\C-x\C-e".
That's Ctrl-x Ctrl-e
. Set the EDITOR to something you like. I use emacs
, but let's use TextEdit again since this is OS X.
$ EDITOR='open -nW'
$ echo "this command neets editing" #now type ctrl-x then ctrl-e
Your editor will open up. Edit the command and save the file. When you close your editor the new command will run.
For more information about fc
, run help fc
. For more information about edit-and-execute-command
, run man readline
. For more information on bind
, run help bind
.
-
I wish there were at least one cli that allows you to work like a simple text editor, its 2016 and I don't think asking to include simple cursor support from a mouse input or even normal text editing capabilities from the keyboard is resource intensive anymore. but thanks– DavidAug 21, 2016 at 3:47
-
1I suppose it depends on what you call "simple". If you run
M-x shell
in emacs, you can get a shell inside an editor. It's ok for some tasks, but gets messy if you invoke something that, for example, has its own interpretation of the cursor keys. You can do some pretty significant editing within most shells, but the keyboard commands are a bit obscure. They predate the great standardization across operating systems that happened in the mid 90s.– EricAug 21, 2016 at 12:35 -
1In Bash's default configuration, Ctrl-a takes you to the start of a line and Ctrl-e takes you to the end. Ctrl-k cuts everything from the cursor to the end of the line. Ctrl-y pastes the text you last cut. These are also the key bindings that work in emacs. Mouse support in a raw terminal (not running in a window manager) is usually limited to copy-and-paste, with the paste location determined by the text cursor. The capabilities are there, but they will be very unfamiliar to people who first learned on more modern user interfaces.– EricAug 21, 2016 at 12:40
-
1It looks like OS X's terminal supports cursor positioning by mouse. Option-click does it. Source: osxdaily.com/2014/03/06/place-cursor-at-mouse-position-terminal– EricAug 21, 2016 at 12:48
-
Thanks Eric. I was just wondering why I don't see any newer clis like iterm integrate native cursor support or normal keyboard bindings. I think this would greatly improve the user experience. Maybe this will be my next project :)– DavidAug 21, 2016 at 18:58