Thursday, April 28, 2011

RE: Few questions

> 3. How can I get vi to close out my (..), '..', {..}, and so on (this
>     might become annoying but I'd like to try something like this anyway).

I have a few simple mappings that are specific to Perl IF statements, but they could easily be modified to any structure:
 
" 1-2. Open Perl IF statement and reposition cursor (normal and insert modes)
noremap <silent><Leader>[ <ESC>aif () {<ESC><Left><Left>i
inoremap <silent><Leader>[ <ESC>aif () {<ESC><Left><Left>i
" 3. Close Perl IF statement and reposition cursor
inoremap <silent><Leader>] <ESC>o
" 4. Finish Perl IF statement and reposition cursor
inoremap <silent><Leader>' <ESC>o}<CR>
 
The first two maps (normal and insert modes) create the IF structure and reposition the cursor between the parens in insert mode to enter the condition(s).
The third map escapes from insert mode and opens a new line in insert mode to enter the statement(s) under the IF.
The fourth map escapes from insert mode, opens a new line with }, and issues a carriage return, which causes autoindent to align the closing brace.
 
The characters after the leader were chosen for convenience and proximity to each other. '[' to open the IF, ']' to close the IF, and ' to finish the IF.
Hope this helps.
 
Roy
 

No comments: