[Linux] Using the Vim editor

Commands that switch the editor to insert mode

  • a will append: it moves the cursor one position to the right before switching to insert mode
  • i will insert
  • o will insert a blank line under the current cursor position and move the cursor to that line.
  • Esc key switches back to command mode.

Pressing the Esc key switches back to command mode. If you’re not sure what mode you’re in because you use a really old version of vi that doesn’t display an “INSERT” message, type Esc and you’ll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.

Basic operations

These are some popular vi commands:

  • n dd will delete n lines starting from the current cursor position.
  • n dw will delete n words at the right side of the cursor.
  • x will delete the character on which the cursor is positioned
  • :n moves to line n of the file.
  • :w will save (write) the file
  • :q will exit the editor.
  • :q! forces the exit when you want to quit a file containing unsaved changes.
  • :wq will save and exit
  • :w newfile will save the text to newfile.
  • :wq! overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account.
  • /astring will search the string in the file and position the cursor on the first match below its position.
  • / will perform the same search again, moving the cursor to the next match.
  • :1, $s/word/anotherword/g will replace word with anotherword throughout the file.
  • yy will copy a block of text.
  • n p will paste it n times.
  • :recover will recover a file after an unexpected interruption.

Refer: http://www.tldp.org/LDP/intro-linux/html/sect_06_02.html


[Ruby] Why should we use symbol for key in Hash

Using symbols not only saves time when doing comparisons, but also saves memory, because they are only stored once

1. Symbol and String

– Symbols in Ruby are basically “immutable strings” .. that means that they can not be changed, and it implies that the same symbol when referenced many times throughout your source code, is always stored as the same entity, e.g. has the same object id.

– Strings on the other hand are mutable, they can be changed anytime. This implies that Ruby needs to store each string you mention throughout your source code in it’s separate entity, e.g. if you have a string “name” multiple times mentioned in your source code, Ruby needs to store these all in separate String objects, because they might change later on (that’s the nature of a Ruby string).

2. Use in Hash

– If you use a string as a Hash key, Ruby needs to evaluate the string and look at it’s contents (and compute a hash function on that) and compare the result against the (hashed) values of the keys which are already stored in the Hash.

– If you use a symbol as a Hash key, it’s implicit that it’s immutable, so Ruby can basically just do a comparison of the (hash function of the) object-id against the (hashed) object-ids of keys which are already stored in the Hash. (much faster)

Downside: Each symbol consumes a slot in the Ruby interpreter’s symbol-table, which is never released. Symbols are never garbage-collected. So a corner-case is when you have a large number of symbols (e.g. auto-generated ones). In that case you should evaluate how this affects the size of your Ruby interpreter.


If you do string comparisons, Ruby can compare symbols just by their object ids, without having to evaluate them. That’s much faster than comparing strings, which need to be evaluated.

If you access a hash, Ruby always applies a hash-function to compute a “hash-key” from whatever key you use. You can imagine something like an MD5-hash. And then Ruby compares those “hashed keys” against each other.

Refer: http://stackoverflow.com/posts/8189435/revisions

[Docker] Error: Can’t not access internet from container in Ubuntu 14.04

When I use Docker, I create container from image (Ex: Ubuntu:14.04) and ssh to this container by command:

docker run -it <image_id> bash

When I ssh to container, I used command ping google.com 

but I get error:  ping: unknown host google.com

To fix it, I have 2 solutions, but first, you must get DNS of your machine by command


Result in my machine:


=> I’ll use DNS:

1. Solution 1

– Go to file sudo nano /etc/docker/daemon.json and add your DNS to this file

    "dns": ["", "", ""]

Save it and restart docker by command: sudo service docker restart

2. Solution 2

solution 1- Go to file sudo nano /etc/default/docker, uncomment and add your DNS to this row:

DOCKER_OPTS="--dns --dns --dns --ip-masq=true"

Save it and restart docker by command: sudo service docker restart

If when restart, you get error:

docker stop/waiting
start: Job failed to start

or error:

stop: Unknown instance: 
start: Job failed to start

– You shoud go to file sudo nano /etc/default/docker, and comment row

DOCKER_OPTS="--dns --dns --dns --ip-masq=true"

and try again with solution 1.

==> After you try adding DNS successfully, you can ssh to container and run

ping google.com once again. And this is my result after try fixing with solution 1

