Showing posts with label VIM. Show all posts
Showing posts with label VIM. Show all posts

Sunday, July 06, 2008

txt2regex

txt2regex is an awesome tool that allows you to specify a regular expression in an interactive curses-based interface that is then printed out in a variety of formats, including English, vim, perl, etc. Perfect for when you know exactly what you want to match, but are not particularly knowledgeable about the idiosyncrasies of the current regex engine you're using.

Sunday, August 26, 2007

VIM split windows and macros quick reference

ex commands for split windows
  • :sp filename create a new split with filename
  • :vsp filename create a new vertical split with filename
  • :new create a new split with an empty buffer
  • :vne create a new vertical split with an empty buffer
  • :wa save all open files
  • :wqa save and quit all open files
Ctrl-W related commands for split windows
  • C-w h moves split to the left of current split
  • C-w j moves split below the current split
  • C-w k moves split above the current split
  • C-w l moves split to the right of the current split
  • C-w + increase the current split by one line (or, prefix this command with a number to do that many line increases)
  • C-w - decrease the current split by one line (same addendum as above)
  • C-w < decrease the current split width
  • C-w > increase the current split width
  • C-w _ Maximize the current split
Split windows in the .vimrc (taken from this page ... also see the same author's excellent Efficient Editing with VIM )
  • map <C-J> <C-W>j<C-W>_ " Hit C-j to move the current split down and maximize it
  • map <C-K> <C-W>k<C-W>_ " Hit C-k to move the current split up and maximize it
  • set wmh = 0 " allow splits with 0 lines open
Macros
  • q<register> start recording and save the macro in the specified register (use a lowercase letter, for example)
  • q (again) end recording
  • @<register> playback the macro

Thursday, August 02, 2007

My .vimrc file

Thanks to Evan Klitzke and others at Yelp; a lot of this is ripped from their .vimrc's. All hail vim!


set modeline " Respect other people's options (when a modeline is present)
set encoding=utf-8 " Use UTF-8 (8-bit variable width Unicode)

" indenting options
set autoindent " Keep the indent level when hitting Return
set smartindent " Use smart indenting (mostly useful for C/C++ files)
set cindent " Don't indent Python really poorly
set tabstop=4 " Make tabs appear four spaces wide (default is 8 spaces)
set shiftwidth=4
set noexpandtab " Use hard tabs please! Watch out for files with soft tabs
" that don't have a modeline present, especially Python files.
set fo=tcoqan " Options for formatting text (i.e. for use with gq)

" UI stuff
set showmatch " Show matching parens as they come up
set ruler " Show the column number in the status bar
set incsearch " Find as you type
set lz " Don't redraw the screen in the middle of executing macros
set nohlsearch " Highlighting search terms is _really_ annoying
behave xterm " Just in case...
set wrap " Display files with word wrap, but don't actually insert newlines
" (Set by default)... so lines don't go off edge
set lbr " Wrap only at word boundaries (default is at any character)
syntax enable " Who wouldn't want syntax highlighting?

" Only allow folds that are manually set
set foldmethod=manual

" Make our folds look like how they were when we exited vim last time.
set viminfo=s1,<1024,'1024,/1024,:1024,@1024,c,f1,%,n~/.viminfo
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ let b:doopenfold = 1 |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ unlet b:doopenfold |
\ exe "normal zv" |
\ endif

set nocompatible " Don't act like SysV vi
set shell=/bin/bash " Use this shell to execute commands
filetype plugin indent on " ability to run different vimrcs on different filetypes
set autoread " When a file has been detected to have been changed outside of Vim and
" it has not been changed inside of Vim, automatically read it again.
set mouse=a " Have mouse operate in all modes -- disable mouse by holding the shift key
set ttyfast " Tell vim to optimize for a fast terminal; will be on by
" default if your $TERM is xterm or screen, but could be
" turned off if you use a weird terminal (e.g. 'screen-bce').
" Set 'nottyfast' for slow SSH connections.
set history=50
set showmode " show what mode (e.g. INSERT) you're in
set showcmd " Show (partial) command in status line
set backspace=indent,eol,start " explicitly makes the backspace work when at the beginning of a line
nmap <C-N><C-N> :set invnumber <CR> " Press Ctrl-N twice to toggle line numbers in the left margin

"splits
set wmh=0 " allow splits with 0 lines open
map <C-J> <C-W>j<C-W>_ " Hit C-j to move the current split down and maximize it
map <C-K> <C-W>k<C-W>_ " Hit C-k to move the current split up and maximize it

" cycling through tabs
map <C-H> <C-PageUp>
map <C-L> <C-PageDown>
imap <C-H> <C-PageUp>
imap <C-L> <C-PageDown>

set title " sets title of terminal in menubar to that of file currently being edited
set ignorecase " perform case-insensitive searches...
set smartcase " ... unless there are capitalized characters in the search pattern
set mousehide " hide the mouse when editing
set exrc " eliminates the possibility of another .exrc getting confused with mine on multi-user systems
set bg=light "this almost always looks better -- take this out or set it to dark if things look funny

" for common typos
command! Q quit
command! W write
command! Wq wq
cabbrev Set <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'set' : 'Set')<CR>

Or, say you want soft tabs instead of hard ones (see converting tabs to spaces):
set expandtab     "soft tabs
set tabstop=2 "set tabstops
set shiftwidth=2 "set shiftwidth
set softtabstop=2 "delete this many spaces upon deleting soft tab

