4 ms·
A favorite that I use very frequently is `esc .` (or maybe `alt+.`), which "up-arrows" through the last token of your command history. mkdir /tmp/foo #
by ramses0 3y ago
A favorite that I use very frequently is `esc .` (or maybe `alt+.`), which "up-arrows" through the last token of your command history.
mkdir /tmp/foo # /tmp/foo is the last token
cd <esc .> # cd /tmp/foo is the result
touch bar.txt # bar.txt is the last token
vim <esc .> # vim bar.txt is the result
:wq # ...exit vim
ls <esc .><esc .> # two tokens previously gets you to "ls /tmp/foo"
It's kindof like an "it" variable. Another "trick" I use often is `fc` and related: `help`, and the humble `#` comment character.
# `fc` - mnemonic: "Fix Command"
cp -rv /tmp/foo /tmp/bar # ...but don't press enter!
<ctrl-a> # <enter> # beginning of line, comment it out, commit to history
fc # now you can edit to your heart's content
man fc # it's a builtin
help fc # and builtins have documentation via "help"
man cd # going to the "builtins" manpage is not that useful
help cd # ...did you know cd supports -L and -P ??
...and a slightly related vim-tip in the context of "fc". `<shift-v>:!ls /tmp` is an interesting technique to "bring in" the output of a command for editing.
Example:
git commit -m "whoops, don't commit just yet (don't press enter)"<ctrl-a>#<enter>
# this leaves you with a `#git commit -m ...` comment in your history
fc # fix command
$VIM => o<esc>V:!git status --porcelain | grep -v ^??
# within vim you're using visual selection with `:!...` to "filter" the selection (a blank line) with an external command
# in the case of `V:!ls` you're filtering the current line through `ls`
# ...which is basically running `ls` (give you a list of files)
# the more complicated `git status --porcelain | grep ...`
# ...lets you "pull in" the output of that command
# ...(with full pipeline support), and then compose
# ...a more complicated (and accurate) command than
# ...messing around on the command line. eg:
git restore --staged FILE1.txt FILE2.txt && git commit -m "...etc..."
Usually you use something like `:!make` to execute the `make` command from vim, but this is an "abuse" of visually-selecting something to filter and replacing the selection with the output. The selection is passed on stdin to the executed command, eg: `V:!tr [a-z] [A-Z]` would be a terribly inefficient way to filter the current line through "tr / uppercase". `ls` as far as I know doesn't really do anything with stdin, so you're basically filtering/replacing the visual selection with the output of `ls` (or any other command), which is great to give you access to accurate filenames or whatever info you can get from a command (`curl`, `git`, `ls`, `apt search ...`, `pip list`, ...etc...).
I find using `fc` with `EDITOR=vim` far superior to having the command line (readline) set to "vim/vi" mode, as with `fc`, I'm able to context-switch far easier and have a whole document as a palette / canvas to be able to compose complicated commands.
TL-DR: use <esc .> (escape followed by a period) to access "just the last token/word", and use `# commented-out-command.sh` along with `fc` to have full editing power to accurately compose (or fix) a complicated command.