VI Editor

Paragraph
Next – }
Previous – {

Sentence

Next sentance – )
Previous sentance – (

Inserting and appending text

inserts text to the left of cursor – i
nserts in the beginning of line -I
appends text to right of cursor -a
appends to the end of line- A

Adding new line

add a new line below the current line -o
adds a new line above the current line – O

file

Go to end of file – :$
on chacter forword – :w
One word forword – :W
go to a line number – :ine_number
display file info – :^g

Moving Cursor in File

Left – h
Right – l
Up – k
Down – j

Line

Beginning – ^ or B
end – $

Deleting the text :

deletes text above the text – x
deletes text character on right of cursor- X
deletes line- 20 20d
deletes current line- dd
delete till end of current -line. D

Replacing a character & word

replace the character above the cursor – r
replces characters until Esc is pressed – R
replaces the word from cursor to the end indicated by $ sign – cw
replaces till end of line – C

Substitute

subistutes current charcater – s
substitutes entire line – S

Undo last changes

undo last change – u
undo changes to the current line – U

Copy and pasting lines

copys the current line into buffer – yy
copies 5 lines from the current line – 5yy
pastes the current buffer – p

Searching

Searches for the word name in the file – /name
n continues search forward – n
N searches backwards – N

Saving

saves the text does not quit -:w
saves & quit the editor – :wq!
save ZZ
Quit without saving – q!
Search & Replace – s/<search>/<replace>/g .

Repeating last command .

Recovering a unsaved vi file – vi -r filename

Create A New Coding Standard

Please follow the below given steps

1) Find out the location of PHP_CodeSniffer install .

locate PHP_CodeSniffer

2) Change the directory to install

cd path/to/PHP_CodeSniffer

3) Create a directory that represent the new coding standard and change the directory to it

sudo mkdir PHPCodezStandard

cd PHPCodezStandard

4) Create the directory sniff that is used to store all the sniff files for this coding standard and change the directory to it

sudo mkdir Sniffs

5) Create a file ruleset.xml

sudo vi ruleset.xml

6) Paste the below given content in ruleset.xml

<?xml version=”1.0″?>
<ruleset name=”PHPCodezStandard”>
<description>New coding standard.</description>
</ruleset>

Then change the directory to Sniffs

cd Sniffs

7) Create the sniff that require a single php file and it’s name should be end with Sniff.php and should be placed in a subdirectory

sudo  mkdir Commenting
cd Commenting
sudo vi PHPCodezSniff.php

8 ) Paste the below given code in PHPCodezSniff.php

<?php
class PHPCodezStandard_Sniffs_Commenting_PHPCOdezSniff implements PHP_CodeSniffer_Sniff
{
public function register()  {
return array(T_COMMENT);
}

public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr){
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr][‘content’]{0} === ‘#’) {
$error = ‘Hash comments are prohibited; found %s’;
$data  = array(trim($tokens[$stackPtr][‘content’]));
$phpcsFile->addError($error, $stackPtr, ‘Found’, $data);
}

}
}
?>

10) You can use this standard as follows

phpcs –standard=/usr/share/php/data/PHP_CodeSniffer/PHPCodezStandard exception.php

FILE: /var/www/test/exception.php
——————————————————————————–
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
——————————————————————————–
2 | ERROR | Hash comments are prohibited; found #
——————————————————————————–

PHP_CompatInfo Features

  • parse a single file
  • parse a folder/directory
  • parse an array (list of file)
  • ability to give a list of functions to ignore when calculating the version needed
  • ability to give a list of directories to ignore when calculating the version needed
  • ability to go recursively or not into folder to find files
  • ability to reduce or extends list of extensions to parse for PHP code
  • ability to give a list of constants to ignore when calculating the version needed. Available since version 1.2.0
  • ability to get list of functions related to a PHP version (or a subset). Available since version 1.2.0
  • ability to give a list of php modules/extensions to ignore when calculating the version needed. Available since version 1.4.0
  • ability to give a list of php versions to ignore when calculating the version needed. Available since version 1.4.0
  • ability to exclude from parsing scope, some functions, extensions, constants, based on conditionnal code. Available since version 1.7.0
  • ability to know conditional code (such as function_exists) used by php scripts. Available since version 1.7.0

PHP_Compat

PHP_Compat provides missing functionality in the form of functions and constants for older versions of PHP.

The replicated functions are designed to be interchangable with their native equivilants. They have the same signature, same return values and throw the same errors. Each function is unit tested to ensure accuracy.

PHP_Compat is designed for ease of use. It has no dependencies and can be used completely outside the PEAR infrastructure.

Anti Pattern

An anti-pattern (or antipattern) is a pattern that may be commonly used but is ineffective and/or counterproductive in practice

Many anti-pattern ideas amount to little more than mistakes, rants, unsolvable problems, or bad practices to be avoided if possible. Sometimes called pitfalls or dark patterns, this informal use of the term has come to refer to classes of commonly reinvented bad solutions to problems.

Check a directory with PHP CodeSniffer

phpcs /var/www/test/xml

FILE: /var/www/test/xml/index.php
——————————————————————————–
FOUND 2 ERROR(S) AFFECTING 2 LINE(S)
——————————————————————————–
1 | ERROR | End of line character is invalid; expected “n” but found “rn”
2 | ERROR | Missing file doc comment
——————————————————————————–
FILE: /var/www/test/xml/xml.php
——————————————————————————–
FOUND 8 ERROR(S) AND 1 WARNING(S) AFFECTING 8 LINE(S)
——————————————————————————–
1 | ERROR | End of line character is invalid; expected “n” but found
| | “rn”
2 | ERROR | Missing file doc comment
13 | ERROR | Expected “foreach (…) {n”; found “foreach(…)n{n”
15 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 3
16 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 3
18 | ERROR | Line indented incorrectly; expected 4 spaces, found 3
19 | ERROR | Line indented incorrectly; expected at least 8 spaces, found 4
19 | WARNING | Line exceeds 85 characters; contains 92 characters
21 | ERROR | Line indented incorrectly; expected at least 4 spaces, found 3
——————————————————————————–

Check a file with PHP CodeSniffer

phpcs test.php

FILE: /var/www/test/test.php
——————————————————————————–
FOUND 2 ERROR(S) AFFECTING 2 LINE(S)
——————————————————————————–
1 | ERROR | End of line character is invalid; expected “n” but found “rn”
2 | ERROR | Missing file doc comment
——————————————————————————–