Импортировать предыдущие команды в редактируемую команду

244
Jakub

Я играю с zsh, чтобы изменить время модификации изображения в соответствии с атрибутом exif «Date / Time Original».

Для этого я делаю одну команду:

PDATE=$(exiftool -p '$DateTimeOriginal' $PIC | sed 's/[: ]//g') touch -t $(echo $PDATE | sed 's/\(..$\)/\.\1/') $PIC

Я получил несколько изображений с неправильным значением даты / времени оригинала, поэтому эти изображения не были обработаны.

Так что я работаю над получением даты из имени файла изображения, и я получил

for i in `grep -E -o 'IMG\S+jpg' logfile`; do  dte=$(echo $i | grep -E -o '20.' | tr -d '_');  touch -t $dte $i;  done 

где logfile - это файл, содержащий сообщения exiftool с неверным значением атрибута и т. д.

Теперь последняя команда второго фрагмента не работает, потому что мне нужно сделать подстановку sed, как в первом фрагменте.

Мой вопрос: находясь в интерактивном режиме zsh vi, как я могу получить доступ к первому фрагменту в истории, не теряя при этом содержимое текущей команды?

Я бы вообразил редактирование текущей команды в vi (как я могу это сделать, когда я нажимаю символ 'v' в режиме управления zsh vi), перечислив команду истории и выбрав одну из них, и она будет вставлена ​​в строку ниже.

1

2 ответа на вопрос

2
One Other

There is zsh-cmd-architect created specifically to do what you describe. It displays you current command, allows to move blocks of it left and right, and also displays searchable history, from which you can choose blocks of commands there.

0
Jakub

You are using zsh in the vi mode.

To solve your problem we divide the problem in two parts

1. part - print command history lines matching a pattern

For this we create a file ~/bin/history-print-regexp.sh containing:

#!/bin/zsh # command 'regular-expression' number-of-lines(counting from the end of the file) nmbr=15 if [ "$#" -eq 2 ]; then nmbr=$2 fi if [ "$#" -eq 0 ]; then echo "You need at least one argument" echo "Usage command regexp outputlinesCount" fi #tac - reverse print, grep -E - use extended regexp, cut -d ';' use ; as delimiter #and print second field (-f 2), uniq -u print only unique lines tac ~/.histfile | grep -E $1 | cut -d ';' -f 2 | uniq -u | head -n $nmbr 

Now we need to get it working together with your present command. For this we do:

While being in the zsh vi normal mode you press the v button twice and the vi window opens.

In your ~/.vimrc file you put this function

function! ShowHistoryMatching(pattern) 10new exe 'r!' . "history-print-regexp.sh " . a:pattern call cursor(1,1) endfunction :cnoremap ch call ShowHistoryMatching 

Use of this script is, while being in the zsh total vi editor editing your present command you press :ch which is extended to :call ShowHistoryMatching and you add ("pattern") to this and press Enter and the results open in the window above. From there you can easily copy a desired line to your second window.

C-w,q closes a window C-w,j goes to the window below.