2012-06-12 13 views
77

Solo una domanda per migliorare le mie abilità bash. Faccio sempre questo:Usando la cronologia di bash per ottenere un comando precedente, copialo e poi "eseguilo" ma con il comando commentato

$ history | grep some_long_command 

... 
... 
123 some_long_command1......... 
124 some_long_command2......... 
... 

posso quindi eseguire il comando il comando che ho trovato facendo:

!123

Tuttavia, spesso mi voglio fare questo:

some_long_command1foobar 

ie cambia il comando prima di eseguirlo. Si può usare bash per eseguire questo comando:

#some_long_command1

in modo che viene commentato.

Quindi non devo usare il mouse per evidenziare il comando, modificarlo e quindi eseguirlo (posso solo usare la tastiera - più veloce).

Suppongo di poter scrivere uno script per farlo ma potrebbero esistere già delle funzionalità incorporate in qualche luogo ....?

Grazie.

+4

Il modo più semplice: mettete 'bind Space: magic-space' nel vostro profilo bash. Quindi, premendo 'Spazio' si espande'! 123' in 'some_long_command1'. – Blaz

+0

@courteous: Grazie! Quello era nuovo per me. Molto utile. – marlar

risposta

134

Suggerirei invece di utilizzare il comando di cronologia, si utilizza ctrl+r e si inizia a digitare il comando. Quando si preme un tasto freccia come se si volesse modificarlo, il riconoscimento del completamento automatico scomparirà e sarà possibile modificarlo prima di eseguirlo.

UPDATE: anche, se si vuole per scorrere i diversi comandi che contengono la stringa appena digitato, continuare a premere ctrl+r

+6

Come ho fatto a NON saperlo. GRAZIE. Contrassegnerò come risolto quando il "tempo risolto" minimo è passato. – ale

+8

Se si usano le combinazioni di tasti vi ('set -o vi'), è anche possibile usare'/'per cercare la cronologia e modificarla. –

+0

+1 William, anche grande. – ale

10
!123:gs/old/new/ 

Verrà eseguito il comando 123 che sostituisce la stringa 'vecchio' con la stringa ' nuovo'.

+0

+1 Anche una bella risposta. Penso che richieda un po 'più di energia cerebrale rispetto alla soluzione di @ Miqeul, ma comunque interessante. – ale

+0

concordato. 'ctrl + r' è la cosa migliore di sempre! :) –

3

Si può anche mettere

shopt -s histverify 

nel vostro .bash_profile, che fa sì che qualsiasi espansione della storia ad apparire sulla linea di comando senza eseguirlo, che consente di modificare prima di farlo.

17

È anche possibile provare il comando fc per modificare il comando nella cronologia.

WIKI says,

fc è un programma standard su Unix che elenca o modifiche e reexecutes, comandi precedentemente inseriti ad una shell interattiva. fc è un integrato comando nella shell bash; help fc mostrerà le informazioni di utilizzo.


parte reverse-incremental search (Ctrl + R), abbiamo alcuni più bash scorciatoie:

Da man bash:

 
previous-history (C-p) 
    Fetch the previous command from the history list, moving back in the list. 
next-history (C-n) 
    Fetch the next command from the history list, moving forward in the list. 
beginning-of-history (M-<) 
    Move to the first line in the history. 
end-of-history (M->) 
    Move to the end of the input history, i.e., the line currently being entered. 
reverse-search-history (C-r) 
    Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search. 
forward-search-history (C-s) 
    Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search. 
non-incremental-reverse-search-history (M-p) 
    Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user. 
non-incremental-forward-search-history (M-n) 
    Search forward through the history using a non-incremental search for a string supplied by the user. 
yank-nth-arg (M-C-y) 
    Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the "!n" history expansion had been specified. 
yank-last-arg (M-., M-_) 
    Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified. 
shell-expand-line (M-C-e) 
    Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions. See HISTORY EXPANSION below for a description of history expansion. 
history-expand-line (M-^) 
    Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion. 
insert-last-argument (M-., M-_) 
    A synonym for yank-last-arg. 
operate-and-get-next (C-o) 
    Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored. 
edit-and-execute-command (C-xC-e) 
    Invoke an editor on the current command line, and execute the result as shell commands. 
54

In realtà, si posso solo aggiungere :p al comando per stamparlo senza effettivamente eseguirlo. Per esempio:

$ ls -la 
$ !!:p 

stamperà ls -la del comando precedente senza correre, e si può solo premere (su) per trovarlo e modificarlo.

Si può anche fare

!123:p 

per stampare il comando 123 come comando precedente.

+1

thats cool :) grazie –

+1

Questo è quello che sto cercando. : p –

+2

+1 La risposta migliore, in quanto consente ai normali comandi di cronologia di rispondere all'OP: '! mycmd: p'. Se hai attivato i comandi vi in ​​Bash, la risposta di Pursell è altrettanto buona o migliore. Ctrl-r, come menzionato da Miquel, è il migliore. –

5

È possibile passare alla modalità di modifica premendo M-^(opzione-shift-6 su un mac).

Tipo questo:

! 123M-^

E sarete modifica comando # 123. È un po 'come usare ctrl-r, ma iniziare con la sintassi del punto esclamativo.

+0

Can 'M-^'essere ri-legarsi a qualcosa di più facile da usare per le dita? – Blaz

3

Invece di utilizzare il comando history, legare history-search-backward/history-search-forward di scorciatoie da tastiera che possono essere ricordati facilmente (io preferisco PgUp/PgDown). Per fare questo, mettere questo nel file .inputrc:

"<key code>": history-search-backward 
"<key code>": history-search-forward 

Per ottenere <key code>, tipo Ctrl-V <key> nel guscio, e sostituire la partenza ^[ con \e in tutto ciò che era uscita.

Dopo questo è impostato, si può semplicemente digitare some e premere PgUp per ottenere some_long_command. Se hai bisogno di un comando simile some_long_command with_some_other_arg successivamente nella cronologia, puoi scorrere fino a quando non lo raggiungi digitando some e poi premendo ripetutamente PgUp, oppure puoi digitare some, premere PgUp, spostare il cursore su dove i due comandi inizia a differire, digita alcuni caratteri e premi PgUp ancora una volta. Questa capacità di sfogliare/differenziare rapidamente tra comandi simili rende a mio avviso uno strumento molto più comodo di Ctrl-R.

1

Si può wan di provare "scatola suggeriscono" -come storia https://github.com/dvorka/hstr - si legge la storia Bash e permette per la navigazione veloce.

hh

Per ottenere l'ultimo comando è sufficiente digitare hh, passare al comando e utilizzare freccia destra per scarica su riga di comando (dove è possibile modificare e/o aggiungere commenti).

0

^p per ottenere l'ultimo comando digitato in UNIX/Solaris

0

Mettere

alias r = 'fc -s'

nel .bashrc (dir casa) allora si può solo digitare in

r <whatever> 

al prompt dei comandi e si eseguire una copia dell'ultimo <whatever> di comando (stesse params) che è in y la nostra storia. basta premere la freccia per vedere cosa hai eseguito se senti il ​​bisogno.

Problemi correlati