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.