spenthil

stuff.

Terminal Keyboard Wizardry

leave a comment »

The following are some things I have learned to be quicker with terminals while keeping the damage to my hands at a minimum. The tips focus around BASH and Vim keyboard shortcuts, but should have analogs in other environments.

Tweaking OS X and the Terminal Itself.

First thing I recommend doing is remapping caps lock to be a control modifier when held and escape under single keypress. On OS X you can do this by:

  1. Remap the caps lock button to control:
    1. System Preferences -> Keyboard & Mouse -> Keyboard tab -> “Modifier Keys…” button in the bottom left
    2. For “Caps Lock” Select “Control”
  2. Remaps control so just tapping control sends escape, while holding it down acts as a modifier:
    1. Install KeyRemap4MacBook
    2. Got to KeyRemap4MacBook’s settings (found in System Preferences)
    3. In the remapping pane expand “Remap Control_L Key”
    4. Select “Control_L to Control_L (+ When you type Control_L only, send Escape)

Now exiting out of insert mode in Vim and accessing all the control modifiers in both BASH and Vim doesn’t require you to leave the home row.

BASH Shortcuts and Settings

Strictly speaking these shortcuts are available to all applications that use libreadline.

For all the OS X people: Any BASH shortcuts you see that use alt can be done with escape instead. Alternatively, to make this work well in Terminal check the “map option as meta key” preference. Then you can use Option+ for all ALT+ shortcuts and also meta keys like esc .

For a nifty list of bash shortcuts take a look at http://www.bigsmoke.us/readline/shortcuts.

Vim Shortcuts and Settings

Fortunately, Vim is well documented through the help commands (:help). The preference file is really personal taste, but you might be able to get some inspiration from mine:

set nocompatible

func Eatchar(pat)
  let c = nr2char(getchar())
  return (c =~ a:pat) ? '' : c
endfunc

set tags=tags;/
" keep 50 lines of command line history
set history=50
" do incremental searching
set incsearch
" By default Vim searches are case-sensitive. It's often handier if they
" aren't:
set ignorecase
" However sometimes matching case is important. Usually this is with cases
" involving upper-case or mixed-case search strings. This command assumes that
" a search string is case-sensitive if it contains any upper-case characters:
set smartcase
" patterns to put to ignore when completing file names
set wildignore=*.bak,~,*.o,*.swp,*.class

" SCRIPTS
au Filetype html,xml,xsl source ~/.vim/scripts/closetag.vim

" DISPLAY OPTIONS
" show title in console title bar
set title
" smoother changes
set ttyfast
" show the cursor position all the time
set ruler
set number
" display incomplete commands
set showcmd
" 256 color mode for the only app that really uses it
set t_Co=256
set background=dark
colorscheme desert256
" set guifont=Inconsolata
set guifont=Droid
" syntax highlighting
set bg=light
syntax on
set hlsearch
" A status line will be used to separate windows
set laststatus=2
set statusline=%<%F%h%m%r%h%w%y\ %{&ff}\ %=\ lin:%l\,%L\ col:%c%V\ pos:%o\ ascii:%b\ %P
set nowrap

" GENERAL KEYBOARD/MOUSE
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
" page down with <Space> (like in `Lynx', `Mutt', `Pine', `Netscape Navigator',
" `SLRN', `Less', and `More'); page up with - (like in `Lynx', `Mutt', `Pine'),
" or <BkSpc> (like in `Netscape Navigator'):
noremap <Space> <PageDown>
noremap <BS> <PageUp>
noremap - <PageUp>
" [<Space> by default is like l, <BkSpc> like h, and - like k.]
" paste mode - this will avoid unexpected effects when you
" cut or copy some text from one window and paste it in Vim.
set pastetoggle=<F11>
" Screen is complaining about this with the "lib/liblow.c error"
" Disable mouse
set mouse=
"so the a row key works
set t_kd=
set t_kr=
set t_kl
let mapleader = "`"
" make tab in modes ident code
imap <tab> <C-t>
imap <S-tab> <C-d>
vmap <tab> >gv
vmap <s-tab> <gv
nmap <tab> >>
nmap <S-tab> <<
" have the h and l cursor keys wrap between lines (like <Space> and <BkSpc> do
" by default), and ~ covert case over line breaks also have the cursor keys
" wrap in insert mode:
set whichwrap=h,l,~,[,]
"You can make a command so :W invokes sudo:
command W w !sudo tee % > /dev/null

" FILE TYPE OPTIONS
filetype plugin on

au FileType javascript set omnifunc=javascriptcomplete#CompleteJS
au FileType html set omnifunc=htmlcomplete#CompleteTags
au FileType css set omnifunc=csscomplete#CompleteCSS

