A reference for commands that might come in handy

BASH TERMINAL | VIM | GIT | WORDPRESS | OSX?| NODE?| Meteor.js | General JS

 

BASH TERMINAL

grep -rl "string" /path

This performs a “grep” search looking through files that contain the string in quotes–it searches the contents of files. Many arguments are available, but this one uses:

  • -r?(or?--recursive) option is used to traverse also all sub-directories of?/path, whereas
  • -l?(or?--files-with-matches) option is used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given that?grep?stop reading a file at first match with this option).

General JS

Object.keys(obj.Data).length

Gets the length of an object in modern browsers.


history | grep -rl "foobar"

This takes your bash history (history of commands inputted into the bash console) and performs a grep search on it looking for, in this case, “foobar”. It then outputs the command number along with the matching command. You can then execute the command by typing the number followed by a “bang”, ie: “123!”.


find ./ -name 'program.c' 2>/dev/null

Searches current (./) directory for file containing the name ‘program.c’–it searches only the filenames and not the contents. If it encounters any errors (2) it outputs them to disintegration (>/dev/null) where anything sent just disappears.


find?./ -type f -exec sed -i "" 's#\<h4\>Registration Process\</h4\>#\<p\>\<a href="/resources/Flyer.pdf" \>\<span\>Link to Resource\</span\>\</a\>\</p\>\<h4\>Registration Process\</h4\>#g' {} +

Okay, this is a more advanced use of find because once it finds a file, it executes the following sed command upon it. Since this find command basically looks at everything in the current directly (and subdirectories) that is a qualified “file”, that basically turns the sed into a recursive function. Note in the sed command I have used the ‘#’ symbol as a delimiter to make it more legible with my html code. What the example above does is find an existing h4 tag with given text and precede it with some other html code I needed to include in several dozen pages. NOTE: this was done on OS X, a linux OS may have a variation on this syntax.


fg

I am forever finding myself instinctively typing ctrl-z rather that “u” in vim. “ctrl-z” actually sends a process to the background… if you don’t know this it can drive you nuts. In order to bring the process back to the ForeGround simply use the command “fg”. To see all paused, or background tasks, use the command:

jobs

If you have multiple paused tasks you can bring a specific one to the foreground by using it as in argument of fg:

fg 2

sudo -s

After you enter your password it will keep you logged in as su for the session. Don’t ask me what it does technically, I just accept it. Type “man sudo” to get more details.


wget https://raw.github.com/path/to/file.conf

This will download the file from the provided link to the current directory


wget -E -H -k -K -p -e robots=off url ./

This will recursively download the website from the provided link to the current directory; In case robots.txt is disallowing you add -e robots=off

Expanded:

wget --adjust-extension --span-hosts --convert-links --backup-converted --execute robots=off --page-requisites [url] ./

eval "$(fc -ln 432 455)"

This will reexecute a series of history commands from bash– in this example !432 to !455


scp -P2222 file vagrant@localhost:/path/to/where/you/want/thefile

This will copy a local machine file to the vagrant Virtual Machine (VM). The password is vagrant in my instance.


Bash commands in terminal

For a more complete reference go to http://www.softpanorama.org/Scripting/Shellorama/bash_command_history_reuse.shtml

!!

This will repeat the last command WITH arguments. If using zsh you can use tab to fill it in visually.


!S

This will repeat the last command’s last argument. If using zsh you can use tab to fill it in visually.


!:2

This will repeat the last command’s second argument. If using zsh you can use tab to fill it in visually.


!:0

This will repeat the last command (but not arguments). If using zsh you can use tab to fill it in visually.


VIM

There are too many tips for vim to put here so I will just include some useful links


Something I do in all my Linux environments is setup an remap key combo for the key. I like to use the key combo in consecutive order instead. To do this. Go to the root directory “~/” and edit/create the file “.vimrc” which loads on vim load. In this file add the following:

inoremap jk <esc>

Save, close, exit vim. Reopen vim and try it out. Typing , then immediately after should act the same as pressing the key.


:set paste!

Within Vim, use this to paste in cleaned up code that has been copied form say your browser. It cleans up the spacing and indentation and what not. Was told to type the following command to exit the paste mode:

:set nopaste

:%s/foo/bar/g

Find each occurrence of ‘foo’ (in all lines), and replace it with ‘bar’. Alternatively you can add other arguements to command such as: c (ask for confirmation before replace), and I (for case-insensitive).


