Search This Blog

Showing posts with label hints. Show all posts
Showing posts with label hints. Show all posts

Compress sparse files preserving their holes

use -S option for tar
tar -Sczvf sparse_file.tar.gz sparse_file

Find FIFO (named pipe) in Linux

 find ./ -type p 

Find sparse files in Linux

find ./ -type f -printf "%S\t%p\n" | awk '$1 < 1.0 {print}'

find ./ -type f -printf "%S\t%p\n" | awk '$1 < 1.0 {print $2}'



To find and remove the sparse files:
find ./ -type f -printf "%S\t%p\n" | awk '$1 < 1.0 {print $2}' | xargs rm -f



To find and compress the sparse files:
find ./ -type f -printf "%S\t%p\n" | awk '$1 < 1.0 {print $2}' | xargs -I {} sh -c "tar -Sczvf {}.tar.gz {}; rm -f {}"



Excel custom formatting cells for file sizes (GB and TB)


[<1000000]0.00," KB";[<1000000000]0.00,," MB";0.00,,," GB"
[<1000000000]0.00,," MB";[<1000000000000]0.0,,," GB";0.0,,,," TB"

Install RSAT Active Directory Administrative Center on Windows 10 (22H2)

Run PowerShell as Administrator and paste the commands below:

$currentWU = Get-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWUServer" | select -ExpandProperty UseWUServer
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWUServer" -Value 0
Restart-Service wuauserv
Get-WindowsCapability -Name Rsat.ActiveDirectory.DS-LDS.Tools -Online | Add-WindowsCapability –Online
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWUServer" -Value $currentWU
Restart-Service wuauserv




see also

Java: remove unsafe html tags


public class HtmlTagRemover {
    public static String removeUnsafeTags(String html) {
        // Define the regular expression pattern to match unsafe tags
        String unsafeTagsPattern = "<(script|iframe|object|embed)[^>]*>.*?</\\1>";

        // Remove the unsafe tags from the HTML string
        String safeHtml = html.replaceAll(unsafeTagsPattern, "");

        return safeHtml;
    }

    public static void main(String[] args) {
        String html = "<b>Safe HTML</b><script>alert('XSS')</script>";
        String safeHtml = removeUnsafeTags(html);
        System.out.println(safeHtml);
    }
}

Install pre-requisites for flake8 (python language server) on Linux

  • Install pip:
    sudo apt install python3-pip
  • Check python version:
    python3 --version
    Python 3.10.6
  • Install virtualenv:
    sudo apt install python3.10-virtualenv

My zsh prompt setup

Add following line to ~/.zshrc:
PROMPT='%F{red}%*%f %F{blue}%1~%f %# '



More details about ZSH prompt: https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html

Setup Neovim and LazyVim

I. Install Neovim




II. Install LazyVim

  • Follow the instructions to install LazyVim: https://www.lazyvim.org/installation
  • Install fd, ripgrep, fzf, lazygit:
    • Mac OS:
      brew install fd ripgrep fzf lazygit
    • Linux:
      apt install fd-find ripgrep fzf lazygit



III. Configuration

  • Install toggleterm.nvim. Create file: ~/.config/nvim/lua/plugins/toggleterm.lua:
    -- ~/.config/nvim/lua/plugins/toggleterm.lua
    return {
      {
        "akinsho/toggleterm.nvim",
        tag = "2.4.0",
        keys = {
          { "<leader>th", "<cmd>ToggleTerm size=15 direction=horizontal<cr>", desc = "Open a horizontal terminal" },
          { "<leader>tt", "<cmd>ToggleTerm<cr>", desc = "Toggle terminal" },
          { "<F7>", "<cmd>ToggleTerm<cr>", desc = "Toggle terminal" },
          { "<F7>", "<cmd>ToggleTerm<cr>", mode= "t", desc = "Toggle terminal" },
        },
        opts = {
          --[[ things you want to change go here]]
        },
      },
    }
  • Set options in ~/.config/nvim/lua/config/options.lua:
    -- ~/.config/nvim/lua/config/options.lua
    vim.opt.relativenumber = false -- Relative line numbers
    vim.opt.shiftwidth = 4 -- Size of an indent
    vim.opt.tabstop = 4 -- Number of spaces tabs count for
    
  • Install nightfox colorscheme. Create file: ~/.config/nvim/lua/plugins/colorscheme.lua:
    -- ~/.config/nvim/lua/plugins/colorscheme.lua
    return {
      -- add nightfox colorscheme
      { "EdenEast/nightfox.nvim" },
    
      -- Configure LazyVim to load nightfox
      {
        "LazyVim/LazyVim",
        opts = {
          colorscheme = "carbonfox",
        },
      }
    }



See also

Mac OS: fix duplicated Open With entries

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user;killall Finder;echo "Open With has been rebuilt, Finder will relaunch"



see also