" PYTHON AND C
let python_highlight_all = 1
" stolen primarily from http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc
" vimrc file for following the coding standards specified in PEP 7 & 8.
au FileType python set omnifunc=pythoncomplete#Complete
" Number of spaces to use for an indent.
" This will affect Ctrl-T and 'autoindent'.
" Python: 4 spaces
" C: 8 spaces (pre-existing files) or 4 spaces (new files)
au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead *.c,*.h set shiftwidth=8
au BufNewFile *.c,*.h set shiftwidth=4
" Number of spaces that a pre-existing tab is equal to.
" For the amount of space used for a new tab use shiftwidth.
" Python: 4
" C: 4
au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=4
" Replace tabs with the equivalent number of spaces.
" Also have an au for Makefiles since they require hard tabs.
" Python: yes
" C: no
" Makefile: no
au BufRead,BufNewFile *.py,*.pyw set expandtab
au BufRead,BufNewFile *.c,*.h set noexpandtab
au BufRead,BufNewFile Makefile* set noexpandtab
" Use the below highlight group when displaying bad whitespace is desired
highlight BadWhitespace ctermbg=red guibg=red
" Display tabs at the beginning of a line in Python mode as bad.
au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/
" Make trailing whitespace be flagged as bad.
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
" Wrap text after a certain number of characters
" Python: 79
" C: 79
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h set textwidth=79
" Turn off settings in 'formatoptions' relating to comment formatting.
" - c : do not automatically insert the comment leader when wrapping based on
"    'textwidth'
" - o : do not insert the comment leader when using 'o' or 'O' from command mode
" - r : do not insert the comment leader when hitting <Enter> in insert mode
" Python: not needed
" C: prevents insertion of '*' at the beginning of every line in a comment
au BufRead,BufNewFile *.c,*.h set formatoptions-=c formatoptions-=o formatoptions-=r
" Use UNIX (\n) line endings.
" Only used for new files so as to not force existing files to change their
" line endings.
" Python: yes
" C: yes
au BufNewFile *.py,*.pyw,*.c,*.h set fileformat=unix
" The following section contains suggested settings.  While in no way required
" to meet coding standards, they are helpful.
" Set the default file encoding to UTF-8:
" set encoding=utf-8
" Puts a marker at the beginning of the file to differentiate between UTF and
" UCS encoding (WARNING: can trick shells into thinking a text file is actually
" a binary file when executing the text file):
"set bomb
" For full syntax highlighting:
let python_highlight_all=1
"syntax on
" Automatically indent based on file type:
au BufRead *.py filetype indent on
" Keep indentation level from previous line:
au BufRead *.py set autoindent
" Folding based on indentation:
au BufRead *.py set foldmethod=indent
au BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

" PERL
" stolen primarily from http://www.perlmonks.org/?node_id=540167
" perl comments start with #, anywhere on the line
au FileType perl set comments=:#
au FileType perl set cinkeys-=0#
au FileType perl set commentstring=#%s
" See ':help syntax'
let g:perl_fold = 2
let g:perl_fold_blocks = 2
au FileType perl set foldlevelstart=2
au FileType perl set foldmethod=syntax
" handy abbreviations
au FileType perl iab ddp use Data::Dumper; print Dumper();<Left><Left><C-R>=Eatchar('\s')<CR>
au FileType perl iab ddd use Data::Dumper; die Dumper();<Left><Left><C-R>=Eatchar('\s')<CR>
au FileType perl iab ddw use Data::Dumper; warn Dumper();<Left><Left><C-R>=Eatchar('\s')<CR>
au FileType perl iab pct print "Content-type:text/html\n\n";<CR>
au FileType perl set makeprg=perl\ -w\ %
au FileType perl nmap <Leader>pf :!perldoc -f <cword><CR>
au FileType perl nmap <Leader>pd :e `perldoc -ml <cword>`<CR>
" autoindent
au FileType perl set autoindent|set smartindent
" 4 space tabs
au FileType perl set tabstop=4|set shiftwidth=4|set expandtab|set softtabstop=4
" show matching brackets
au FileType perl set showmatch
" show line numbers
au FileType perl set number
" check perl code with :make
au FileType perl set makeprg=perl\ -c\ %\ $*
au FileType perl set errorformat=%f:%l:%m
au FileType perl set autowrite
" dont use Q for Ex mode
map Q :q
" paste mode - this will avoid unexpected effects when you
" cut or copy some text from one window and paste it in Vim.
set pastetoggle=<F11>
" comment/uncomment blocks of code (in vmode)
vmap _c :s/^/#/gi<Enter>
vmap _C :s/^#//gi<Enter>
" my perl includes pod
let perl_include_pod = 1
" syntax color complex things like @{${"foo"}}
let perl_extended_vars = 1
" Tidy selected lines (or entire file) with _t:
nnoremap <silent> _t :%!perltidy -q<Enter>
vnoremap <silent> _t :!perltidy -q<Enter>
" Deparse obfuscated code
nnoremap <silent> _d :.!perl -MO=Deparse 2>/dev/null<cr>
vnoremap <silent> _d :!perl -MO=Deparse 2>/dev/null<cr>

" XML
" for zope3 xml config file *.zcml
autocmd BufRead,BufNewFile *.zcml :set ft=xml
http://www.bigsmoke.us/readline/shortcuts

Written by Senthil Palanisami

November 24, 2009 at 5:15 pm

Posted in Uncategorized

Leave a Reply