10 cool Vim editor tips users should know

1. Opened a file with vim but does not have permission to write, forgot to do sudo? No worries, you can still write to the file if user has sudo rights.

:w !sudo tee %

2. Revert document to 5min back. Opposite of “:earlier” is “:later”.

:earlier 5m

3. Execute external command and dump output in current window.

:.! [command]

Add current date in the fileopened.

:.! date

4. Delete commands

diw to delete the current word.

di( to delete within the current parenthesis.

di” to delete the text between the quotes.

More can be found at

:help text-objects

5. Turn vim into hex editor. Open file with “-b” to avoid any damage.

:%!xxd

Revert:

:%!xxd -r

6. Searching

/pattern       - search forward for pattern
?pattern       - search backward
n              - repeat forward search
N              - repeat backward
*              - search for word currently under cursor
g*             - search for partial word under curso

Some variables available to set:

:set ignorecase - case insensitive
:set smartcase  - use case if any caps used 
:set incsearch  - show match as search proceeds
:set hlsearch   - search highlighting

7. Variables

:set            - shows vars different from defaults
:set all        - shows all values
:set foo?       - shows the value of foo
:set foo+=opt   - add opt to the value w/o changing others
:set foo-=opt   - remove opt from value
:set foo&       - reset foo to default value
:setlocal foo   - only the current buffer

:verbose set foo?   - tells you where it was last set

8. Replace

Replace occurrences of 'foo' with 'bar' in the whole file

:%s/foo/bar/g

Replace occurrences of ‘foo’ with ‘bar’ in the current line

:s/foo/bar/g

Replace everything with confirmation first, note the ‘c’ below

:%s/foo/bar/gc

Replace words exactly matching ‘foo’ with confirmation first

:%s/\<foo\>/bar/gc

Replace everything with case insensitive and confirmation first, note the ‘c’ and ‘I’ below

:%s/foo/bar/gic

9. Create horizontal and vertical windows. ‘-o’ for horizontal and ‘-O’ for vertical windows.

vim -o file1 file2

vim -O file1 file2

10. Get help

:help ‘incsearch’

:help :substitute

18 Essential Python tricks

  1. Get file path of imported modules

>> import os
>>> print os
<module ‘os’ from ‘/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc’>
>>>

2. Get memory usage of an object

>> import sys
>>> var = 10
>>> print sys.getsizeof(var)
24
>>>

3. Print string n times

>> sample=”Python”
>>> n=4
>>> print sample*n
PythonPythonPythonPython
>>>

4. swapping of two numbers

>> a=10
>>> b=20
>>> a
10
>>> b
20
>>> a,b = b,a
>>> a
20
>>> b
10
>>>

5.Reverse a string

>> sample = “Python”
>>> sample[::-1]
‘nohtyP’
>>>

6. Return multiple values from function

>> def foo():
… return 10, 20

>>> x, y = foo()
>>> x
10
>>> y
20
>>>

7. Create string from list

>> list_str = [“Python”, “is”, “awesome”]
>>> ” “.join(list_str)
‘Python is awesome’
>>>

8. Transpose a matrix

>> mtx = [ [1,2,3], [4,5,6], [7,8,9] ]
>>> zip(*mtx)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>>

9. Assign list elements to multiple variables

>> x, y, z = [10,20,30]
>>> x
10
>>> y
20
>>> z
30
>>>

10. Zipping two lists

>> x = [1, 2, 3]
>>> y = [‘a’, ‘b’, ‘c’]
>>> for i in zip(x,y): print i

(1, ‘a’)
(2, ‘b’)
(3, ‘c’)
>>> z=zip(x,y)
>>> z
[(1, ‘a’), (2, ‘b’), (3, ‘c’)]
>>>

11. Create single list out of list of lists

>> x = [[1, 2], [3, 4], [5, 6]]
>>> import itertools
>>> list(itertools.chain.from_iterable(x))
[1, 2, 3, 4, 5, 6]
>>>

12. check if two strings are anagrams

>> from collections import Counter
>>> def is_anagram(string1, string2):
… return Counter(string1) == Counter(string2)

>>> is_anagram(“xyzw”, “zywx”)
True
>>> is_anagram(“xyzw”, “zyx”)
False
>>>

13. Taking stdin as list of int

>> x = map(int, raw_input().split())
1 2 3 4 5 6
>>> x
[1, 2, 3, 4, 5, 6]
>>>

14. Ternary operator for conditional assignment

>> a = 2
>>> is_even = True if a % 2 == 0 else False
>>> is_even
True
>>> a=3
>>> is_even = True if a % 2 == 0 else False
>>> is_even
False
>>>

>> [ x**3 if x%2 != 0 else 0 for x in range(9)]
[0, 1, 0, 27, 0, 125, 0, 343, 0]
>>>

15. “_” operator : references last executed operations

>> a = 10
>>> b= 20
>>> _
[0, 1, 0, 27, 0, 125, 0, 343, 0]
>>>
>>> a+b
30
>>> a-b
-10
>>> _
-10
>>>

16. Run simple http server

Hindons-MacBook-Air:~ hindon$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 …

17. Inspect an object

>> x=10
>>> dir(x)
[‘__abs__’, ‘__add__’, ‘__and__’, ‘__class__’, ‘__cmp__’, ‘__coerce__’, ‘__delattr__’, ‘__div__’, ‘__divmod__’, ‘__doc__’, ‘__float__’, ‘__floordiv__’, ‘__format__’, ‘__getattribute__’, ‘__getnewargs__’, ‘__hash__’, ‘__hex__’, ‘__index__’, ‘__init__’, ‘__int__’, ‘__invert__’, ‘__long__’, ‘__lshift__’, ‘__mod__’, ‘__mul__’, ‘__neg__’, ‘__new__’, ‘__nonzero__’, ‘__oct__’, ‘__or__’, ‘__pos__’, ‘__pow__’, ‘__radd__’, ‘__rand__’, ‘__rdiv__’, ‘__rdivmod__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rfloordiv__’, ‘__rlshift__’, ‘__rmod__’, ‘__rmul__’, ‘__ror__’, ‘__rpow__’, ‘__rrshift__’, ‘__rshift__’, ‘__rsub__’, ‘__rtruediv__’, ‘__rxor__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__sub__’, ‘__subclasshook__’, ‘__truediv__’, ‘__trunc__’, ‘__xor__’, ‘bit_length’, ‘conjugate’, ‘denominator’, ‘imag’, ‘numerator’, ‘real’]
>>>

18. Create dictionary from two lists

>> x = [1,2,3]
>>> y=[‘a1′,’b1′,’c1’]
>>> dict(zip(x,y))
{1: ‘a1’, 2: ‘b1’, 3: ‘c1’}
>>>

 

How to reset Prism admin password of Nutanix cluster

Connect to CVM using ssh with “nutanix” user. CVM has a pre installed python file (reset_admin_password.py) to reset Prism admin password.

Run this file ‘reset_admin_password.py’ which reset prism admin password to factory default – ‘Nutanix/4u’.

Login in prism UI with ‘admin’  user & ‘Nutanix/4u’ password and create a new password. UI will ask to login again with new password, you can verify password update task created in the UI.

How to install Nutanix Openstack Pike drivers?

Nutanix released Openstack pike drivers recently. Let’s take a step by step look on how to consume these drivers in your Openstack Pike deployment.

Requirements:-

  1. Openstack Pike deployment
  2. Nutanix PE (Prism Element) set up

Steps:-

  1. Download Nutanix rpm/deb package from Nutanix portal.
  2. install the rpm ( rpm -i…..) on machines running neutron-server, nova-compute, glance and cinder-volume.
  3. Add your Nutanix PE details in /etc/nutanix_openstack_config.json
  4. neutron-server:
    1. Edit neutron.conf (/etc/neutron/plugin.ini) to have below config:-
      1. type_drivers=vlan
      2. tenant_network_types=vlan
      3. mechanism_drivers=nutanix
    2. Edit /usr/lib/python2.7/site-packages/neutron-<x.y.z>-py2.7egg-info/entry_points.txt to have below config
      1. [neutron.ml2.mechanism_drivers]
        1. nutanix=nutanix_openstack.neutron.driver:AcropolisNetworkDriver
    3. restart neutron service:  service neutron-server restart
  5. Glance:
    1. Edit /etc/glance/glance-api.conf to have below edit
      1. [glance_store]
        1. stores=http
        2. default_store=http
      2. Edit /usr/lib/python2.7/site-packages/glance_store-<x.y.z>-py2.7.egg-info/entry_points.txt to have below config
        1. [glance_store.drivers]
          1. http = nutanix_openstack.glance:Store
      3. restart glance api and registry services ex. service openstack-glance-api restart & service openstack-glance-registry restart
  6. Cinder-volume:
    1. Edit /etc/cidner/cinder.conf to have below config
      1. [DEFAULT]
        1. enabled_backend=nutanix_openstack
        2. glance_host=$glance_service_ip
        3. glance_api_servers=$glance_service_ip:$glance_api_port
      2. [nutanix_openstack]
        1. volume_driver=nutanix_openstack.cinder.driver.AcropolisVolumeDriver
    2. Edit /usr/lib/python2.7/site-packages/cinder-<x.y.z>-py2.7.egg-info/SOURCES.txt to have below config
      1. nutanix_openstack/cinder/driver.py
    3. restart cinder-volume service:  service openstack-cinder-volume restart
  7. nova-compute:
    1. Edit /etc/nova/nova.conf to have below config
      1. [DEFAULT]
      2. compute_driver=nutanix_openstack.nova.AcropolisComputeDriver
      3. vnc_enabled=True
    2. restart nova-compute: service nova-compute restart
    3. run nova vnc in background
      1. /usr/bin/prism_vnc_proxy –bind_address=0.0.0.0 –bind_port=<random-port> –prism_hostname=<cluster-ip> –prism_username=<prism admin user> –prism_password=<prism admin user password> –docroot=/usr/share/nutanix_openstack/vnc/static &
      2. Note that, bind_port should be same as defined in /etc/nutanix_openstack_config.json cluster vnc port number.