More stuff from Rajiv:

set directory=/var/tmp,/tmp " put all the backup stuff in tmp directories
set shortmess+=A " no swap file complaints
set nobackup " don't litter directories with swap files

" move across rows, not lines
noremap j gj
noremap k gk
noremap gk k
noremap gj j

set display=lastline " ??????????????
syntax sync fromstart " always render syntax from the beginning of the file
au! BufRead,BufNewFile * lcd %:p:h " cd into the directory of the file you're editing
set vb t_vb= " no visual bell

Friday, April 06, 2007

My .exrc file (vi/m) and then some

What follows is my .exrc file for various versions of vi, and some more related stuff. I have yet to look through the vim documentation exhasutively to see if there are any more useful options -- this is just stuff I have gleaned from random places on the Internet and from friends.

Plain vanilla vi (as on Solaris)
set nu " show line numbers
set ts=4 " tabs take up four columns
set sw=4 " set shiftwidth (>> or <<) to 4 columns set showmatch " show matches of ()'s, {}'s, and []'s
set showmode " show what mode (e.g. INSERT) you're in
set autoindent " indent to the extent of the previous line

Vim:
syntax enable " enables syntax highlighting
set nu " show line numbers
set ts=4 " tabs take up four columns
set sw=4 " set shiftwidth (>> or <<) to 4 columns
set title
" sets title of terminal in menubar to that of file currently being edited
set expandtab " tabs aren't really tabs, but many spaces
set softtabstop=4 " delete four spaces when you delete a "tab"
set ignorecase " perform case-insensitive searches...
set smartcase " ... unless there are capitalized characters in the search pattern
set mousehide " hide the mouse when editing
set exrc " eliminates the possibility of another .exrc getting confused with mine on multi-user systems
set backspace=indent,eol,start "explicitly makes the backspace work when at the beginning of a line
set bg=light "this almost always looks better -- take this out or set it to dark if things look funny
set nohlsearch "no annoying search highlighting

" for common typos
command! Q quit
command! W write
command! Wq wq

Vim Python
set autoindent " indent to the extent of the previous line

Vim C/C++/Java
set cindent " indenting for C-style languages
set showmatch " show matches of ()'s, {}'s, and []'s

For Text (Vim 7.0 only)
setlocal spell spelllang=en_us " English(US) spellcheck on (Vim 7.x only)
set tw=0 wrap linebreak " don't split words between lines, only wrap at end of word
"lots of abbreviations (varies depending on the subject material)
ab STIA Science, Technology and International Affairs

How to produce nicely formatted C code in Vim (From p.221 of Learning the vi editor):
set nocp incsearch
set cinoptions=:0,p0,t0
set cinwords=if,else,while,do,for,switch,case
set formatoptions=tcqr
set cindent
syntax on
source ~/.exrc

Options set when vim 6.4/7 starts on my system (Ubuntu Linux):
helplang=en " language to display help in
ruler " show row and column in bottom right
ttyfast " indicates a fast terminal connection
viminfo='20,"50 " configuration info
history=50 " number of : commands and previous search patters to be remembered
scroll=32 " number of lines to scroll with CRTL-U and CTRL-D commands
ttymouse=xterm2 " mouse codes
backspace=indent,eol,start "explicitly makes the backspace work when at the beginning of a line
fileencodings=ucs-bom,utf-8,latin1 " list of character encodings considered when starting to edit a file
printoptions=paper:letter " controls format and output of printing
runtimepath=~/.vim,/usr/share/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vimcurrent,/usr/share/vim/vimfiles/after,/usr/share/vim/addons/after,~/.vim/after " list of directories which will be searched for runtime files
suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc " Files with these suffixes get a lower priority when multiple files match a wildcard.

Cool stuff that I have seen various places but I didn't find particularly useful (maybe someone else will):
set list "replaces all EOLs and TABs with $ and ^I
"auto-complete of brackets
inoremap [ [] <left>
inoremap ( () <left>
inoremap { {} <left>
inoremap " "" <left>
inoremap ' '' <left>

To see the options vi(m) has enabled, enter :set
To get help with options in Vim, enter :help set or :set all for regular vi
Or just :help for the help contents... Vim has excellent documentation

Links

Saturday, August 19, 2006

VIM (and some vi)

Everybody who knows me knows that I love vim. Vim is a powerful version of the vi editor. It usually comes with any *NIX instillation. It rules. Here are some links, and some of my favorite (non-obvious) tricks. If you are new to all of this, a good place to start is the VI lovers home page or, if you are in a hurry, the quick reference card.

VIM

Plain old VI

Tricks
  • Swap two characters xp
  • Swap two lines ddp
  • Reverse the order of all lines :g/^/m0/
  • Remove all lines with only whitespace :g/^\s*$/d
  • Move all lines onto one logical line :%j
  • Remove all blank lines :g/^$/d
  • Insert a newline at the end of every line :%s/$/^m/ (keystrokes to get this one are: Ctrl+V, Ctrl+M)
  • Text completion before, after cursor: Ctrl+N, Ctrl+P (thanks to neha for this link )
  • Get a command prompt :shell
  • Execute a shell command :!<command>
  • Append the contents of a file into your vi session :r!cat <file>
  • Omni-completion Ctrl+X Ctrl+O
  • Whole line completion Ctrl+X Ctrl+L
  • Search for a word forward under the cursor *
  • Search for a word backwards under the cursor #
  • Move to a percentage of the file: type the number, then hit %