Sunday, November 25, 2018

Shell configuration of macOS' Terminal

I got a clear picture of shell configuration of macOS' Terminal.app by reading the following article.

https://scriptingosx.com/2017/04/about-bash_profile-and-bashrc-on-macos/

In short,

  • Conventional Linux/Unix
.bash_profile for login session, and .bashrc for without login session.
  • macOS
Terminal.app runs .bash_profile, despite that no login required!
More details are summarized in the following tree.

  • /etc/profile
    • /etc/bashrc
      • /etc/bashrc_Apple_Terminal
  • if it exists: ~/.bash_profile
    • when ~/.bash_profile does not exists, ~/.bash_login
    • when neither ~/.bash_profile nor ~/.bash_login exist, ~/.profile
  • ~/bash_profile can optionally source ~/.bashrc

Saturday, November 24, 2018

Vim editor commands

  1. To go to specific line, type line number after colon.
  2. If it is before opening the document, type like "vim +160 test.txt" to open and show the 160th line of the document.
  3. To edit multiple lines, select multiple lines and use substitute command: https://www.ostechnix.com/comment-multiple-lines-vim-editor/
  4. To open a file with cursor positioned at a specified line, do as in "vi +36 test.txt" to go to line 36 of "test.txt" file.
  5. Insert variable text in each line
  6. https://unix.stackexchange.com/questions/8101/how-to-insert-the-result-of-a-command-into-the-text-in-vim
  7. https://vi.stackexchange.com/questions/3109/insert-an-incrementing-number-to-each-line-in-either-a-selection-or-a-match
  8. line top is '/^' and line end is '/$'.
  9. several vim commands:
  10. https://vim.fandom.com/wiki/Search_and_replace
  11. You can use :%s/.\{1}$// to delete 1 character off the end of each line.
  12. To delete first top 2 spaces of all lines, type ":%s/^  //". If you want to delete one or more spaces then ":%s/^ +//".



  1. To save an editing file; the save path is relative to what is shown by ":pwd", and is changed by ":cd":
  2. https://stackoverflow.com/questions/4980168/how-to-save-as-a-new-file-and-keep-working-on-the-original-one-in-vim

  1. To search for a word in a current document, type "/" and the word(":" unnecessary).
  2. To replace characters only in a selection, type "¥%V" after usual "s" command: ":s¥%Vtarget/replacement/g"
  3. To input Windows' line ending which displayed as "^M", press Ctrl-v Ctrl-m.
  4. To show the number of lines in the opened file:
    :echo line('$')
  5. To break
    :g/^\$/-1j
    https://unix.stackexchange.com/questions/327305/how-do-i-remove-newline-character-from-lines-preceding-a-particular-pattern/329238#329238?newreg=f56f163962ec4ab69405f93c2552e53e
  6. To run vim commands without opening a file in console, use -c option:
    vim YOURFILE -c 'g/{/-1j' -c ":sav $OUTFILE" -c 'q'

On Virtual Environment of Python

Different meanings of "virtuality"

I had been confused with several different packages for realizing "virtual environments" of Python, before finding the following clear article.

https://medium.freecodecamp.org/manage-multiple-python-versions-and-virtual-environments-venv-pyenv-pyvenv-a29fb00c296f



In short,
  • "venv" can create multiple different environments on a single python version, so that one can have different sets of packages installed for each environment.
  • "pyenv" makes it possible for a machine to have different python versions(>=3.3) installed.

So, if one wants to have different environments with various python versions, you need "venv" and "pyenv"; Note that "venv" is included in main python bundle.

venv usages

To create a new virtual environment with "venv" just run python with options and parameters as "-m venv new_environement_path".
I found someone advocating a nice practice to name the new environment's path simply "venv" and to have it under the project directory which uses the environment.

https://qiita.com/fiftystorm36/items/b2fd47cf32c7694adc2e

To activate a virtual environment
$ source path_to_your_virtual_environement/bin/activate
To deactivate
$ deactivate



  • To see which python versions are available online(not on the local machine)
    pyenv install --list
  • To install python version 9.9.9
    peen install 9.9.9



Memos on shell commands


  1. To see which configuration file the shell on your Mac is using, just type "echo $SHELL" in the terminal.
  2. To show all environmental variables: printenv
  3. Check the file type: file -I filename
  4. To see the symboilc link, use "-la" option of ls command, i.e. ls -la.
  5. tee command "tee command reads the standard input and writes it to both the standard output and one or more files.": https://www.geeksforgeeks.org/tee-command-linux-example/
  6. The contents enclosed by a pair of back-quotation "`" will be (together with back-quotations themselves) replaced by the output of the command within.
    So, if one wants to output a log file giving its name datetime part, he/she can write:
    log_'date +%Y-%M-%'
    (not verified on macosX's terminal)
  7. To copy multiple files using cp command, use like:
    cp directory/{file1, file2} or you can even do like file{1,2}.
  8. Similar sequential command; the following will produce folder_1 to folder_9:
    mkdir ./folder_{1..9}
  9. "clear" to clear all terminal output.
    (not verified on macosX's terminal)
  10. memory status check
    ps aux --sort -rss
  11. Loop variables
  12. for databaseName in a b c d e f; do
      # do something like: echo $databaseName
    done 
  13. Strings enclosed by single quotes will be treated literally, while those by double quotes will be expanded at variables contained in it:
    str=efg
    sed "s/def/$str/" somefile
  14. For sed, we can use "|" as separator instead of "/".
  15. To overwrite file content by sed, we need "-i" option to let sed take a backup:
    sed -i.bak s/ABC/DEF somefile
    [This will produce "some file.bak"]
  16. To save a sed output, piping the result to tee as(https://unix.stackexchange.com/questions/283407/can-sed-save-its-output-to-a-file):
    sed bla bla bla | tee file_name
    or
    sed bla bla bla > file_name
  17. To do multiple substitutions at one time, use throw replacement conditions with "-e" option as in:
    sed -e 'condition1' -e 'condition2'
  18. Show ls command's output as one file per one line:
    either "ls -1" or "ls | cat"
  19. String's length in shell script:
    echo ${#str}
    where str is variable defined in advance.
  20. String replacement: ${str[@]/be/eat} will replace first occurrence of "be" with "eat".
    To replace the pattern repeatedly, double the first slash: ${str[@]//be/eat}
    https://www.learnshell.org/en/Basic_String_Operations
  21. To check if a bash variable is empty:
    if [ -z "$var" ];
    https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/
  22. To parse json files, use jq command:
    https://linuxconfig.org/how-to-parse-a-json-file-from-linux-command-line-using-jq
    Use -r(or --raw-output) option to remove quotations.
  23. In some cases of piping, one need additional command such as xargs:
    ls _directory_ | xargs mkdir
    will make folders whose names match the result of ls command.
  24. To strip \n line feed characters,
    tr -d '\n' < yourfile.txt
  25. Put the pattern in single quotes and enable Extended Regular Expression support with -E.
  26. In sed's substitution, \n where n is any number points to n-th group(enclosed by round brackets) in the pattern expression to be replaced.
    sed 's:start_(a.*z)_end:\1:'
    will replace "start_(a****z)_end" with (a****z)
  27. To insert a new line by sed, one can use '$'\n''
    sed 's:PATTERN:\'$'\n'':' file
    https://stackoverflow.com/questions/6111679/insert-linefeed-in-sed-mac-os-x/24299845#24299845
  28. To count the total number of lines in a file
    wc -l file
    https://devconnected.com/how-to-count-files-in-directory-on-linux/
  29. To show 5 lines(in the 'B'efore text) that are at and after a line matching 'REGEX':
    grep -B 5 REGEX
  30. To check if a file exists,
    if [ -f FILEPATH ]; then SOME_ACTION...
    or
    [ -f FILEPATH ] && SOME_ACTION
    Symbol "[" is an alias for "test" command.
  31. Major jq commands summary:
    https://www.howtogeek.com/529219/how-to-parse-json-files-on-the-linux-command-line-with-jq/
  32. To get all keys which are arranged directly under the top round bracket, or on the top of the structure.
    jq '. | keys' JSON_FILEPATH
    For keys that are contained in an item "item1",
    jq '.item1 | keys' JSON_FILEPATH
  33. To count the number of characters of a string
    echo -n $YOUR_STR | wc -m
    -n option is to strip new line char.
  34. To show a character in a certain position,
    echo "SAMPLETEXT" | cut -c3
  35. To check the bit depth of an image:
    https://superuser.com/questions/148661/how-can-i-find-out-if-a-png-file-is-24-bit-color-or-32-bit-color-on-mac-os-x
  36. check the size of a folder
    du -hs <path-to-file>






Writing a post in Markdown in Blogger

Following articles are helpful. * [【令和最新版】Blogger で Markdown を用いて記事を書く方法](https://qiita.com/hoge_fu-ga/items/267ea5ad4f8c120eaeb8) * [Bl...