简体   繁体   中英

sending command from bash terminal history to $EDITOR

What would be the easiest way to send previously used command, from bash history to preffered editor, say VIM.

Right now I am using fc -l to see which command I am interested in, then I isolate it by that line fc -l lineno lineno | vim - fc -l lineno lineno | vim - specifying two times lineno to pick only that line, and send it to VIM.

However this is very time consuming, there must be a better way.

Reason for this is not that I want to re-edit the command, to execute it again, because that one I know how to do, pressing CTRL + X + E will send it immediately to vim, and re-execute it on :wq .

What I want, is to document it, maybe change few things here and there, and post it as a gist, which I can directly from VIM using Gist plug-in.

How about moving more of the work to Vim? Say you are interested in the last 50 commands and run the following command:

fc -nl -50 | sed 's/^\t //' | vim -

Now you see all history items and can search through them with / or filter out with :g/bad-line-pattern/d and :v/good-line-pattern/d . If you're used to Vim it's a matter of several key strokes.

After you navigated to the line you need and want to leave only it in the buffer, you could use a shortcut like this one:

nnoremap <leader>f VyggVGp

It should leave only current line in the buffer.

just do this to put previous command in vim, without re-executing it after save the buffer:

fc -ln -1|vim -

or

history -n -1|vim -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM