Search

Coder's notes

For your infomation

Category

Linux

Useful notes about Linux

Using webmode with Emacs

Want better HTML editing features for Emacs? Not happy with Jinja template editing in Emacs? Try this and you won’t be disappointed.

Download web-mode.el from https://raw.github.com/fxbois/web-mode/master/web-mode.el to your local folder (e.g. ~/.emacs.d/custom-modes).

mkdir ~/.emacs.d/custom-modes
wget -P ~/.emacs.d/custom-modes https://raw.github.com/fxbois/web-mode/master/web-mode.el

Now open~/.emacs and add the following lines

(add-to-list 'load-path "~/.emacs.d/custom-modes")

(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))

;; Indentation for web-mode.el
(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2)

Restart Emacs. Now try to edit any HTML, CSS or Jinja template file 🙂

Setting up Python development environment without superuser permissions

  1. Compile Python from source
    1. Download Python source code from python.org. E.g.:
      wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
      Unpack Python and compile it to our local folder
      tar -xvf Python-3.4.3.tgz
      cd Python-3.4.3
      ./configure --prefix=/home/user/bin/python --enable-unicode=utf8
      make && make install
  2. Install virtualenv locally
    1. Now you should have this folder ~/bin/python. Execute this to make sure Python have been installed properly
      ~/bin/python/bin/python --version
    2. Install virtualenv using local pip version (If you use Python 2.x, change pip to pip2
      ~/bin/python/bin/pip3 install virtualenv
    3. Create a virtualenv (e.g. ~/myp3)
      ~/bin/python/bin/virtualenv -p ~/bin/python/bin/python myp3
    4. Activate the virtualenv (make sure you have a space between the dot and the tilde.
      . ~/myp3/bin/activate
    5. That’s it. Now you should be able to build CPython packages. For example, try to install mecab-python3 which requires to have access to python.h
      pip3 install mecab-python3
  3. Lorem ipsum!

Raspberry Pi – All about wireless connection

Turn Raspberry Pi to be an Access Point [reference link]

Connect Raspberry Pi to a WiFi network:

  1. Method 1(via command line) [reference link]
    • sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    • Edit the file so it looks like this:
      ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
      update_config=1
      network={
      ssid=”Your Wifi Network Name Here”
      proto=RSN
      key_mgmt=WPA-PSK
      pairwise=CCMP TKIP
      group=CCMP TKIP
      psk=”Your Wifi Password Goes Here”
      }
    • If the WiFi network has no password, edit /etc/wpa_supplicant/wpa_supplicant.conf as follow
      ctrl_interface=/var/run/wpa_supplicant
      network={
      ssid="YOUR NETWORK SSID"
      proto=RSN
      key_mgmt=NONE
      }
    • Then restart the network:
      $ sudo /etc/init.d/networking restart
      Reboot RPI if necessary
  2. Method 2 (via GUI):
    • $ startx
    • Look for the WiFi network available and connect with the same way as in normal PC.

Screen tutorial (Fedora Linux)

If you work on terminal a lot, you might find screen is useful. Screen provides a virtual terminal which supports multiple windows, copy paste, etc. and it’ll continue to run after you have closed the real terminal you are working on. It’s distributed with most Linux distro so it’s most likely that you won’t have to install it. Just type screen to run. If it was not pre-installed or you want to update it to latest version you can try:

Fedora: sudo -c “yum install -y screen”

Ubuntu: sudo apt-get install screen

After you have started screen just type echo $STY. If you see something like 3002.pts-0.localhost you’ll know that you are inside a screen.

The features that I find most useful of screen are creating/switching/closing windows and copy paste. Let’s try these first.

Multiple windows

If you press Ctrl+a Ctrl+c (keep pressing Ctrl while you press a and then c) you can create a new screen. To switch between windows, you can try Ctrl+c Ctrl+” (double quotes) to show windows list, use arrow keys to navigate around, and then press Enter to select the windows that you want. An alternative method is to press Ctrl+[0-9] to activate the windows you want to (start from 0).

Copy paste & screen scrolling

If you want to copy paste a part on the screen, press Ctrl+a Ctrl+[ to activate copy-paste mode. After that you can use arrow keys to move around. You may use space to mark the start point and the end point. After you have marked the end point copy-paste mode will be deactivated. You also can deactivate this mode using ESC key (I often use this to scroll up and down the current window). Pasting can be done simply by pressing Ctrl+a Ctrl+]

Starting, detach and resume a screen session

To detach from a screen session, you can press Ctrl+a Ctrl+d (detaching). The screen will continue to run in the background. You can quit screen completely by Ctrl+a Ctrl+\

If you want to resume a screen session that running in the background, type screen -dr into the terminal.

Further reading

Now you knew enough to use screen. There are many more features in screen user manual at: http://www.gnu.org/software/screen/manual/screen.html

Using Git

Clone project `foo’ of user `user‘ from Github

git clone --recursive https://github.com/user/foo

Add a submodule into a repository

git submodule add https://github.com/user/foo.git
git submodule init
git submodule update

Undo modification

git checkout -- [filename]

Update submodule to latest version

git fetch && git checkout master && git merge origin/master

Generate SSH key for passwordless push to Github

Source: https://help.github.com/articles/generating-ssh-keys/

ls -al ~/.ssh
ssh-keygen -t rsa -C "account@gmail.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

# Now add key to Github account (~/.ssh/id_rsa.pub)
https://github.com/settings/ssh

# Test connection
ssh -T git@github.com

# Update the repository remote URL to SSH
git remote set-url origin git@github.com:username/REPO.git
git remote -v

Generate a time stamp in bash

echo `date +%Y-%m-%d_%H%M%S`

Dealing with zip files on Linux

Compress & uncompress files

zip myfile.zip file1.txt file2.txt file3.txt
unzip myfile.zip -d new_output_folder/

Upgrading Fedora using fedup

Read more: https://fedoraproject.org/wiki/Upgrading

yum install fedup
# To update to Fedora version 20 using network
fedup –network 20

If you encounter this error message: Downloading failed: could not verify GPG signature: No public key, run:

yum update fedora-release

or you can ignore GPG completely using

sudo fedup –network 20 –nogpgcheck

Blog at WordPress.com.

Up ↑