Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Work out which commands I use in Fish the most
'use strict'
const fs = require('fs')
const path = require('path')
const os = require('os')
const df = require('data-forge')
// Read the history of commands executed in the Fish shell and work out which
// are used the most!
const filePath = path.join(os.homedir(), '.local/share/fish/fish_history')
const historyFile = fs.readFileSync(filePath)
// Entries in the Fish shell history are stored in the following format:
// - cmd: git add package-lock.json
// when: 1527650946
// paths:
// - package-lock.json
// We only care about the initial part of the command here
const cmdRegex = /^- cmd: ([a-z_\-0-9]+).*$/i
new df.DataFrame(historyFile.toString().split('\n'))
.where(line => cmdRegex.test(line))
.select(line => cmdRegex.exec(line)[1])
.groupBy(cmd => cmd)
.select(group => ({ name: group.first(), count: group.count() }))
.orderByDescending(cmd => cmd.count)
.forEach(cmd => {
console.log(cmd.name + ': ' + cmd.count)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment