You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because I use vim or vim clones like Sublime Text 2's vintage mode, I prefer to use the console in vi mode, rather than the default emacs.
A lot of the information here is encapsulated in the ever knowledagble Peter Krumnis' bash vi editing mode cheat sheet. I'm going to repeat bits and pieces here and there because that's how I learn.
This contains commands shamelessly stolen from many authors. I will try to list all of them at the end of the file, but if I've missed someone, let me know.
Legend
UC/Use Case : a practical/frequently used example of the command in action (I find it easier to add commands into my daily workflow if I have an example that uses them in something I often do)
The Prompt
Navigation
cd - Go back to your $HOME directory (~)
cd - Go back to the previous directory you were in
Master level navigation with pushd and popd
Still learning this, good introhere. I now use z, and dirs to navigate around. It's a better fit for me.
Command history
Working with your previous command
!! - Last command
!foo - Run most recent command starting with 'foo...' (ex. !ps, !mysqladmin)
!foo:p - Print command that !foo would run, and add it as the latest to
!$ - Last 'word' of last command ('/path/to/file' in the command 'ls -lAFh
/path/to/file', '-uroot' in 'mysql -uroot')
!$:p - Print word that !$ would substitute
!^ Print first word of last command
!* - All but first word of last command ('-lAFh /path/to/file' in the command
'ls -lAFh /path/to/file', '-uroot' in 'mysql -uroot')
UC source your profile after editing it:
vim ~/.bash_profile
source !*
!:p - Print words that ! would substitute
!:t filter out the last word in a long sequence. Great for URLssource
!:1 grabs the 2nd (bash works from a zero index) command so install in our example
!:0-1 grabs from the 1st to the 2nd comand so pip install in our example
Replacing text
^foo^bar - Replace 'foo' in last command with 'bar', print the result, then
run. ('mysqladmni -uroot', run '^ni^in', results in 'mysqladmin -uroot')
Working with History [2]
note you can combine the commands above with past commands. For example: rm !5429$ will run rm on the last word of command #5429
history - Print all previous commands you've run
(use history | grep "ssh" to search for command and make your life easier)
!-4 - run the command that is 4 commands back from your last command
!2 - Run command with the id of 2.
!'string' - run the last command that begins with 'string'
Executables
Find out Where a program is located
which subl - Outputs path where executable subl is located e.g. usr/local/bin
Find where a program is located, and cd to that directory
We are going to use the $() evaluator to nest several bash expressions along with dirname which gives the directory path of an executable.
cd $(dirname $(which drush))
cd `dirname $(which subl)` change the current directory to the path of subl e.g. (cd /usr/local/bin) [4]
Note: rename is not installed by default on some systems (Mac Os included) user your favorite package manager to install it. Homebrew users can just brew install rename.
This is a pretty simple use case, but I find that I frequently have to change file extensions/names on large groups of files. This is a lot better than manually using mv everything.
Find the type of a file without an extension:
file bar will output something like bar gzip compressed data, from Unix, last modified: Fri Mar 1 08:33:16 2013
Customizing
.bash_profile = sourced by login shell,
.bashrc = sourced by all shells,
.bash_aliases = should be sourced by .bashrc/.bash_profile
Making changes
Any modifications you make to your profile will only show up after you:
A: open a new shell window
OR
B: source your profile with source ~/.bash_profile (preffered IMO)
Use bash in "vi" editing mode
Add the following to your .bash_profile
set -o vi
Be warned that this takes some getting used to
Avoid duplicates in your history:
export HISTCONTROL=ignoredups
SED
Grep and Sed files
search the current directory for a string, and replace it with another (note, probably better to use perl's built -p -i -e in search, see recipes for an example)
grep -lir 'id="left"' * | xargs sed -ie 's/id="left"/id="left" class="sidebar"/'
Redirecting, Piping, and passing commands
Redirecting
You can redirect the out put of a command into a file, or the contents of a file into a command with the < and > characters, respectively.
Writing output of command to file with >
ls > files.txt writes the output of hte ls command to the file files.txtecho '# This is a new markdown file' > memoir.md adds the echoed text to a new markdown file (overwriting any existing file with that name).
echo "the end." >> memoir.md adds "the end." to the end of the memoir.md file.
Piping
To pass the output of one command to another, connect them with the | character.
E.g. ls -C | wc - w Returns the number of files in your current directory.
to redirect output, and display on screen use the tee command *
Executing commands
You can use the output of commands within other commands by wrapping the command in a `character.
e.g. vim `which drush` passes the outputh of the which command (in this case a path: /usr/local/bin/drush) to vim, which then opens the file.
to nest multiple commands, use $() Explanation*
cd `dirname $(which subl)` change the current directory to the path of subl e.g. (cd /usr/local/bin) [4]
Interactive Calculations with BC
bc to enter basic calculations mode (there is a lot of stuff you can do here)
Curl is the swiss army knife of http requests -- actually, let's be serious: it's the optimus prime of http requests. As such it deserves it's own section (although it may show up elsewhere on ocassion).
Help
I've found curl --manual to be a lot more helpful than the linux man page (man curl). Feel free to | it to your favorite editor.
delete the line of the commit that you want to remove
Git will rebase (hopefully without errors)
(this is applicable to multiple commits as well, provided that the changes don't intersect with a lot of other commits)
git reset --hard origin/master makes local reflect master.
2. That has been pushed
coming soon
3. A rebased merge (a feature branche's commits have been added on to masters commit history, so there is no clear commit to go back to) | source
git reflog# find commit before merge (in this case HEAD@{5})git reset --hard HEAD@{5}
Diff
Submodules
Batch update submodules
Init and Update all submodules in a repo:
git submodule foreach git submodule initgit submodule foreach git submodule update
Everyone on IRC is identified by a Nick, wether they choose so or not (it usually default to the username on your computer). You can set a nick by using /nick <name>. The only issue with this is that anyone can use your Nick, if they grab it first. To prevent this, you can register a Nick (instructions here).
Talking
It's a convention to preface messages to users with their nick. So, if i'm talking to a using named "foobar" I would chat foobar: that's correct. You can use tab completions in IRSSI to make this faster.
google() {
search=""
echo "$1"
for term in $*; do
search="$search%20$term"
done
# changing to ``open`` instead of ``xdg-open`` for OSX
open "http://www.google.com/search?q=$search"
}
# Mac os x, "urls.txt" contains a page url on each line
while read p; do open -a Google\ Chrome $p; done < test.txt
Find recently modified file and move them somewhere
Great for those "oh, I didn't mean to untar to my user directory" moments. Not that I ever have those ^^
Find All jpeg files that have been modified in the past 22 minutes and move them to the /tmp directory
find . -name "*.jpg" -mmin -22 -print0 | xargs -0 -I {} mv {} /tmp
"curl" following the contents of the url https://api.github.com/repos/nicktomlin/laptop/tarballfollowing symlinks (-L) to a local file -o : nt-dotfiles.tar.gz
pass (|) that local tar to tar stripping the first level of components --strip-components=1 (in this case a containing folder) and extract (-x) them to a destination directory (-C) that is the result of the command (`echo $HOME`) (this could be any valid path)
Simple simple:
Download the tar directory at the given url (following symlinks with -L) and Expand it in the currenty directory
curl -L https://api.github.com/repos/nicktomlin/laptop/tarball | tar -x
Find files (by timestamp) in relation to another file with the -newer flag
-i in place editing (dont' redirect changes to STDOUT)
-i".bak" edit in place, but make a backup file with the extension specified in quotes ". Optionally '-i.bak' should work for most systems.
Replace a string
sed -i.bak 's/foo/bar/g'
Delete the 18th line of a file, creating a backup of the file with the extension ".bak":
sed -i".bak" '18 d' /path/to/somefile.txt
Delete lines containing a string:
sed -i".bak" '/foo/d' /path/to/somfile.txt
Combine with find to replace a string (thanks tom!):
find . -name \*.php -exec sed -i "" -e "s/orgVar:/newVar:/g" {} \;
Combine with ack sourceack -l orgVar | xargs sed -i "s/orgVar/newVar/g"
Find string, and append afterwards
sed '/<title>/a\
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">' 1.txt
Remove leading zeroes from filename
for FILE in *; do mv $FILE echo $FILE | sed -e 's:^0*::'; done
Append a snippet of code after a line
sed -i.bak -e "100a\\
drupal_add_js('sites/all/themes/sndev/js/mongoose_cookie.js', 'file');" template.php
The [[]] is necessary for shell compatibility. I don't know any more than that right now :)
Numerical comparison:
i=0; if (( i <= 1 )); then echo "smaller or equal"; else echo "bigger"; fi
file check flags:
-e: file exists
-f: regular file (non directory)
-d: directory
-s: non-zero file
-x: execute permission
Loops
Loops can be run within a script/function as multiple lines:
for i in {1..5};
do
echo "current number is $i"
done
or as a single line (in a prompt), by separating each line with a semicolon:
for i in {1..5}; do echo "current number is $i"; done
Loops on files:
Let's loop over ever .txt file in the directory, and add "edited by" + the current shell username to the end of each file. Note the use of >> to add to the end of a file, and the grave accent mark ` to run a shell command within a script
for i in *.txt
do
echo "edited by `whoami`" >> $i
done
Or a single line version:
; for i in *.txt; do; echo "edited by `whoami` " >> $i; done
Control Structures
If
@todo
Check if program exists:
if hash rename 2>\dev\null
then
# do stuff
fi
Making it executable
chmod a+x
Check if a file is executable with file
*nix has a built in file command that will give you pertinent details about the file (what architecture it is compiled for and if it is executable or not)
I use ST2 in Vintage mode as my primary editor. This is to reduce the confusion of looking at a vim cheatsheet and realizing ST2 has a slightly different implementation of a feature.
Ex Mode
Vintage is pretty decent to start off with, but to use some the more awesome vim commands, you should install sublime EX (available in package manager or this repo)
Preferences
// start in command mode (and stop accidentally deleting swaths of code!)
"vintage_start_in_command_mode": true,
// use system clipboard in vintage mode
"vintage_use_clipboard": true
Differences from vim
Recipes
Multiple Selection
cmd + shift + l is your ticket here.
Edit Multiple Lines
This one is a little tricky, but pretty darn awesome (esp. when dealing with lines of uneven length) when you get the hang of it:
Select the lines you want to edit in visual mode
cmd + shift + l (enable multiple cursors)
v ( twice! - to exit visual mode)
i (or any other combination to enter insert mode)
Edit all occurrences of word
Similar to the normal ST2 method, but with a little multiple vim thrown in
move cursor over target word
ctrl + cmd + g (select all occurences)
cmd + shift + l
v to exit visual mode
Change the content of all <p>
move cursor over target tag <p>
perform the steps above
c i t to change the content of selected tags
Visual Mode
o reverse direction of selection (helpful if you've accidentally missed one line at the opposite end of your selection)
Opening files at a given line number / function from command line link
:w write file
:q close
:wq write and close file
:q! close, discarding any unsavd changes
Some time saving shortcuts (in command mode):
ZZ write and close file
ZQ close filew without saving
Command line
Open file at a specific line:
foo.txt +200
Pipe command from stdin into vim for editing
- tells vim to look at stdin for input.
ls | vim - opens the results of ls in vim. Mix and match with all your favorite commands ^^
Efficiency
Switching Editing modes
Use ctrl + c instead of esc to quickly exit insert mode
Do note that ctrl + c immediately cancels whatever action your are doing (esc is graceful in stopping commands), so it may not interact well with certain motions. You can make ctrl + c function like esc by adding the following to your .vimrc: vnoremap <C-c> <Esc>
Moving Around [1]
Locations
0 Beginning of line
^ First character of line (so wonderful for lines with a lot of indentation)
$ End of line
Lines
40| move to the 40th "column", handy for moving to themiddle of a line if you have text wrapping at 80 characters
Visual Mode
v start highlighting characters
V start highlighting lines (I missed this one for so long)
Characters
t c move the cursor to the space before next 'c' character after the current cursor
T c move the cursor to the space before the nearest'c' character before the current cursor
f c move the cursor to the nearest 'f' character before the current cursor
F c move the cursor to the nearest 'f' character after the current cursor
; repeat the last find character command
, redo the last find character command in the opposite direction
Efficient Deletes
Remember, you are able to combine most vim shortcuts/motions with delete. So d + $ deletes to the end of the line, d 4 wdeletes four words in front of the cursor.
In Command Mode
Words
ea (append at end of word)
ce (delete word after cursor and go into insert mode)
cb (delete part of word before cursor and go into insert mode)
c i w (delete currently cursored word and go into insert mode)
Characters
r (change a single character)
d f ) (delete to the next ")" character -- and delete the character itself)
d F ) (delete to the previous ")" character)
d t ) (delete to before the next ")") [5]
ci { OR ci " OR ci ' (change the next bracketed/double/single/etc quoted text)
cit(delete within html tags)
Lines
c^ delete from cursor position to beginning of line and go into insert mode)
C (delete delete line after cursor and go into insert mode)
Blocks
Editing multiple lines with block mode is a little complex, but once you get the hang of it it is SO much easier than doing it manually. Think about commenting out lines in a bash script ;_;
Use ctrl + v to enter visual block mode.
Select the lines/areas you want to edit using your normal motions
Press I to go into insert mode
Make your edits (will only show up as one line, don't worry)
Press esc, wait a second or two, and see your edits! (Note, you cannot use ctrl+c to exit out of insert mode as that immediately cancels whatever action you are in. If you are in the habit of using that to exit insert mode, and want to keep the magic alive in visual block mode, remap ctrl + c to function like esc with the following line in your vimrc vnoremap <C-c> <Esc>source)
Copy and paste
y - yank
2 + y + w - Yank 2 words
Running shell commands from VIM
: opens up the command pallete
! allows you to execute bash commands
!! re-runs the last external command (handy if you are constantly having to re-run something)
% refers to the open document.
r outputs the command to the current buffer
Ex: Run a python script you are editing.
:!python % enters shell mode and prints
Ex: print the path to the current directory in the document
:r !pwd
Ex: save a read-only file without having to exit and use sudo source
:w !sudo tee % (essentially, copy the current file, and write it as sudo)
Mutt, Pine, and others work well. But I really really like vmail.
Reading
,u Refresh Inbox
,* Star a message
,# Delete a message
,e Archive a message
,r Reply to a message
,a Replly-All to a message
,f Forward message
Composing
,c Compose Message
,q Exit composition mode
,vs Send Message
Saving a draft
:w draft_filename.txt Saves a draft in current directory
Reloading a draft
:e draft_filename.txt Loads draft into email buffer
Sending from the command line
vmailsend < draft_filename.txt
Generating Contacts
vmail -g <number of emails> Generates list from past emails where <number of emails is a number. E.g.: vmail -g 300 Generates list from past 300 emails (pass any number you want as an arguement). Running vmail -g without passing a number scans 500 messages by default.