This is just mostly a fun note about bash
and readline
, but if you set your EDITOR
environment variable to vim
, hitting Ctrl+x+e will open up the current line in vim
. Further, if you type Meta(Alt/Opt/Esc)+Ctrl+e, bash will perform an in place expansion on the current command line, i.e.:
$ echo $EDITOR # Assuming EDITOR was set
with Meta(Alt/Opt/Esc)+Ctrl+e would become
$ echo vim # Assuming EDITOR was set
with Ctrl+x+e would become
1 echo vim # Assuming EDITOR was set
~
~
/tmp/sometmpbufferfile
Note that upon quitting vim, the contents of the vim buffer are executed on the command line.
These features become very useful for me when I want to do multi-line commands in bash such as for loops or programs requiring here statements, and provides an interesting way to save a bit of command-line history to file for later use.
So to answer the original question, you could also write,
$ This is an example
and then hit Ctrl+x+e to load it up in vim
. Also you could have,
$ $(cat /etc/hosts)
and do Meta(Alt/Opt/Esc)+Ctrl+e then Ctrl+x+e, which would put all of the hosts file on one line and load it up in vim
(probably not the best use of these features--however, the usefulness of these methods can be extrapolated from the few examples discussed here).
Note that I assume that your readline
is set to emacs
mode. If your readline
is set to vim
mode you can easily discover the relevant bindings by using the command:
bind -p
and searching for edit-and-execute-command
or shell-expand-line
, which were respectively associated with the bindings Ctrl+x+e and Meta(Alt/Opt/Esc)+Ctrl+e.
:help stdin
... It's amazing how fast one can find the answers by asking their question of the built in documentation.