Posts

printf - Printing with format

Image
printf formats and prints its arguments according to format, a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive value.   To Get Zero Leading Number $ printf "%03d" 1 $ 001   To Print Line by Line Including Speces in Terminal Interactive script in terminal sometime requires to clear terminal's face and print line by line without flickering the terminal's face. In that case, using printf "%-*.*s%s\n" "$MINCOL" "$MAXCOL" "string of the line" is a trick. For breaking down, %-*.*s is for width i.e., minimum number of columns and maximum number of columns. In above example, "$MINCOL" and "$MAXCOL" are variables of minimum and maximum...

rebase or no-rebase for git pull

Image
In the following scenario, the safest option is using git pull --rebase or setting configuration by git config pull.rebase true . Analysis of the scenario Current state on Remote : File aaaaa.txt exits and file bbbbb.txt does not exist yet. Current state on Local : File aaaaa.txt does not exist and file bbbbb.txt has been created. In above scenario and local repository on the computer, git push will fail with the following message: To https://codeberg.org/minsoehan/ttmy.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://codeberg.org/minsoehan/ttmy.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push -...

Command Notes for Script

These are command notes for scripting in linux. Removing Duplicate Lines Removing duplicate lines can be done by sord and uniq command. $ sort textfile | uniq Above approach is sorting first and filter unique line. The problem is that uniq command only check next line is identical to current line and skip if so. Therefore, sorting first is required. What if sorting is not desired? In case of that, awk command can be used. The clever part is '!_[$0]++' in below command. $ awk '!_[$0]++' textfile Using Variable in awk and sed Using variable in awk command line is different from others. In below command -v VAR=apple '$0 ~ VAR' is key. $ awk -v VAR=apple '$0 ~ VAR' textfile It will print all line including apple that matched to VAR by awk. The option -v means assigning variable and $0 means the whole line. You can print specific field like $3 or $1 , etc. But using variable in sed command is different. Us...

ANSI Escape Code

The escape sequence like ^[[A or \e[A are part of a terminal's ANSI Escape Codes which represent keyboard input or control sequences. For arrow keys, they typically correspond to specific sequences sent by the terminal when keys are pressed. To see the escape sequence code of a key, use showkey -a in a terminal and press key you want. It will show like below on pressing arrow keys. Note: Ctrl-D will terminate the program , Ctrl-C would not work. [user@hostname ~]$ showkey -a Press any keys - Ctrl-D will terminate this program ^[[A 27 0033 0x1b 91 0133 0x5b 65 0101 0x41 ^[[B 27 0033 0x1b 91 0133 0x5b 66 0102 0x42 ^[[D 27 0033 0x1b 91 0133 0x5b 68 0104 0x44 ^[[C 27 0033 0x1b 91 0133 0x5b 67 0103 0x43 ^D 4 0004 0x04 [user@hostname ~]$ And cat -v also can be used. It will show like below on pressing arrow keys. [user@hostname ~]$ cat -v ^[[A^[[B^[[D^[[C See also Keyboard Input for Arch Wiki To continue about colorizing output in terminal usi...

Simulating read -n1 of bash in dash

Simulating read -n 1 of bash in dash is to use stty command in linux. # using stty -icanon time 0 min 1 echo -n "Type your character: " stty -echo -icanon time 0 min 1 CHA=$(head -c 1) stty sane echo "You typed: "$CHA"" # OR using stty -cbreak echo -n "Type your character: " stty -echo -cbreak CHA=$(head -c 1) stty sane echo "You typed: "$CHA"" # using stty -icanon time 0 min 1 # using dd bs=1 count=1 echo -n "Type your character: " stty -echo -icanon time 0 min 1 CHA=$(dd bs=1 count=1 2>/dev/null) stty sane echo "You typed: "$CHA"" # OR using stty -cbreak # and using dd bs=1 count=1 echo -n "Type your character: " stty -echo -cbreak CHA=$(dd bs=1 count=1 2>/dev/null) stty sane echo "You typed: "$CHA"" Above approaches work perfectly. See stty man page for more. It can not be handle with the keys like Arrow Keys or Page Up/Down keys, see: ANS...

HTML Email in Gmail

Image
Writing HTML Email in Gmail is easy but tricky. There are also browser extensions providing that functionality. For Example, HTMail: Insert HTML into Gmail, Outlook and Yahoo Mail extensions for chrome browser.   HTML Email in Gmail However, the simplest and cleanest way is inserting HTML manually. It is easy and tricky. This howto is only for Gmail. Follow below steps: Press Compose for new Email Type snippet of text to be replaced following by pressing Enter for new line. Note: pressing Enter for newline is very important so browser's HTML Editor in Developer Tools would create separate div tag otherwise pressing Send in Gmail will wipe out all content and reset to the snippet of text that inserted previously. In the browser's Developer Tools, Right Click on the highlighted code block and select Edit As HTML . Then insert your HTML div block only. Your HTML Email will now appear in Gmial's new email compose box. Send it. ...

My Neovim Setup

Neovim is a fork of Vim aiming to improve the codebase, allowing for easier implementation of APIs, improved user experience and plugin implementation. Setting Up Neovim This is my neovim setup of personal preference. The setup or configuration is all included in ~/.config/nvim/init.lua file. Except native user settings, the following plugins are installed: savq/paq-nvim plugin manager written in Lua. hrsh7th/nvim-cmp auto-completion. neovim/nvim-lspconfig language server protocol (LSP). L3MON4D3/LuaSnip snippet engine. rafamadriz/friendly-snippets snippets. nvim-treesitter/nvim-treesitter treesitter for better syntax highlighting colors. marko-cerovac/material.nvim material colorscheme. nvim-telescope/telescope.nvim telescope search tool. nvim-tree/nvim-tree.lua side bar file explorer. nvimdev/indentmini.nvim indent line. windwp/nvim-ts-autotag autoclose and autorename html tag. The above plugins ar...