Reverse find

2 snippets across 2 stacks - Bash & Linux, JavaScript

Also written as reverse search

SHBash & Linux

Reverse History Search

SH · Shortcuts & Productivity
Syntax
Ctrl+R, then type to search
Example
# Press Ctrl+R, then type 'docker'
(reverse-i-search)`docker': docker compose up -d --build

Note Press Ctrl+R again to cycle through older matches. Enter executes the found command. Ctrl+G or Esc cancels the search. This is one of the biggest timesavers in daily terminal work.

JSJavaScript

findLast() and findLastIndex()

JS · Modern Features
Syntax
arr.findLast(callback)
arr.findLastIndex(callback)
Example
const transactions = [
  { type: "credit", amount: 500 },
  { type: "debit", amount: 200 },
  { type: "credit", amount: 300 },
  { type: "debit", amount: 150 },
];

const lastCredit = transactions.findLast(t => t.type === "credit");
console.log(lastCredit); // { type: "credit", amount: 300 }

const lastCreditIdx = transactions.findLastIndex(t => t.type === "credit");
console.log(lastCreditIdx); // 2

Note ES2023. Searches from the end of the array. More readable and efficient than reversing first or using a manual reverse loop.

Frequently asked questions

How do you reverse find?
This task is covered in 2 stacks on this page: Bash & Linux, JavaScript. The "Reverse History Search" snippet in Bash & Linux uses `Ctrl+R, then type to search`.
Which command does the Bash & Linux example use?
The "Reverse History Search" snippet uses `Ctrl+R, then type to search`, from the Shortcuts & Productivity section of the Bash & Linux cheat sheet.
Which stacks cover "reverse find" on this page?
Bash & Linux, JavaScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Reverse History Search": Press Ctrl+R again to cycle through older matches. Enter executes the found command. Ctrl+G or Esc cancels the search. This is one of the biggest timesavers in daily terminal work.