Compile Neovide from source on Mac OS

  • Install the latest version of Rust.
  • Install CMake. Using homebrew:
    brew install cmake
  • Check out the source code:
    git clone https://github.com/neovide/neovide
  • Compile and install the binary: neovide into ~/.cargo/bin:
    cd neovide
    cargo install --path .
    The resulting binary is to be found under ~/.cargo/bin.
  • In case you want a nice application bundle:
    cargo install cargo-bundle
    cargo bundle --release
    Copy ./target/release/bundle/osx/neovide.app to ~/Applications and enjoy.



See also

NeoVim: Clean Up Everything (Config, Plugins, Cache ...)

# linux/macos (unix)
rm -rf ~/.config/nvim
rm -rf ~/.local/share/nvim
rm -rf ~/.local/state/nvim
rm -rf ~/.cache/nvim




# windows
rd -r ~\AppData\Local\nvim
rd -r ~\AppData\Local\nvim-data



see also

Use GNU grep to count the length of substring between two words

Note: the command below requires GNU grep, it is available on Linux by default. On Mac OS, you need to install it manually using
brew install grep
and command is called ggrep instead of grep.


Get the substring between two words

echo "<person><name>abc</name></person>" | grep -o -P '(?<=<name>).*(?=</name>)'

Output should be:
abc



Count the length of substring

echo "<person><name>abc</name></person>" | grep -o -P '(?<=<name>).*(?=</name>)' | wc -c

Output should be:
3

Count substring lengths for each line

cat multi-lines.txt | while read line; do echo $line | grep -o -P '(?<=<name>).*(?=</name>)' | wc -c; done




see also

Use grep to count number of occurs of a specific element in a single line XML text

For example, I can input.txt below, three lines of XML text, I want to count the number of id elements for each line:
<result><id>1</id><id>5</id><id>7</id></result>
<result><id>71</id></result>
<result><id>15</id><id>33</id></result>



I use grep -o:
cat input.txt | while read line; do echo $line | grep -o '<id>' | wc -l; done



The output should be:
3
1
2

Regular Expression Capture Groups

  • Capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap[1]);
    }
    
    
  • Named capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(?<word>\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap["word"]);
    }
    
    

Enable code syntax highlighting in blogger site using highlightjs

  • Template -> Edit HTML
  • Insert the following inside <head> tag:
        
        <!-- BEGIN: Syntax Highlighting with highlight.js -->
        <link crossorigin='anonymous' href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/agate.min.css' integrity='sha512-wI7oXtzNHj/bqfLA3P6x3XYbcwzsnIKaPLfjjX8ZAXhc65+kSI6sh8gLOOByOKImokAjHUQR0xAJQ/xZTzwuOA==' referrerpolicy='no-referrer' rel='stylesheet'/>
        <script crossorigin='anonymous' integrity='sha512-bgHRAiTjGrzHzLyKOnpFvaEpGzJet3z4tZnXGjpsCcqOnAH6VGUx9frc5bcIhKTVLEiCO6vEhNAgx5jtLUYrfA==' referrerpolicy='no-referrer' src='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js'/>
        <script>hljs.highlightAll();</script>
        <!-- END: Syntax Highlighting with highlight.js -->
        <!-- BEGIN: CSS for code in pre and code -->
        <style type='text/css'>
            pre {
                font-family: monospace;
                font-size: 1 em;
                background: #333;
                color: #33cc33;
                tab-size: 4;
                padding: 5px;
                border: 1px dotted grey;
                border-radius: 5px;
                line-height: 2.0;
                overflow-x: auto;
                white-space: pre-wrap;
            }
    
            code {
                white-space: pre-wrap;
                overflow-x: scroll;    
                font-family: monospace;
                font-size: 1em;
            }
        </style>
        <!-- END: CSS for code in pre and code -->
        
        
  • Click save icon to save the template
  • Use it in your blog post:
    <pre><code class="language-html">...</code></pre>



See also

Customize AstroNvim

Enable user customisation

cp -r ~/.config/nvim/lua/user_example ~/.config/nvim/lua/user



Install new plugins

nvim ~/.config/nvim/lua/user/init.lua

  plugins = {
    init = { 
      -- You can also add new plugins here as well:
      -- Add plugins, the packer syntax without the "use"
      { "bryant-the-coder/astron.nvim" }, -- astron colorscheme
    },
  },

:PackerSync



Change colorscheme

nvim ~/.config/nvim/lua/user/init.lua
 -- Set colorscheme to use
 -- colorscheme = "default_theme",
 colorscheme = "astron",



Disable relativenumber option

nvim ~/.config/nvim/lua/user/init.lua
relativenumber = false,



See also

Migrate git repository to GitLab using the command line

  • Create a new EMPTY repository in GitLab. (Note: You MUST uncheck "Initialize repository with README")
  • Clone from the source remote repository (from e.g. BitBucket, Github):
    git clone --mirror git@bitbucket.org:path/to/REPO.git
  • Push the local repository to GitLab:
    cd REPO.git
    git push --mirror git@gitlab.com:path/to/REPO.git



see also