13 November 2021

vim search patterns

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

created 2001 · complexity basic · version 6.0


When searching in Vim, you enter a search pattern. This tip provides a tutorial introduction to using search patterns.

Finding a whole word

In a program, you may want to search for an identifier named i. However, entering the search /i will find every hit, including the "i" in words like "if" and "while". In a pattern, \< represents the beginning of a word, and \> represents the end of a word, so to find only the whole word "i", use the pattern:

\<i\>

In normal mode, press / to start a search, then type the pattern (\<i\>), then press Enter.

If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word. Vim inserts \< and \> automatically (see searching).

The pattern \<i finds all words that start with "i", while i\> finds all words that end with "i".

Finding duplicate words

Sometimes words are accidentally duplicated in text (like this this). The following pattern finds repeated words that are separated by whitespace (spaces, tabs, or newlines):

\(\<\w\+\>\)\_s*\<\1\>

The pattern searches for \<\w\+\> (word beginning \<, word character \w, one or more \+ word characters, word end \>). That is, it searches for a whole word. It then looks for any amount of whitespace (\_s*); \s matches space or tab, while \_s matches space or tab or newline (end-of-line character). Finally, the pattern looks for \1 which is the whole word that was found in the escaped parentheses.

Finding this or that

A search pattern can use \| to search for something or something else. For example, to search for all occurrences of "red" or "green" or "blue", enter the following search pattern (in normal mode, press / then type the pattern, then press Enter):

red\|green\|blue

To replace all instances of "red" or "green" or "blue" with "purple", enter:

:%s/red\|green\|blue/purple/g

However, the above pattern finds "red" in "bored", so the substitute would change "bored" to "bopurple". If that is not what you want, use the following pattern to find only the whole words "red" or "green" or "blue":

\<\(red\|green\|blue\)\>

In a pattern, \< and \> respectively specify the beginning and end of a word, while \( and \) respectively specify the beginning and end of a group (the pattern \<red\|green\|blue\>, without escaped parentheses, would find "red" occurring at the beginning of a word, or "green" occurring anywhere, or "blue" occurring at the end of a word).

After searching with the command /\<\(red\|green\|blue\)\> you could change the whole words "red" or "green" or "blue" to "purple" by entering the following (the search pattern is empty in this command, so it uses the last search):

:%s//purple/g

In a substitute, you can use & in the replacement to mean the "whole matched pattern" (everything that was found). For example, the following will insert quotes around all occurrences of the whole words "red" and "green" and "blue":

:%s/\<\(red\|green\|blue\)\>/"&"/g

If you do not want the whole matched pattern, you can use parentheses to group text in the search pattern, and use the replacement variable \1 to specify the first group. For example, the following finds "color x" and replaces it with "colored x" where x is the whole word "red" or "green" or "blue":

:%s/color \<\(red\|green\|blue\)\>/colored \1/g

Finding two words in either order

You can search for a line that contains two words, in any order. For example, the following pattern finds all lines that contain both "red" and "blue", in any order:

.*red\&.*blue

In a pattern, \& separates alternates, each of which has to match at the same position. The two alternates in this example are:

  • .*red (will match all characters from the beginning of a line to the end of the last "red"); and
  • .*blue (will match all characters from the beginning of a line to the end of the last "blue").

A line which contains both "red" and "blue" will match both alternates, starting at the beginning of the line. The pattern .*red\&.*blue finds the last alternate (but only if all alternates match at the same position), so if you are highlighting matches, you will see text matched by .*blue highlighted.

An alternative procedure is to use a pattern that explicitly finds "red" followed by "blue", or "blue" followed by "red":

\(red.*blue\)\|\(blue.*red\)

To search for lines that contain only the whole words "red" and "blue", in either order, use one of the following patterns:

.*\<red\>\&.*\<blue\>
\(\<red\>.*\<blue\>\)\|\(\<blue\>.*\<red\>\)

Finding trailing zeroes

The following pattern finds redundant trailing zeroes in numbers:

\(\.\d\+\)\@<=0\+\>

The pattern does not find the "0" in "1.0", but it finds the trailing "00" in each of the following numbers: 1.000 1.000200 1.0002000300. After searching, the command :%s///g would delete all the redundant zeroes (the search pattern is empty, so it uses the last search).

The pattern:

  • Contains \@<= which checks if the preceding atom matches just before what follows.
  • First searches for 0\+\> (what follows).
  • Then checks if what is just before matches \(\.\d\+\) (preceding atom).

The first search is 0\+\> which finds one or more (\+) of 0 followed by end of word (\>).

The check is \(\.\d\+\)\@<=0\+\> which verifies that the text immediately before the trailing zeroes consists of a decimal point (\.), then one or more decimal digits (\d\+). The escaped parentheses \(...\) make an "atom" from what is enclosed by the parentheses.

References

17 September 2021

Password entering dialogue box would not show up in Outlook

Password Entering dialogue box would not show up in Outlook

 

Problem: After changing password, open Outlook, it is not connected the exchange server. 'Missing password' shows up at bottom right corner, but the password entering box would not show up.

Cause: You are sharing screen in Zoom meeting.

Solution: Stop sharing screen, then the password entering box wil l show up.

16 September 2021

Ubuntu (20.04) Linux Network Configuration

Ubuntu (20.04) Linux Network Configuration

 

https://ubuntu.com/server/docs/network-configuration

 

Ubuntu ships with a number of graphical utilities to configure your network devices. This document is geared toward server administrators and will focus on managing your network on the command line.

Ethernet Interfaces

Ethernet interfaces are identified by the system using predictable network interface names. These names can appear as eno1 or enp0s25. However, in some cases an interface may still use the kernel eth# style of naming.

Identify Ethernet Interfaces

To quickly identify all available Ethernet interfaces, you can use the ip command as shown below.

ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.102.66.200/24 brd 10.102.66.255 scope global dynamic eth0
       valid_lft 3257sec preferred_lft 3257sec
    inet6 fe80::216:3eff:fee2:5242/64 scope link
       valid_lft forever preferred_lft forever

Another application that can help identify all network interfaces available to your system is the lshw command. This command provides greater details around the hardware capabilities of specific adapters. In the example below, lshw shows a single Ethernet interface with the logical name of eth0 along with bus information, driver details and all supported capabilities.

sudo lshw -class network
  *-network
       description: Ethernet interface
       product: MT26448 [ConnectX EN 10GigE, PCIe 2.0 5GT/s]
       vendor: Mellanox Technologies
       physical id: 0
       bus info: pci@0004:01:00.0
       logical name: eth4
       version: b0
       serial: e4:1d:2d:67:83:56
       slot: U78CB.001.WZS09KB-P1-C6-T1
       size: 10Gbit/s
       capacity: 10Gbit/s
       width: 64 bits
       clock: 33MHz
       capabilities: pm vpd msix pciexpress bus_master cap_list ethernet physical fibre 10000bt-fd
       configuration: autonegotiation=off broadcast=yes driver=mlx4_en driverversion=4.0-0 duplex=full firmware=2.9.1326 ip=192.168.1.1 latency=0 link=yes multicast=yes port=fibre speed=10Gbit/s
       resources: iomemory:24000-23fff irq:481 memory:3fe200000000-3fe2000fffff memory:240000000000-240007ffffff

Ethernet Interface Logical Names

Interface logical names can also be configured via a netplan configuration. If you would like control which interface receives a particular logical name use the match and set-name keys. The match key is used to find an adapter based on some criteria like MAC address, driver, etc. Then the set-name key can be used to change the device to the desired logial name.

network:
  version: 2
  renderer: networkd
  ethernets:
    eth_lan0:
      dhcp4: true
      match:
        macaddress: 00:11:22:33:44:55
      set-name: eth_lan0

Ethernet Interface Settings

ethtool is a program that displays and changes Ethernet card settings such as auto-negotiation, port speed, duplex mode, and Wake-on-LAN. The following is an example of how to view supported features and configured settings of an Ethernet interface.

sudo ethtool eth4
Settings for eth4:
    Supported ports: [ FIBRE ]
    Supported link modes:   10000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: No
    Supported FEC modes: Not reported
    Advertised link modes:  10000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: No
    Advertised FEC modes: Not reported
    Speed: 10000Mb/s
    Duplex: Full
    Port: FIBRE
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000014 (20)
                   link ifdown
    Link detected: yes

IP Addressing

The following section describes the process of configuring your systems IP address and default gateway needed for communicating on a local area network and the Internet.

Temporary IP Address Assignment

For temporary network configurations, you can use the ip command which is also found on most other GNU/Linux operating systems. The ip command allows you to configure settings which take effect immediately, however they are not persistent and will be lost after a reboot.

To temporarily configure an IP address, you can use the ip command in the following manner. Modify the IP address and subnet mask to match your network requirements.

sudo ip addr add 10.102.66.200/24 dev enp0s25

The ip can then be used to set the link up or down.

ip link set dev enp0s25 up
ip link set dev enp0s25 down

To verify the IP address configuration of enp0s25, you can use the ip command in the following manner.

ip address show dev enp0s25
10: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.102.66.200/24 brd 10.102.66.255 scope global dynamic eth0
       valid_lft 2857sec preferred_lft 2857sec
    inet6 fe80::216:3eff:fee2:5242/64 scope link
       valid_lft forever preferred_lft forever6

To configure a default gateway, you can use the ip command in the following manner. Modify the default gateway address to match your network requirements.

sudo ip route add default via 10.102.66.1

To verify your default gateway configuration, you can use the ip command in the following manner.

ip route show
default via 10.102.66.1 dev eth0 proto dhcp src 10.102.66.200 metric 100
10.102.66.0/24 dev eth0 proto kernel scope link src 10.102.66.200
10.102.66.1 dev eth0 proto dhcp scope link src 10.102.66.200 metric 100 

If you require DNS for your temporary network configuration, you can add DNS server IP addresses in the file /etc/resolv.conf. In general, editing /etc/resolv.conf directly is not recommanded, but this is a temporary and non-persistent configuration. The example below shows how to enter two DNS servers to /etc/resolv.conf, which should be changed to servers appropriate for your network. A more lengthy description of the proper persistent way to do DNS client configuration is in a following section.

nameserver 8.8.8.8
nameserver 8.8.4.4

If you no longer need this configuration and wish to purge all IP configuration from an interface, you can use the ip command with the flush option as shown below.

ip addr flush eth0

Note

Flushing the IP configuration using the ip command does not clear the contents of /etc/resolv.conf. You must remove or modify those entries manually, or re-boot which should also cause /etc/resolv.conf, which is a symlink to /run/systemd/resolve/stub-resolv.conf, to be re-written.

Dynamic IP Address Assignment (DHCP Client)

To configure your server to use DHCP for dynamic address assignment, create a netplan configuration in the file /etc/netplan/99_config.yaml. The example below assumes you are configuring your first Ethernet interface identified as enp3s0.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true

The configuration can then be applied using the netplan command.

sudo netplan apply

Static IP Address Assignment

To configure your system to use static address assignment, create a netplan configuration in the file /etc/netplan/99_config.yaml. The example below assumes you are configuring your first Ethernet interface identified as eth0. Change the addresses, gateway4, and nameservers values to meet the requirements of your network.

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 10.10.10.2/24
      gateway4: 10.10.10.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [10.10.10.1, 1.1.1.1]

The configuration can then be applied using the netplan command.

sudo netplan apply

Loopback Interface

The loopback interface is identified by the system as lo and has a default IP address of 127.0.0.1. It can be viewed using the ip command.

ip address show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever

Name Resolution

Name resolution as it relates to IP networking is the process of mapping IP addresses to hostnames, making it easier to identify resources on a network. The following section will explain how to properly configure your system for name resolution using DNS and static hostname records.

DNS Client Configuration

Traditionally, the file /etc/resolv.conf was a static configuration file that rarely needed to be changed or automatically changed via DCHP client hooks. Systemd-resolved handles name server configuration, and it should be interacted with through the systemd-resolve command. Netplan configures systemd-resolved to generate a list of nameservers and domains to put in /etc/resolv.conf, which is a symlink:

/etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

To configure the resolver, add the IP addresses of the nameservers that are appropriate for your network to the netplan configuration file. You can also add an optional DNS suffix search-lists to match your network domain names. The resulting file might look like the following:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s25:
      addresses:
        - 192.168.0.100/24
      gateway4: 192.168.0.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [1.1.1.1, 8.8.8.8, 4.4.4.4]

The search option can also be used with multiple domain names so that DNS queries will be appended in the order in which they are entered. For example, your network may have multiple sub-domains to search; a parent domain of example.com, and two sub-domains, sales.example.com and dev.example.com.

If you have multiple domains you wish to search, your configuration might look like the following:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s25:
      addresses:
        - 192.168.0.100/24
      gateway4: 192.168.0.1
      nameservers:
          search: [example.com, sales.example.com, dev.example.com]
          addresses: [1.1.1.1, 8.8.8.8, 4.4.4.4]

If you try to ping a host with the name of server1, your system will automatically query DNS for its Fully Qualified Domain Name (FQDN) in the following order:

  1. server1.example.com

  2. server1.sales.example.com

  3. server1.dev.example.com

If no matches are found, the DNS server will provide a result of notfound and the DNS query will fail.

Static Hostnames

Static hostnames are locally defined hostname-to-IP mappings located in the file /etc/hosts. Entries in the hosts file will have precedence over DNS by default. This means that if your system tries to resolve a hostname and it matches an entry in /etc/hosts, it will not attempt to look up the record in DNS. In some configurations, especially when Internet access is not required, servers that communicate with a limited number of resources can be conveniently set to use static hostnames instead of DNS.

The following is an example of a hosts file where a number of local servers have been identified by simple hostnames, aliases and their equivalent Fully Qualified Domain Names (FQDN’s).

127.0.0.1   localhost
127.0.1.1   ubuntu-server
10.0.0.11   server1 server1.example.com vpn
10.0.0.12   server2 server2.example.com mail
10.0.0.13   server3 server3.example.com www
10.0.0.14   server4 server4.example.com file

Note

In the above example, notice that each of the servers have been given aliases in addition to their proper names and FQDN’s. Server1 has been mapped to the name vpn, server2 is referred to as mail, server3 as www, and server4 as file.

Name Service Switch Configuration

The order in which your system selects a method of resolving hostnames to IP addresses is controlled by the Name Service Switch (NSS) configuration file /etc/nsswitch.conf. As mentioned in the previous section, typically static hostnames defined in the systems /etc/hosts file have precedence over names resolved from DNS. The following is an example of the line responsible for this order of hostname lookups in the file /etc/nsswitch.conf.

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4
  • files first tries to resolve static hostnames located in /etc/hosts.

  • mdns4_minimal attempts to resolve the name using Multicast DNS.

  • [NOTFOUND=return] means that any response of notfound by the preceding mdns4_minimal process should be treated as authoritative and that the system should not try to continue hunting for an answer.

  • dns represents a legacy unicast DNS query.

  • mdns4 represents a Multicast DNS query.

To modify the order of the above mentioned name resolution methods, you can simply change the hosts: string to the value of your choosing. For example, if you prefer to use legacy Unicast DNS versus Multicast DNS, you can change the string in /etc/nsswitch.conf as shown below.

hosts:          files dns [NOTFOUND=return] mdns4_minimal mdns4

Bridging

Bridging multiple interfaces is a more advanced configuration, but is very useful in multiple scenarios. One scenario is setting up a bridge with multiple network interfaces, then using a firewall to filter traffic between two network segments. Another scenario is using bridge on a system with one interface to allow virtual machines direct access to the outside network. The following example covers the latter scenario.

Configure the bridge by editing your netplan configuration found in /etc/netplan/:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: no
  bridges:
    br0:
      dhcp4: yes
      interfaces:
        - enp3s0

Note

Enter the appropriate values for your physical interface and network.

Now apply the configuration to enable the bridge:

sudo netplan apply

The new bridge interface should now be up and running. The brctl provides useful information about the state of the bridge, controls which interfaces are part of the bridge, etc. See man brctl for more information.

networkd-dispatcher for pre-up, post-up, etc. hook scripts

Users of the former ifupdown may be familiar with using hook scripts (e.g pre-up, post-up, etc.) in their interfaces file. Netplan configuration does not currently support hook scripts in its configuration definition.

Instead to achieve this functionality with the networkd renderer, users can use networkd-dispatcher. The package provides users and packages hook points when specific network states are reached to aid in reacting to network state.

Note: If not on Ubuntu Server, but Desktop the network is driven by Network Manager - in that case you’d need NM Dispatcher scripts instead.

The Netplan FAQ has a great table that compares event timings between ifupdown/systemd-networkd/network-manager

It is important to be aware that those hooks run asychronous; that is they will not block transition into another state.

The Netplan FAQ also has an example on converting an old ifupdown hook to networkd-dispatcher.

Resources

 

Want IP of Ubuntu WSL 2 be in the same network of host Window system

Want IP of Ubuntu WSL 2 be in the same network of host Window system

 

https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723

 

 

13 September 2021

How to grep word1 & word2?

https://stackoverflow.com/questions/4487328/match-two-strings-in-one-line-with-grep

You can use

grep -inH 'string1' filename | grep -inH 'string2'

Or

grep -inH 'string1.*string2\|string2.*string1' filename

 

06 September 2021

SSH client command line

SSH client command line

ssh -p 22 (user)@127.0.0.1


sshd: no hostkeys available -- exiting

sshd: no hostkeys available -- exiting

https://www.garron.me/en/linux/sshd-no-hostkeys-available-exiting.html

I only needed to run

ssh-keygen -A

In the /etc/ssh/ folder, and the start the server

/etc/init.d/ssh start


What if ssh would not start?

What if ssh would not start?

 
This issue is caused by a bad configuration of /etc/ssh/sshd_config file. When the service try to launch it does not recognize every fields of this configuration file. In order to solve this issue, you must use the tool

/usr/sbin/sshd -T

In case /etc/ssh/sshd_config was wrong, this would show wrong parameters with lines.

You must correct this issues and then restart the service:

/etc/init.d/ssh restart

How to install and configure SSH on Ubuntu

How to install and configure SSH on Ubuntu

https://help.ubuntu.com/community/SSH/OpenSSH/Configuring

 

Accessing network applications between windows and wsl linux ubuntu

Accessing network applications between windows and wsl linux ubuntu

https://docs.microsoft.com/en-us/windows/wsl/compare-versions

 
Accessing Linux networking apps from Windows (localhost)
Accessing Windows networking apps from Linux (host IP)
Connecting via remote IP addresses
Accessing a WSL 2 distribution from your local area network (LAN)
IPv6 access


How to start a service on ubuntu?

How to start a service on ubuntu?


sudo service ssh start

And one could reasonably wonder, "how would you know that the service name was 'ssh'?" You can see them using  

service --status-all

05 September 2021

How to Delete Lines in Vim / Vi

How to Delete Lines in Vim / Vi 

 https://linuxize.com/post/vim-delete-line/

 

 Deleting a Line

The command to delete a line in Vim is
  dd .

Below are step-by-step instructions to delete a line:

    Press the Esc key to go to normal mode.
    Place the cursor on the line you want to delete.
    Type 
d and hit Enter to remove the line.

Pressing dd multiple times will delete multiple lines.
Deleting Multiple Lines

To delete multiple lines at once, prepend the dd command with the number of lines to be deleted. For example, to delete five lines you would do the following:

    Press the Esc key to go to normal mode.
    Place the cursor on the first line you want to delete.
    Type 
5dd  and hit Enter to delete the next five lines.

Delete a range of lines

The syntax for deleting a range of lines is as follows:

  :[start],[end]d 

For example, to delete lines starting from 3 to 5 you would do the following:

    Press the Esc key to go to normal mode.
    Type 
:3,5d  and hit Enter to delete the lines.

You can also use the following characters to specify the range:

    . (dot) - The current line.
    $ - The last line.
    % - All lines.

Here are a few examples:

    :.,$d - From the current line to the end of the file.
    :.,1d - From the current line to the beginning of the file.
    :10,$d - From the 10th line to the end of the file.

Delete All Lines

To delete all line you can use either the % symbol that represents all lines or the 1,$ range:

    Press the Esc key to go to normal mode.
    Type  
:%d  and hit Enter to delete all the lines.

Deleting Lines Containing a Pattern

The syntax for deleting multiple lines based on a specific pattern is as follows:


 :g/<pattern>/d 

The global command (g) tells the delete command (d) to delete all lines containing the <pattern>.

To match the lines not matching the pattern, add an exclamation mark (!) before the pattern:

 :g!/<pattern>/d

The pattern can be a literal match or regular expression . Below are some examples:

     :g/foo/d  - Delete all lines containing the string “foo”. It also removes line where “foo” is embedded in larger words, such as “football”.
    :g!/foo/d - Delete all lines not containing the string “foo”.
    :g/^#/d - Remove all comments from a Bash script. The pattern ^# means each line beginning with #.
    :g/^$/d - Remove all blank lines. The pattern ^$ matches all empty lines.
    :g/^\s*$/d - Remove all blank lines. Unlike the previous command, this also removes the blank lines that have zero or more whitespace characters (\s*).



29 August 2021

Vim auto complete

Type in a few letters, press Ctrl+n (next), or Ctrl+p (previous), a list of candidate words appears, which are all the words in the file, beginning with the a few letters you have typed in.

If the first candidate word is what you want, then you can continue to type in anything you want next.

Otherwise, you can continue to press Ctrl+n, or Ctrl+p, until the candidate word you want, then continue to type in anything you want next.

:help popupmenu-completion
:help popupmenu-keys

28 August 2021

Using Tabs in Vim

 Using tab in vim

 

https://www.linux.com/training-tutorials/vim-tips-using-tabs/

 

 1. Open a new tab for authoring a new text file

:tabnew

2. Open an existing file in a new tab


:tabf *abc

or

:tabf abc*

or

:tabf abc[tab]

f means find. Suppose that there is a file whose name is abcdef.txt. You type 'abc', then press the Tab key, system will automatically type in the rest of the file name for you.

3. Go to different tabs

gt - to the next tab.
gT - move to the previous tab.
g0 - move to the first tab.
g$ - move to the last tab.

ngt

n: 1 - the first tab; 2 - the second tab, and so on.

4. Moving a tab

:tabm n

n: 0 - the first tab; 1 - the second tab, and so on.

5. Running commands in all the opened tabs

e.g. if we want to substitute 'bar' for 'foo' in all the opened tabs, then

:tabdo %s/foo/bar/gc

5. To see a list of the files opened in all the tabs

:tabs


22 August 2021

Can open Excel files locally but not from network

 

Can open Excel files locally but not from network

Problem: 

Can open Excel files locally but not from network

Solution:

File -> Options -> Trust Centre -> Trust Centre Settings -> Protected View.

Clear all the ticks. Click OK.



16 August 2021

export pandas dataframe to html and appy css styles

 export pandas dataframe to html and appy css styles

 

 Ku Tang Pan

I found the most precise, and frankly the easiest way of doing it is skipping the styling, to_html() etc. and converting the DF to a dictionary using the df.to_dict() method.

Specifically what gave me trouble, was displaying the styled pandas html in an outlook email, as it just wouldn't render properly with the css mess that pandas was producing.

iterate over the dict and generate the html there by simply wrapping keys/values in the tags that you need, adding classes etc. and concatenate this all into one string. Then paste this str into a prepared template with a predefined css.

For convenience I found it's useful to export the same df twice, using .to_dict() and to_dict('index') to first fill in the columns and then work your way down row by row. Alternatively just have a list of relevant column names.

dict_data = [df.to_dict(), df.to_dict('index')]

return_str = '<table><tr>'

for key in dict_data[0].keys():
    return_str = return_str + '<th class="header">' + key + '</th>'

return_str = return_str + '</tr>'

for key in dict_data[1].keys():
    return_str = return_str + '<tr><th class="index">' + key + '</th>'
    for subkey in dict_data[1][key]:
        return_str = return_str + '<td>' + dict_data[1][key][subkey] + '</td>'

return_str = return_str + '</tr></table>'

and then return_str goes into the template.

 

10 August 2021

How to read a file, which only contains a string, into Python, and then convert the string into a list?

 How to read a file, which only contains a string, into Python, and then convert the string into a list?


from pathlib import Path

txt = Path('filename.txt').read_text()

list_ = list(txt.split('; '))

08 August 2021

How to convert a pandas dataframe into a python list, and a python list into a pandas dataframe?

 How to convert a pandas dataframe into a python list, and a python list into a pandas dataframe?


1. To convert a pandas dataframe into a python list

email_list = df['Email'].to_list()

or

email_list = df[['Name', 'Email']].to_list()


2. To convert a python list into a pandas dataframe

Examples

>>> mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
...           {'a': 100, 'b': 200, 'c': 300, 'd': 400},
...           {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
>>> df = pd.DataFrame(mydict)
>>> df
      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000


28 July 2021

Can not open PPT

 Can not open PPT


Symptom

When opening a PPT, 'PowerPoint found a problem with content in xxx.pptx. PowerPoint can attempt to repair the presentation.

If you trust the source of this presentation, click Repair.'

After you click 'Repair', you get

'There was an error accessing xxxxx.pptx.'

If you click 'Show Help', you get

'This error can be caused by the following conditions:

. Your hard drive or floppy drive has a corrupt section (damaged track or sector).
. A temporary operating system or network failure has occurred.
. Your network is unavailable, slow, or is corrupting data packets (failure of a router, network card, or noise on the network transmission line).
. Your antivirus program may be causing problems accessing certain files.

In the case of a damaged disk, you must save the file to another location (for example, a different drive).

In the case of a failure of the operating system, ...

If the network is experiencing problems, ... consult your network administrator.'

You click OK, you see nothing.


Solution

Click 'File -> Options -> Trust Centre -> Trust Centre Settings... -> Protected View',

Clear all the tick marks. Click OK.

You open the .pptx file fine now.

27 July 2021

How to concatenate all the text files in a directory with Python?

 How to concatenate all the text files in a directory with Python?


https://stackoverflow.com/questions/13613336/python-concatenate-text-files

import glob2

filenames = glob2.glob('*.txt')  # list of all .txt files in the directory

with open('outfile.txt', 'w') as f:
    for file in filenames:
        with open(file) as infile:
            f.write(infile.read()+'\n')

How to set pseudo class attributes within the body part of a web page?

 How to set pseudo class attributes within the body part of a web page?

https://stackoverflow.com/questions/1033156/how-to-write-ahover-in-inline-css


html


<a href="https://www.google.com/" class="one">Hello World</a>


css


.one {

text-decoration: none;
}

.one:link {
text-decoration: none;
}

.one.visited {
text-decoration: none;

.one:hover{
text-decoration: none;
}

.one:active{
text-decoration: none;
}

09 July 2021

How to get lost files back resulting from running git add and git restore -s@ -SW?

 How to get lost files back resulting from running git add and git restore -s@ -SW?


Problem

suppose you ran

$ git add .

then you ran

$ git status

and saw that you have added some files which you did not want to add. 

(You did not run 

$ git commit

)

then you wanted to remove them from the index.

In haste, you ran 

$ git restore -s@ -SW

resulting in those files are gone away from your working tree (resulted from -W).

You want to get them back.

Solution:

Run

$ git fsck --cach --no-reflogs --lost-found --unreachable head

You will get a list of SHA1, which point to your lost files.

Then run

$ git show SHA1 > name_of_lost_file

one by one, every lost file will now be in your current work directory!


01 July 2021

How to find all the files containing the string 'abc' in the current directory and all the sub-directories

How to find all the files containing the string 'abc' in the current directory and all the sub-directories?


find . -type f | xargs fgrep '...'

29 June 2021

CSS for 5 Typical Media Query Break Points (Device Screen Sizes)

CSS for 5 Typical Media Query Break Points (Device Screen Sizes)


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>

/* Typical device breakpoints - 5 groups */

/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
  [class*="col-"] {width: 100%; }
}

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
  .col-min600-1 {width: 8.33%;}
  .col-min600-2 {width: 16.66%;}
  .col-min600-3 {width: 25%;}
  .col-min600-4 {width: 33.33%;}
  .col-min600-5 {width: 41.66%;}
  .col-min600-6 {width: 50%;}
  .col-min600-7 {width: 58.33%;}
  .col-min600-8 {width: 66.66%;}
  .col-min600-9 {width: 75%;}
  .col-min600-10 {width: 83.33%;}
  .col-min600-11 {width: 91.66%;}
  .col-min600-12 {width: 100%;}
}

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
  .col-min768-1 {width: 8.33%;}
  .col-min768-2 {width: 16.66%;}
  .col-min768-3 {width: 25%;}
  .col-min768-4 {width: 33.33%;}
  .col-min768-5 {width: 41.66%;}
  .col-min768-6 {width: 50%;}
  .col-min768-7 {width: 58.33%;}
  .col-min768-8 {width: 66.66%;}
  .col-min768-9 {width: 75%;}
  .col-min768-10 {width: 83.33%;}
  .col-min768-11 {width: 91.66%;}
  .col-min768-12 {width: 100%;}
}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
  .col-min992-1 {width: 8.33%;}
  .col-min992-2 {width: 16.66%;}
  .col-min992-3 {width: 25%;}
  .col-min992-4 {width: 33.33%;}
  .col-min992-5 {width: 41.66%;}
  .col-min992-6 {width: 50%;}
  .col-min992-7 {width: 58.33%;}
  .col-min992-8 {width: 66.66%;}
  .col-min992-9 {width: 75%;}
  .col-min992-10 {width: 83.33%;}
  .col-min992-11 {width: 91.66%;}
  .col-min992-12 {width: 100%;}
}

/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
  .col-min1200-1 {width: 8.33%;}
  .col-min1200-2 {width: 16.66%;}
  .col-min1200-3 {width: 25%;}
  .col-min1200-4 {width: 33.33%;}
  .col-min1200-5 {width: 41.66%;}
  .col-min1200-6 {width: 50%;}
  .col-min1200-7 {width: 58.33%;}
  .col-min1200-8 {width: 66.66%;}
  .col-min1200-9 {width: 75%;}
  .col-min1200-10 {width: 83.33%;}
  .col-min1200-11 {width: 91.66%;}
  .col-min1200-12 {width: 100%;}
}

</style>
</head>

<body>

</body>

</html>

27 June 2021

How to obtain same effect as 'less' in Linux in mysql?

 How to obtain same effect as 'less' in Linux in mysql?


i.e. the output display stops at the first screen, before you can move the output in vi way.

$pager less

$select * from table;

Furthermore, use -

$pager less

$select * from table\G

so that fields are displayed from up to down, instead of from left to right.

On Ubuntu, Creating Virtual Environments

On Ubuntu, creating Virtual Environments

https://packaging.python.org/tutorials/installing-packages/#creating-and-using-virtual-environments

python3 -m venv <DIR>
source <DIR>/bin/activate

 '<DIR>' is actually your site name, e.g. 'mysite'.

26 June 2021

On Windows, How to Install, Create and Activate a Python Virtual Environment (Project) for Django? mkvirtualenv is not recognized as an internal or external command, operable program or batch file

 (For Windows)

How to Install, Create and Activate a Python Virtual Environment (Project) for Django? 

mkvirtualenv is not recognized as an internal or external command, operable program or batch file


https://stackoverflow.com/questions/56778211/error-message-mkvirtualenv-is-not-recognized-as-an-internal-or-external-command


For Python 3.3 or newer, Commands for installing, creating and activate virtual environment has been changed.

You can install virtual environment using pip:

py -m pip install --user virtualenv

For creating new environment:

py -m venv myproject

To activate your virtual environment:

.\myproject\Scripts\activate

After activating virtual environment, You’ll see “(myproject)” next to the command prompt.

20 June 2021

How to list all html files in a directory recursively sorted by file size?

 How to list all html files in a directory recursively sorted by file size?

$du -a -h | sort -nr | grep 'html' | less

Note:

1. '-a' means all files and directories, not just directories.

2. '-h' means human readable.

3. '-n' means numeric sort.

4. '-r' means recursive.

How to clean up unnecessary files and optimise the local repository

 How to clean up unnecessary files and optimise the local repository?

$git gc

11 June 2021

The Best Website on Which You Draw Charts and Diagrams

 The Best Website on Which You Draw Charts and Diagrams

http://lucid.app


How to create an alias command in git to display a log in graph?

How to create an alias command in git to display a log in graph?


$alias graph="git log --all --oneline --graph  --decorate"

(I find that it outputs the same as without '--decorate'.)

Note:

'--oneline' is a shorthand for both '--pretty=oneline' and  '--abbrev-commit' used together.

-----------------------------------------------------------------------------------------

I find the 2 following commands are very helpful,

1.

$git log --all --oneline --graph

2.

$git show-branch --all --more=1000

(you can replace 1000 with any number, according to your requirements.)

08 June 2021

How to undo (almost) anything with Git

How to undo (almost) anything with Git


https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/



 

checkout, reset, restore, revert, and switch. What do they really do?

 checkout, reset, restore, revert, and switch. What do they really do?

Written by torek, etc.


https://stackoverflow.com/questions/58003030/what-is-the-git-restore-command-and-what-is-the-difference-between-git-restor


The whole page is useful, especially from 'To add to VonC's answer, and bring into the picture all the relevant commands, in alphabetical order, I will cover:'

I also like -

$git restore -s@ -SW -- <file> 

above the aforesaid section.



07 June 2021

How to undo and redo in git?

 How to undo and redo in git?

For both undo and redo, use git reflog and git reset --hard head@{n}

You will find n in the result of git reflog.

06 June 2021

How to find what the git merge conflicts are?

 How to find what the git merge conflicts are?

After you run
$git merge <branch name 2>

If the merge failed, and it said that there was a conflict, the error message would tell you in which file the conflict was - 'Merge conflict in <file name>'.

Run -
$git diff <current branch name> <branch name 2> <file name>

you will get what the conflict is. Content in red is the change done in <current branch name>, and that in green is the change done in <branch name 2>.



01 June 2021

My '_vimrc' File

source $VIMRUNTIME/vimrc_example.vim


set diffexpr=MyDiff()

function MyDiff()

  let opt = '-a --binary '

  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif

  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif

  let arg1 = v:fname_in

  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif

  let arg1 = substitute(arg1, '!', '\!', 'g')

  let arg2 = v:fname_new

  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif

  let arg2 = substitute(arg2, '!', '\!', 'g')

  let arg3 = v:fname_out

  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif

  let arg3 = substitute(arg3, '!', '\!', 'g')

  if $VIMRUNTIME =~ ' '

    if &sh =~ '\<cmd'

      if empty(&shellxquote)

        let l:shxq_sav = ''

        set shellxquote&

      endif

      let cmd = '"' . $VIMRUNTIME . '\diff"'

    else

      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'

    endif

  else

    let cmd = $VIMRUNTIME . '\diff'

  endif

  let cmd = substitute(cmd, '!', '\!', 'g')

  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3

  if exists('l:shxq_sav')

    let &shellxquote=l:shxq_sav

  endif

endfunction



        " 字符编码{{{

" Vim显示的编码(设置这个不会改变文件的编码){

        if has('win32') || has('win64')

        set encoding=utf-8

        set termencoding=chinese

        endif

" }

" 编辑已存在的文件时的参考文件编码.需要注意顺序,前面的字符集应该比后面的字符集大{

        set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

" }

" }}}

"

"

filetype plugin indent on

autocmd FileType html setlocal shiftwidth=2 tabstop=2

autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4

set number

"

" The following commands do not work

" autocmd FileType html 

" set omnifunc=htmlcomplete#CompleteTags


" The following commands do not work

" set omnifunc=csscomplete#CompleteCSS

" autocmd FileType css set omnifunc=csscomplete#CompleteCSS


" html css class id auto complete 

" https://github.com/Shougo/neocomplete.vim

let g:neocomplete#enable_at_startup = 1



12 May 2021

Several Issues about Responsive Web Pages and HTTPS

Several Issues about Responsive Web Pages and HTTPS


1. Texts and images are not aligned to HTML5 animations viewed on phones

They are aligned on computer monitors.

Cause: width of text and images are '100%', but that of the animation is '1140px'.

Solution: After I changed width into '100%' and height into 'auto' in -

(1) 'animation_container' div;

(2) canvas;

(3) 'dom_overlay_container' div,

the text and images have become aligned to the html5 animation now.

(Got help from 'Cypher- Entrepreneur')


2. There is a link on the image banner, but when viewed in browser, there is no link.

Cause: '>' is missing in <div> of <div><a href=""><img ...></a></div>

Solution: Inserted the missing '>', then the link on the image banner works now.

Note: (1) Better use 'Sublime Text' software to edit code files.

        (2) Better use https://www.aliciaramirez.com/closing-tags-checker/ to check if any closed tag is missing. 

(Got help from 'Cypher- Entrepreneur')


3. Website completely normal visited outside of our company LAN network, but can not open at all in LAN

Cause: IP of CloudFlare has changed.

Handling: The IP on our LAN DNS server which is mapped to our website domain was 104.28.30.95, which is an IP of CloudFlare.

Since we can not open our website at all, I changed IP on our LAN DNS into 64.91.250.231, which is the IP when we ftp and upload our website files. After the change, we can open our website, but (1) it is http, not https, 'not secure' shows. (2) none of our html5 animations show up, and some of the images do not show.

Pinging website domain name from out of LAN, IP addresses are (1) 172.67.133.250 (2) 104.21.25.97. Pinging website domain name from within LAN, IP is 64.91.250.231, of course.

Solution: changed the IP on LAN DSN from 64.91.250.231. The problem is gone. Website becomes normal viewed whether from out of LAN, or in LAN.

(Got help from 'Benjamin Gao)


4. On home page of website, there are two html5 animations. The first animation shows up fine, but the second one does not show.

If I remove the first animation, then the second animation shows.

Cause: </iframe> for the second animation is missing.

Solution: Added </iframe>. The second animation shows.


5. The distance between the two animations is too far away.

Cause: 'vh' in 'min-height:98vh' cause the distance.

Solution: Removed 'min-height:98vh'. The space in vertical direction disappeared. But there is a big blank space horizontally.

Using responsive iframe solved the problem of the problem of big blank space horizontally.

(Got help from 'Cypher Entrepreneur')