23 May 2022

How to find a pattern in lines and append something to those lines?

https://unix.stackexchange.com/questions/543451/vi-finding-a-pattern-in-lines-and-add-at-the-end-of-those-lines


How to delete all the lines which contain a pattern in vim?

https://vim.fandom.com/wiki/Delete_all_lines_containing_a_pattern



The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.

For example, to delete all lines containing "profile" (remove the /d to show the lines that the command will delete):

:g/profile/d

More complex patterns can be used, such as deleting all lines that are empty or that contain only whitespace:

:g/^\s*$/d

To delete all lines that do not contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:

:g!/^\s*"/d

Note that g! is equivalent to v, so you could also do the above with:

:v/^\s*"/d

The next example shows use of \| ("or") to delete all lines except those that contain "error" or "warn" or "fail" (:help pattern):

:v/error\|warn\|fail/d

22 May 2022

How to pass args to a Python program when running it?

To learn how, find the Python Doc/howto/ directory and open the file 'argparse.rst', e.g. 

~/Downloads/Python-3.10.0/Doc/howto/argparse.rst

14 May 2022

How to get a list of the members of an email distribution list, and a list of the members of an office 365 team?


(1) Email distribution list

Go to office.com > 9 dots > admin > show all > exchange > dashboard_recipients_groups.

Click to select a group, e.g. cics.staff. Click 'Edit' > membership. Ctrl-a Ctrl-c

Ctrl-v in a text editor.

(2) Team

Go to the team, e.g. team_staff. Wait for quite a while, and need repeat scrolling up and down, until all the members are displayed, before mouse-dragging all the members, and Ctrl-c

Ctrl-v in a text editor.

How to read a text file in Python?

https://stackoverflow.com/questions/53919299/python-how-to-properly-use-readline-and-readlines

There are 4 methods.


Method #1 

(Iterate lines by referencing the file pointer)

with open(INFILE, 'r', encoding='utf_8') as f:
    listOfLines = []
    for line in f:    # (Type of 'f' is '_io.TextIOWrapper. 
                      # Yet we can use 'for line in' 
                      # for accessing its lines!)
        listOfLines.append(line.rstrip())  # rstrip() strips off                                               # newline characters

The above method is the best and fastest way, compared with read(), readline(), readlines(), to access a text file. It occupies the least memory at any time. In addition, you need not handle the end-of-file issue as with readline().


Method #2 

(readline())

with open(INFILE, 'r', encoding='utf_8') as f:
    listOfLines = []
    while True:
        if not line:
            break
        line = f.readline()
        listOfLines.append(line.rstrip())


Method #3

(readlines())

with open(INFILE, 'r', encoding='utf_8') as f:
    listOfLines = []
    for line in f.readlines()
        listOfLines.append(line.rstrip())


Method #4

(read())

with open(INFILE, 'r', encoding='utf_8') as f:
    strOfWhole = f.read()

(The END)


How to duplicate a tab (with the same window layout) in vim?

https://vi.stackexchange.com/questions/3879/duplicate-tab-with-windows


13 May 2022

How to export a list of members of an email group from Office 365?

https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-i-really-export-distribution-list-members/efe355ff-cc64-4235-8615-364bbff75382

Based on my tests and researches, it is not feasible to export distribution list members using UI interface. We can only export the members via Windows PowerShell. To do that, the steps are as follows:

1. Open Windows PowerShell and connect to Exchange Online PowerShell. Here are the commands:

Install-Module ExchangeOnlineManagement (Enter Y to install the module)

Connect-ExchangeOnline (Sign in using admin account and password)

2. Then run the following script to get a csv file about Distribution list.

$Groups = Get-DistributionGroup

$Groups | ForEach-Object {

$group = $_.Name

$members = ''

Get-DistributionGroupMember $group | ForEach-Object {

$members=$_.Name

New-Object -TypeName PSObject -Property @{

GroupName = $group

Members = $members

EmailAddress = $_.PrimarySMTPAddress

}}

} | Export-CSV "C:\DistributionGroupMember.csv" -NoTypeInformation -Encoding UTF8

Here is a screenshot about the result I got:

Image


10 May 2022

How to install vim plugin from github?

e.g.1  install ctrlp from github


Clone the plugin from the git repository
To clone from github:

git clone https://github.com/kien/ctrlp.vim.git ~/.vim/bundle/ctrlp.vim

2
Update your vimrc
Add the following to your vimrc file:

set runtimepath^=~/.vim/bundle/ctrlp.vim

To edit vimrc, type:

vim ~/.vimrc

3
Restart vim

============================

e.g. 2 install airline from github


git clone https://github.com/bling/vim-airline ~/.vim/bundle/vim-airline

Put the following line into ~/.vimrc

set funtimepath^=~/.vim/bundle/vim-airline

I got :help airline to work with this command:

:helptags ~/.vim/bundle/vim-airline/doc

Like you, now the help-page displays when I type :help airline.

To add airline and the buffer list, install airline, and then just add this to your .vimrc:

" Enable the list of buffers
let g:airline#extensions#tabline#enabled = 1

" Show just the filename
let g:airline#extensions#tabline#fnamemod = ':t'


Next I needed to replace the tab shortcuts that I’d no longer be using. I did this by adding these to my .vimrc as well:

" This allows buffers to be hidden if you've modified a buffer.
" This is almost a must if you wish to use buffers in this way.
set hidden

" To open a new empty buffer
" This replaces :tabnew which I used to bind to this mapping
nmap <leader>T :enew<cr>

" Move to the next buffer
nmap <leader>l :bnext<CR>

" Move to the previous buffer
nmap <leader>h :bprevious<CR>

" Close the current buffer and move to the previous one
" This replicates the idea of closing a tab
nmap <leader>bq :bp <BAR> bd #<CR>

" Show all open buffers and their status
nmap <leader>bl :ls<CR>
These settings gave me a hybrid approach to how I expected tabs to work in Vim yet it still gave me the same power that comes from understanding and using buffers.



How to split a window and let the new window be on the right or below?

:bel(owright) {command}

e.g. 
:bel sp {filename}
:bel vs {filename}
:bel new {filename}

Understanding buffers, windows and tabs in vim

https://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/


08 May 2022

How to show vertical line in Vim?

https://stackoverflow.com/questions/1919028/how-to-show-vertical-line-to-wrap-the-line-in-vim/3787678#3787678


to display vertical lines

:set colorcolumn=5,9,13,17

Not to display vertical lines

:set colorcolumn=0

Why I love Vim: It’s the lesser-known features that make it so amazing

https://www.freecodecamp.org/news/learn-linux-vim-basic-features-19134461ab85/


How to install and use a vim plugin manager? How to install a vim plugin?

How to install and use a vim plugin manager? How to install a vim plugin?

https://github.com/junegunn/vim-plug#readme


In the above readme file, it asks you to reload .vimrc
How to reload .vimrc?

Setting up Vim for Python

https://dev.to/mrdestructive/setting-up-vim-for-python-ej


How to split window and open terminal in vim?

 https://dev.to/mr_destructive/vim-terminal-integration-4pfp


^w v

To split window vertically. (the same file is opened in the new split window.)


^w l

To go to the newly split windows on the right


:term

To open a terminal. (The window on the right split horizontally, and the terminal window is on the top.)


^w j

To go to the split window below the terminal window


^w c

To close the window below the terminal window


05 May 2022

How to capitalise selected words in vim

https://stackoverflow.com/questions/17440659/capitalize-first-letter-of-each-word-in-a-selection-using-vim

:s/\<./\u&/g

  • \< matches the start of a word
  • . matches the first character of a word
  • \u tells Vim to uppercase the following character in the substitution string (&)
  • & means substitute whatever was matched on the left-hand side
  • g means substitute all matches, not only the first

=======================================================

:help case says:

To turn one line into title caps, make every first letter of a word
uppercase:
    : s/\v<(.)(\w*)/\u\1\L\2/g

Explanation:

:                      # Enter ex command line mode.

space                  # The space after the colon means that there is no
                       # address range i.e. line,line or % for entire
                       # file.

s/pattern/result/g     # The overall search and replace command uses
                       # forward slashes.  The g means to apply the
                       # change to every thing on the line. If there
                       # g is missing, then change just the first match
                       # is changed.

The pattern portion has this meaning:

\v                     # Means to enter very magic mode.
<                      # Find the beginning of a word boundary.
(.)                    # The first () construct is a capture group.
                       # Inside the () a single ., dot, means match any
                       #  character.
(\w*)                  # The second () capture group contains \w*. This
                       # means find one or more word characters. \w* is
                       # shorthand for [a-zA-Z0-9_].

The result or replacement portion has this meaning:

\u                     # Means to uppercase the following character.
\1                     # Each () capture group is assigned a number
                       # from 1 to 9. \1 or back slash one says use what
                       # I captured in the first capture group.
\L                     # Means to lowercase all the following characters.
\2                     # Use the second capture group

Result:

ROPER STATE PARK
Roper State Park

02 May 2022



Input CSV File:

country, population
Usa, 1273
Usa, 4343
Usa, 1240
Uk, 7879
Uk, 3224
Uk, 4342
Tr, 6565
Tr, 7889
Tr, 1980

========================
import csv

index = {}
with open('/tmp/data.csv') as f:
    cr = csv.reader(f)
    next(cr) # skip header row
    for row in cr:
        index.setdefault(row[0], []).append(int(row[1]))

print("['country', 'avgPop']")
for c, v in index.items():
    print("['{}', '{}']".format(c, int(sum(v) / len(v))))


Result:

['country', 'avgPop']
['Usa', '2285']
['Uk', '5148']
['Tr', '5478']

01 May 2022

How to slice a number of columns from a 2-dimensional list in Python?

https://www.codegrepper.com/code-examples/python/python+multiaxis+slicing

# Basic syntax:
[your_list[i][start_col : end_col] for i in range(start_row, end_row)]

# Example usage:
your_list = [[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11, 12, 13, 14, 15]]
# obtain rows 0-1 (inclusive) and cols 2-3 (inclusive)
[your_list[i][2:4] for i in range(0,2)]
--> [[3, 4], [8, 9]]

How to pick up a single column from a 2-dimensional list in Python?

https://stackoverflow.com/questions/30062429/how-to-get-every-first-element-in-2-dimensional-list

a = [[4.0, 4, 4.0], [3.0, 3, 3.6], [3.5, 6, 4.8]]

You can get the index [0] from each element in a list comprehension


>>> [i[0] for i in a]
[4.0, 3.0, 3.5]