Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, July 17, 2014

Drush autocomplete in bash shell

View updated version of this blog here.

For shell users like me, Drush autocomplete is a bliss. This blog will tell you how you can autocomplete Drush commands in bash shell. The commands provided here will work for Ubuntu systems, I am not sure whether they will work for other systems as I do not know where Drush is installed for other systems.

  1. Install Drush in your system. If it is already installed, skip to step 2. Otherwise, you can install it from here
  2. Drush already provides you with bash aliases and autocompletion scripts. We just need to place them in right place.
  3. Execute cat /usr/share/php/drush/examples/example.bashrc >> ~/.bashrc This will append required bash aliases to your current bash settings.
  4. Execute sudo ln -s /usr/share/php/drush/drush.complete.sh /etc/bash_completion.d/ This shell script does the smart autocompletion, and here we link the source with existing bash settings. The script does the smart autcomplete job like, suggesting modules to be enabled/disabled.
  5. All settings are done. Just restart the shell ("exit" the current shell and open a new shell) and you are good to go.
List of bash aliases that you will get:
# Aliases for common drush commands that work in a global context.
alias dr='drush'
alias ddd='drush drupal-directory'
alias dl='drush pm-download'
alias ev='drush php-eval'
alias sa='drush site-alias'
alias lsa='drush site-alias --local'
alias st='drush core-status'
alias use='drush site-set'

# Aliases for drush commands that work on the current drupal site
alias cc='drush cache-clear'
alias cca='drush cache-clear all'
alias dis='drush pm-disable'
alias en='drush pm-enable'
alias pmi='drush pm-info'
alias pml='drush pm-list'
alias rf='drush pm-refresh'
alias unin='drush pm-uninstall'
alias up='drush pm-update'
alias upc='drush pm-updatecode'
alias updb='drush updatedb'
alias q='drush sql-query'

You can write "drush en" and then double press "tab" key for suggestions, you will see list of modules.

Update:
The location of drush.complete.sh may vary according to how you install drush. To find where the script resides, execute sudo find / -name "drush.complete.sh". Suppose the location is /home/subhojit/.composer/vendor/drush/drush/drush.complete.sh, you then execute the command sudo ln -s /home/subhojit/.composer/vendor/drush/drush/drush.complete.sh /etc/bash_completion.d/

Saturday, March 1, 2014

Quick and easy Drush sql sync

View updated version of this blog here.

Drush provides an excellent tool sql-sync to sync database between two machines. I needed this tool to synchronize my local database with production database. Simple use of drush sql-sync was slow, I used it with some options and it was easy and quick. I am going to show you how I did it.

You need to create Drush alias for your Drupal site. Create aliases for local and production setup. If you don't know how to create Drush alias, you can learn it from here. I will use an option for sql-sync that can only be used through Drush alias setup.

Here is a sample Drush alias setup: (mysite.aliases.drushrc.php)

<?php

$aliases['local'] = array (
  'root' => '/var/www/mysite',
  'uri' => 'http://mysite.local',
  'path-aliases' =>
  array (
    '%drush' => '/usr/share/drush',
    '%site' => 'sites/default/',
    '%files' => 'sites/default/files/'
  ),
);
$aliases['live'] = array (
  'root' => '/var/www/mysite',
  'uri' => 'http://mysite.com',
  'path-aliases' =>
  array (
    '%drush' => '/usr/share/drush',
    '%site' => 'sites/default/',
    '%files' => 'sites/default/files/'
  ),
  'remote-user' => 'some-user',
  'remote-host' => 'xx.xxx.xxx.xx',
  'source-command-specific' => array(
    'sql-sync' => array(
      'create-db',
      'no-cache',
      'skip-tables-key' => array('*cache*'),
    ),
  ),
);


If you see the live part of the alias code, I have used source-command-specific, it means if I use live as source any Drush specified inside it will be executed, in our case it is sql-sync.

The options I have used:
create-db - Means create new database. Default is, import will be merged with current database.

no-cache - Do not cache sql-dump

skip-tables-key - Do not import cache tables, as they will be created dynamically. Comma separated multiple keys can also be provided. It can only be used through alias file.

After this alias setup execute drush sql-sync:
drush -v sql-sync @mysite.live @mysite.local

Sunday, March 31, 2013

Display current Git branch in bash prompt

View updated version of this blog here.

While working on a project I maintain multiple branches for development. I keep at least two branches, master (the default one) and develop. I work on develop branch and when I complete certain task I merge the work with master branch and push code to the repository. That's how I work :)

At times I forget on which branch I am working, I have to run:

git branch

to know the current branch. I am a bit lazy and I find running the same command again and again annoying. So I thought why not display the current branch in the command prompt. I did google search and found several results. This post is the result of those multiple search results.

Edit .bashrc file. Run the following command to edit .bashrc file

vim ~/.bashrc

You can replace vim with your favourite editor.

At the end of file enter the following piece of code:

# Append current git branch in prompt

parse_git_branch() {

  if ! git rev-parse --git-dir > /dev/null 2>&1; then

    return 0

  fi



  git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')



  echo "[$git_branch]"

}

PS1="${debian_chroot:+($debian_chroot)}\[\033[01;36m\]\u@\h\[\033[00m\]:\[\033[01;32m\]\w\[\033[00m\]\[\033[01;31m\]\$(parse_git_branch)\[\033[00m\]$ "

My bash prompt
My bash prompt
Cool.. eh? :)

PS1 is used to change the bash command prompt.

Inside the PS1 variable you can see I have given certain codes like 01;32, 01;31, etc. These are color codes for bash prompt. A list of colors are available here.

References: