Search This Blog

Thursday, November 26, 2020

Getting top contributors of given directory based on commit counts

git log --pretty=format:"%an%x09" dir | sort | uniq -c | sort -nr

git log --pretty option prints the contents of the commit logs in a given format (%an%x09b) for given directory, here %an is placeholder for Author Name and %x09 is placeholder for tab.

Output of first command will be series of author names of commits affecting given directory ordered by date.

Since we are not concerned with order we will just sort the output so that we can easily group the names using uniq -c which along with unique values prints the count.

Now the output is in alphabetical order of Author Names if we want to order by count of commits we can pipe it to sort -nr  where n represents sort by numerical value and r represents sort in reverse order.

Sunday, April 26, 2020

Using set command for temporarily disabling shell history

set is used to change the value of shell attributes and positional parameters, or display the names and values of shell variables.

If you are executing a command which contains your password or some other sensitive details and you don't want it logged in command history you can turn off history logging by

$ set +o history

To check whether history is turned off you can execute
$ set -o | grep history
history         off

You can turn it back on by executing 

$ set -o history

Sunday, February 9, 2020

Linux Command History

How to command history is saved in Linux
Command history is saved in .bash_history file by history command.

Customization :
1 - Location
You can change history file location of by overwriting HISTFILE

2 - Time
By default, history doesn't store the time of command execution you can provide HISTTIMEFORMAT to store it

    # for storing the date in history in %Y-%M-%D %HH:%MM:%SS
    export HISTTIMEFORMAT="%F %T: "

3 - History limit
The default size of history is 500 you can increase by overwriting the following variables

    # .bash_history file events limit
    export HISTFILESIZE=20000

    # history command events limit
    export HISTSIZE=10000

Productivity Hack: Use CTRL + R to cycle through history to reuse previously executed command rather than finding by clicking up arrow repeatedly.

Problem: If you are using multiple ssh or tmux sessions, and CTRL + R won't display other sessions command because history is not dumped in the file unless a session is closed gracefully.
There is also the chance of losing history on abruptly closing sessions.
Hack: http://northernmost.org/blog/flush-bash_history-after-each-command/

    export PROMPT_COMMAND='history -a'

Explanation:
history -a appends the current history buffer in .bash_history file
The value of the variable  PROMPT_COMMAND  is examined just before Bash prints each primary prompt (PS1).
PS1  is the primary prompt which is displayed before each command

    [ash-ishh@xBot ~]$ echo $PS1
    [\u@\h \W]\$

If  PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.

List of possible escape sequences: https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html

Thursday, January 23, 2020

Delete matched line with range in vim

You can use . to refer to the current line.

1) :g/foo/.,+2d will delete line with text foo and 2 lines after it

2) :g/foo/-3,.d will delete line with text foo and 3 lines before it

3) :g/foo/-1,+1d will delete line with foo, one line before it and one line after it

vim go to the definition

In vim rather than searching for the function definition or variable initialization you can type gd in command mode which will go to the local declaration of the word where cursor is type gD if you want to go to the global declaration.