Working efficiently with the command line
The following are some tips to help make working from the command line (CLI) efficient.
What can take numerous clicks in a GUI, can be replaced with a one-liner from the command line. Your CLI (or really your shell) can help you in many ways.
Command history
To repeat previous commands you can use up and down arrow. But the most powerful tool is history search - Ctrl+R
in most shells.
To automatically autocomplete commands from the history pressing up or down arrow, add this in your ~/.bashrc
file. Type for example "drush" and press up for suggestions:
# Bind up/down arrow to history search
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
# Settings for history function
export HISTCONTROL=ignoreboth:erasedups
export HISTIGNORE='&:e:c:l:ca:cd:cd -'
Tab completion
To not have to type or remember long commands, most commands offer tab completion. You type part of an option or an argument, and then hit tab to complete it. (You can also type part of the command name to complete it.) You can also use completion to list all options or arguments if you have forgotten them.
Both Drush or Composer supports tab completion.
Aliases
To not have to repeatedly type long commands, you can create aliases in Unix (Linux and macOS) functioning as shortcuts. You can also string together several commands in an alias, automating tedious tasks. You can call your aliases anything you want, as long as another command with that name doesn't already exist.
In these examples DDEV is used, but it works just the same way with other programs.
DDEV, Drush, and Composer aliases
Here is an example of a few aliases. Add it in your ~/.bash_aliases
file:
# DDEV, Drush, and Composer aliases
alias dart='ddev start'
alias dop='ddev stop'
alias drush='ddev drush'
alias din='ddev drush install'
alias dun='ddev drush pm:uninstall'
alias duli='ddev drush uli'
alias creq='ddev composer require'
alias crem='ddev composer remove'
Now run this command to make it take effect:
source ~/.bash_aliases
You should now be able to enter a DDEV installation, type dart
and hit Return after which DDEV should start.
Most docker based dev environments provide build-in aliases and allow you to create your owns, there usually based on bash scripts or gnu-make:
- DDEV: https://ddev.readthedocs.io/en/latest/users/extend/custom-commands/
- Lando : https://docs.lando.dev/landofile/tooling.html
- Docker4Drupal: https://wodby.com/docs/1.0/stacks/laravel/local/#make-commands
- Docksal: https://docs.docksal.io/fin/custom-commands/
Open web site logged in as admin
This one-liner will generate a one-time-login, and open up the web site in your browser, logged in as admin:
xdg-open $(ddev drush uli)
You can define a destination URL like this:
xdg-open $(ddev drush uli)?destination=/upgrade
Run commands via composer.json
You can run commands with Composer. Just add something like this in your composer.json
:
"scripts": {
"project:reinstall": [
"[ ! -z $(ddev drush core:status --field=db-status) ] && ddev drush cache:rebuild || true",
"ddev drush site:install -v --yes",
"ddev drush cache:rebuild",
"ddev drush user:login"
]
},
Run it with this command:
composer project:reinstall
For details, see Effortless Drupal Reinstallations: A Quick Guide Using Composer.
Use a custom DDEV command
You can extend DDEV with Custom Commands to trigger a series of Drush commands or Unix commands. It works really well, and it's easy to write your own custom command files.
This example adds a custom command which creates a report for a project. Add this in a new .ddev/commands/web/report
file in your project:
#!/bin/bash
## Description: Get a report of Drupal version, updates, etc.
## Usage: report
## Example: 'ddev report'
echo '** Check config status, get versions and important log messages, etc.'
echo ''
echo '* Configuration status'
drush config:status
echo ''
echo '* Versions, db and webserver'
drush core:status | grep 'version'
# see webcommands.example for all available web script variables
echo 'Database :' $DDEV_DATABASE
echo 'Webserver :' $DDEV_WEBSERVER_TYPE
echo ''
echo '* Last 3 log entries with severity minimum Warning'
drush watchdog:show --count=3 --severity-min=Warning
echo ''
echo '** Check for Drupal core and module updates'
composer outdated "drupal/*"
echo ''
echo '** Check for security vulnerabilities'
composer audit
echo ''
Run the command, to get a report about your system:
$ ddev report
** Check config status, get versions and important log messages, etc.
* Configuration status
[notice] No differences between DB and sync directory.
* Versions, db and webserver
Drupal version : 10.1.5
PHP version : 8.1.23
Drush version : 12.1.2.0
Database : mariadb:10.4
Webserver : nginx-fpm
* Last 3 log entries with severity minimum Warning
----- -------------- ------ ---------- -----------------------------------
ID Date Type Severity Message
----- -------------- ------ ---------- -----------------------------------
101 03/Nov 19:16 php Error Error: Call to undefined function
chart_info_default_settings
94 03/Nov 18:06 cron Warning Attempting to re-run cron while
it is already running.
93 03/Nov 18:06 cron Warning Attempting to re-run cron while
it is already running.
----- -------------- ------ ---------- -----------------------------------
** Check for Drupal core and module updates
Color legend:
- patch or minor release available - update recommended
- major release available - update possible
Direct dependencies required in composer.json:
drupal/core-composer-scaffold 10.1.5 10.1.6 A flexible Composer projec...
drupal/core-project-message 10.1.5 10.1.6 Adds a message after Compo...
drupal/core-recommended 10.1.5 10.1.6 Core and its dependencies ...
drupal/metatag 1.26.0 2.0.0 Manage meta tags for all e...
drupal/search_api_solr 4.3.0 4.3.1 Offers an implementation o...
drupal/security_review 2.0.1 2.0.2 The Security Review module...
drupal/site_audit 4.0.0-rc3 4.0.0-rc4 This extension provides Si...
drupal/webprofiler 10.1.1 10.1.3 Extract, collect, store an...
** Check for security vulnerabilities
No security vulnerability advisories found.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion