The intelligence of machines and the branch of computer science which aims to create it

Artificial Intelligence Journal

Subscribe to Artificial Intelligence Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get Artificial Intelligence Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Artificial Intelligence Authors: William Schmarzo, Ed Featherston, Dan Blacharski, Corey Roth, Jnan Dash

Related Topics: Artificial Intelligence Journal, Citrix Virtualization Journal, Ubuntu Linux Journal, SEO Journal, Haiti EarthQuake

Blog Feed Post

Slow Down and Make Yourself Faster – Tips for the Terminal

I spend most of my day typing arcane things into black windows with green text, as such I spend some time looking for ways to eliminate keystrokes.

I’ve been using bash and vim for more that 10 years and I continue to learn more features that I can use on a daily basis. I’ll give you some tips at the end, but the moral of the story is learn how to make better use of your tools.

How to Get Better

1) Anytime you’re typing the same thing more than once, ask yourself if there’s a better way to do it. If you don’t know, stop what you’re doing and ask Google. Maybe you should be using awk or sed to do some search and replace job. Maybe you can just do it in vim. If you’re in vim already, do you know how to work on more than one file at a time?

2) Anytime you want to do something you’ve done before (ok, that’s the same as typing it again, right?) think about how to do it faster. You can use ^R to search your bash history, you could write a shell routine, you could write a script, that script could be parametrized.

3) When you want to do something involving multiple files, use find. Really, read the man page for find. It’ll chmod, chown, rm and more all the files you can find with it.

4) Keybindings! Start learning how to do fancy things with single keystrokes. If you’re holding down the arrow keys (or Delete/Backspace), or pounding them repeatedly, there’s probably a better way to get where you’re going. Start of the line, end of the line, delete a word, delete through the end of the line. There are keystrokes for all of these and more in both your shell and editor.

5) Know how to navigate and customize your environment. This is all personal preference, but I keep certain tasks on certain spaces (virtual desktops), user shells in one tabbed window, root shells in another. All of my shells now run screen. And my latest bit of learning was tabs for vim to edit multiple files, this lets me stay organized and move between tasks quickly. You’ll need to figure something out for yourself, but always think about how your environment could be better or faster. Most of these settings will happen in dot files, so you’ll probably want to have an easy way to deploy them to new machines. I use a skel.tgz that I keep on one of my servers, using GitHub or Bitbucket might be another good idea.

Ok now here’s some tips and tools I frequently use:

bash
1) Keybindings:
* ^R – search your history
* ^A – start of the line
* ^E – end of the line
* ^K – delete through the end of the line
* esc-D – delete through the end of a word
* esc-backspace – delete back to the start of a word

2) .bashrc collected from who knows where, some of this may be standard in your distro:
"""
export VISUAL=vim
# Screen needs this
alias vi=vim

# If not running interactively, don’t do anything
[ -z "$PS1" ] && return

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval “$(SHELL=/bin/sh lesspipe)”

# set variable identifying the chroot you work in (used in the prompt
below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we “want” color)
case “$TERM” in
xterm-color) color_prompt=yes;;
ansi) color_prompt=yes;;
screen) color_prompt=yes;;
screen-bce) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability;
turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it’s compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi

if [ "$color_prompt" = yes ]; then

PS1=’${debian_chroot:+($debian_chroot)}\[33[01;32m\]\[email protected]\h\[33[00m\]:\[33[01;36m\]\w\[33[00m\]\$

else
PS1=’${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ ‘
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to [email protected]:dir
case “$TERM” in
xterm*|rxvt*)
PS1=”\[\e]0;${debian_chroot:+($debian_chroot)}\[email protected]\h: \w\a\]$PS1″
;;
*)
;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
eval “`dircolors -b ~/.dir_colors`”
alias ls=’ls –color=auto’
fi

# enable programmable completion features (you don’t need to enable
# this, if it’s already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
. ~/bin/django_bash_completion
echo “Extended Complete enabled”
fi

# Get out of a symlinked path
# this was a case of repeating the same command
alias here=’cd `pwd -P`’

# I want anything in my bin to come first
export PATH=~/bin:${PATH}:/usr/sbin:/sbin

# I frequently grep for a string in all of a particular type of file
# under the current directory
function type_find {
TYPE=$1
shift 1
find ./ -name \*$TYPE -exec grep -H $* {} \;
}

# Mostly python files
function py_find {
type_find py $*
}

“”"

3) .dircolors (this looks good on a black screen, mostly it subs cyan for the usual dark blue)

"""
# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.

# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.

# Below, there should be one TERM entry for each termtype that is
colorizable
TERM linux
TERM linux-c
TERM mach-color
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM dtterm
TERM xterm
TERM xterm-color
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-w
TERM vt100
TERM Eterm

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 00;36 # directory
LINK target # symbolic link. (If you set this to ‘target’ instead of a
# numerical value, the color is as for the file pointed to.)
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file

# This is for files with execute permission:
EXEC 01;32

# List any file extensions like ‘.gz’ or ‘.tar’ that you would like ls
# to colorize below. Put the extension, a space, and the color init
string.
# (and any comments you want to add after a ‘#’)

# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32

# archives or compressed (bright red)
.tar 01;31
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.jar 01;31

# image formats
.jpg 01;35
.jpeg 01;35
.gif 01;35
.bmp 01;35
.pbm 01;35
.pgm 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.mpg 01;35
.mpeg 01;35
.avi 01;35
.fli 01;35
.gl 01;35
.dl 01;35
.xcf 01;35
.xwd 01;35

# audio formats
.ogg 01;35
.mp3 01;35
.wav 01;35
“”"

What’s in my ~bin?
1) django_bash_completion – from extras/django_bash_completion in the django distribution

2) [email protected]:~/bin$ cat pep8_check.sh
# I don't like the line length errors
find ./ -name \*py | xargs pep8 --show-source -r --ignore=E501

3) Other scripts of little use to anyone else

vim
1) Installed plugins:
* Supertab – http://www.vim.org/scripts/script.php?script_id=1643
* desert color theme (looks good on a black terminal)

2) .virmrc I particularly like the mappings for dealing with tabs, as that is the newest addition:

"""
"The default leader is '\', but many people prefer ',' as it's in a
standard location
let mapleader = ','

colors desert
set ts=4
set sw=4
set et
set ai
syntax on
set hlsearch

:function! Go_wide()
:% s/,/ /g
:set ts=40
:set nowrap
:set ss=5
:endfunction

nnoremap <C-p> :set invpaste paste?<CR>
set pastetoggle=<C-p>
set showmode

” Only in python files
au BufRead,BufNewFile *.py,*.pyw highlight OverLength ctermbg=red
ctermfg=white guibg=#592929
au BufRead,BufNewFile *.py,*.pyw match OverLength /\%80v.\+/

” Making it so ; works like : for commands. Saves typing and
eliminates :W style typos due to lazy holding shift.
nnoremap ; :

“clearing highlighted search – by typing “,/”
nmap <silent> <leader>/ :nohlsearch<CR>

” Change Working Directory to that of the current file
cmap cwd lcd %:p:h
cmap cd. lcd %:p:h

” tabbing stuff ^h and ^l for next/prev tabs
map <C-h> :tabp<cr>
map <C-l> :tabn<cr>
imap <C-h> <esc>:tabp<cr>
imap <C-l> <esc>:tabn<cr>
map <C-n> :tabnew

“”"

The post Slow Down and Make Yourself Faster – Tips for the Terminal appeared first on Hurricane Labs.

Read the original blog entry...

More Stories By Hurricane Labs

Christina O’Neill has been working in the information security field for 3 years. She is a board member for the Northern Ohio InfraGard Members Alliance and a committee member for the Information Security Summit, a conference held once a year for information security and physical security professionals.