root@2beb109bc7b3:~# ping google.com
PING google.com ( 56(84) bytes of data.
64 bytes from static.vnpt.vn ( icmp_seq=1 ttl=56 time=1.06 ms
64 bytes from static.vnpt.vn ( icmp_seq=2 ttl=56 time=1.51 ms
64 bytes from static.vnpt.vn ( icmp_seq=3 ttl=56 time=2.32 ms
64 bytes from static.vnpt.vn ( icmp_seq=4 ttl=56 time=1.19 ms
64 bytes from static.vnpt.vn ( icmp_seq=5 ttl=56 time=1.55 ms

That’s all. Thank you for reading. 🙂

[Docker] Error Cannot connect to the Docker daemon. Is the docker daemon running on this host?

In Ubuntu, when I install Docker successfully, I try running with command:

docker images #show all images in local

but it has error

Cannot connect to the Docker daemon. Is the docker daemon running on 
this host?

because it doesn’t have permission of root. I can run it by command

sudo docker images

Another way, we can config it to needn’t use sudo

  • Step 1: go to /var/run

    cd /var/run
  • Step 2: Add permission for file docker.sock
    sudo chmod -R 776 docker.sock

And now, we can use command docker images for shorter code


Error: AbstractController::DoubleRenderError when render in Rails

When work, I’ve had bug when use 2 render error 404 and 422. 

When I go to link: http://localhost:3000/products/cloths.js. Firstly, controller will understand params {name=cloths.js} and will select in database with name=cloths.js and not found any record with that name, then the website will use function render_404.

After that, website will check 422 error because the link has .js and use function render_422. At this time, this error will appear:

AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like “redirect_to(…) and return”.)

To fix error, I use this code in function render after

self.response_body = nil

It will

# Clear the previous response body to avoid a DoubleRenderError
# when redirecting or rendering another view

Example for my problem:

In function 404, which render before:

def render_404
   if request.xhr? || request.format.json?
     render json: { meta: { message: "404 not found" } }, status: :not_found, layout: false
     render file: path_to_error_file(404), status: :not_found, layout: false

And in function 422, which render after and has error  AbstractController::DoubleRenderError

def render_422
  self.response_body = nil
  if request.xhr? || request.format.json?
    render json: { meta: { message: "422 Error" } }, status: :422_error, layout: false
   render file: path_to_error_file(422), status: :422_error, layout: false, 
      content_type: 'text/html'

Access remote server with local phpMyAdmin in Ubuntu


Default, the login page of phpmyadmin have only username & password. If you want to add some remote server you can follow by some steps:

  1. Find file “/etc/phpmyadmin/config.inc.php”
  2. Find row “/* Advance to next server for rest of config */” and add some configs below “$i++;”

/* Advance to next server for rest of config */

/* some additional configs  */
$cfg[‘Servers’][$i][‘host’] = ‘’;
$cfg[‘Servers’][$i][‘user’] = ‘mery_box’;
$cfg[‘Servers’][$i][‘password’] = ‘TewKszmceNQA’;
$cfg[‘Servers’][$i][‘auth_type’] = ‘config’;

And the result:


Show log of MySQL when running query in rails

When you run query to select, update, insert… in ruby, you can show log realtime when query is running.

First, go to MySQL and run:

show variables like '%log%';

And find record with variable_name =  general_log_file and get its value. In case, value is /var/lib/mysql/OFF016.log


Then, set

set global general_log = 1;

Then, open terminal and run:

sudo tailf <value>

In case is:

sudo tailf /var/lib/mysql/OFF016.log

Error: “#1071 – Specified key was too long; max key length is 767 bytes” when import mysql db

When import db to MySQL, if it has error: #1071 – Specified key was too long; max key length is 767 bytes.

Go SQL command to run some query to fix it.

use my_database_name;

set global innodb_large_prefix=on;

set global innodb_file_format=Barracuda;

set global innodb_file_per_table=true;

And import again. 🙂


You can also watch video to fix it easily:

Fixed: 767 bytes error video

Paperclip AV Transcoder can not upload video

When use gem paperclip-av-transcoder to upload video and get error:

Av::UnableToDetect - Unable to detect any supported library:
  av (0.9.0) lib/av/cli.rb:10:in `initialize'
  av (0.9.0) lib/av.rb:24:in `cli'
  paperclip-av-transcoder (0.6.4) lib/paperclip/paperclip_processors/transcoder.rb:14:in `initialize'

=> May be your computer missing ffmpeg and libav-tools

To install them:

// libav-tools
sudo apt-get install libav-tools

// ffmpeg
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg gstreamer0.10-ffmpeg


And if after install them, we get error:

Av::CommandError - error while running command avconv -loglevel quiet -i

Let’s uninstall them and reinstall ffmpeg static build

// Uninstall  
sudo apt-get remove ffmpeg  
sudo apt-get purge libav-tools

// install ffmpeg static build
wget http://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz
tar xf ffmpeg-git-64bit-static.tar.xz
cd ffmpeg-git-64bit-static
sudo mv ff* qt-faststart /usr/bin/

Can’t show group chat Skype in Ubuntu 14.04

I installed Skype in Ubuntu 14.04,  but when I logged in to Skype, I only show and chat with my friends and can’t show any groups, I fixed it by the way:

Open chat box with any my friend and type:

/dumpmsnp   #show details about your connection

And I showed log:

System: MSNP: Connection Data:
 * Status: LoggedOut

Ref:It’s LoggedOut   => It is problem

To solve it, type:


Then log out and restart Skype. After that, type again:


And I see:

System: MSNP: Connection Data (MSNP24):
 * Status: LoggedIn

And I also see all my groups in Skype, and the issue was solved.

Ref:  https://community.skype.com/t5/Mac-archive/New-chat-groups-not-showing-up-in-client/td-p/3206434