:%s/foo.\{-}/bar/

This is how you do a wildcard search in vim. Vim has its own dialect of regex. Some things are the same while others may appear wildly different from the perl regex syntax that is most common. Instead of .* use .\{-}.



function! WrapForTmux(s)
  if !exists('$TMUX')
    return a:s
  endif

  let tmux_start = "\Ptmux;"
  let tmux_end = "\\\"

  return tmux_start . substitute(a:s, "\", "\\", 'g') . tmux_end
endfunction

let &t_SI .= WrapForTmux("\[?2004h")
let &t_EI .= WrapForTmux("\[?2004l")

function! XTermPasteBegin()
  set pastetoggle=[201~
  set paste
  return ""
endfunction

inoremap   [200~ XTermPasteBegin()

 

Insert the above code in your .vimrc config file to automatically turn paste mode on when pasting code… and then turn it off immediately after. Its even better than a remapped “toggle” button. Read more about it here.


After a fresh install, I never have persistent vim history working. This post details the fix in the .vimrc file and gives a tip for keeping the undo files tidy. Click to see tips
A ton of other useful tips can be found in this slideshow.


GIT

git init

Creates a git repository (repo) from your current directory.

or…

git init newrepo

Creates a new directory that is a Git repository with the name “newrepo”.


git clone [insert clone https address here] [insert local directory that is destination of clone]

Clones an existing remote git repo to a local directory.


git status

Gives you the current status of your local repo vs the remote repo. It will tell you if you are ahead or behind and by how much. It will also show you files that either have yet to be added to staging, or files that have been added, but not committed yet. If you type:

git add .

It will add all files recursively from the current working directory (provided you are inside a git repo).


git commit -m "Insert message here for reference of what exactly this commit has changed"

This will commit whatever recently added changes have been made. It puts them in the queue for pushing to the remote repo. Be sure to include a good note in quotes to reference exactly what you did commit.


git push

Will push your committed changes to the remote repo updating what is there


git log -p -2

This shows the diff introduced in each commit. You can also use -2, which limits the output to only the last two entries.


git log --follow -p file

This will show you the commit history of a specific file. Very useful if you want to review a file’s history of changes during its lifetime.


For more git commands visit gitref.org.


git branch branchname 

You can create the a branch from a previos commit via hash,

git branch branchname HEAD~3

…or by using a symbolic ref.


WordPress

--- functions-formatting.orig
+++ functions-formatting.php

@@ -57 +57 @@
-function wpautop($pee, $br = 1) {
+function wpautop($pee, $br = 0) {

WordPress was adding line breaks wherever I had hidden form fields in a post. This removes that function. You have to edit this in your wordpress functions file. Mine was located at /wp-includes/functions.php After an update mine was found in wp-includes/formatting.php


echo do_shortcode('[shortcodeNameHere]')

This will execute a shortcode from within a php file itself (rather than from the wordpress wysiwyg editor.


UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');

If migrating your wordpress site to a new url, this script will replace all mentions of your old domain name with the new one… particularly useful for links or images in the content area. ?Permalinks in the navigation are handled from the wp-admin settings area.


OSX

OSX Yosemite really borked my local dev environment and I want to capture the steps to fix it for myself and any other lost souls out there.

First go to this page,?https://discussions.apple.com/docs/DOC-3083,?and follow the instructions therein. ?This should get your local server working on you mac, but you still can’t access it from other locally networked devices for testing, such as an ipad in my case.

Next go to this page ,http://mallinson.ca/osx-web-development/,?scroll down to the highlighted “Yosemite Only” section. Follow these instructions to comment out the following lines:

220 
221     AllowOverride none
222     Require all denied
223 

Voila! It works.


NODE

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

Nvm installs node.js inside your user’s home directory. This is fine for development, but if you want to actually host node applications, you don’t want to install the latest new version of node via nvm and discover that you’ve inadvertently caused your production node app (which can be incompatible with the latest node.js) to stop working. It’s best to install one copy of node globally so that other users can access it, and use nvm to switch between your development versions.

To do this, run the previous?command (entering your user’s password at the prompt). Learn more here.


Meteor.js

# Create dump of mongodb on server running meteor
mongodump -h 127.0.0.1 --port 3001 - APPNAME
# Create dump of mongodb on server running via mup (meteor up)
mongodump
# Restore mongodb from dump folder
mongorestore -h 127.0.0.1 --port 3001 -d meteor dump/APPNAME

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *