Archive for the 'vim' Category
How to quickly change several lines at once
Wednesday, January 24th, 2007It's 130.87 K, and just over four seconds, and will play under any version of QuickTime.
It shows me using vim to replace an old variable name (“fullScreenOverlayWindow”) with a new one (“panel”). The sequence of events:
- I move into position.
- I enter Visual Block mode by pressing ctrl-v.
- I jump to the end of the current word with e.
- I jump 7 lines down with 7j. This gives me a total of 8 lines selected (including the one I started on).
- I enter Change mode by pressing c.
- I type my shiny new variable name. (Notice that it only displays on the line where my cursor is; all the others stay blank for the duration of my Change.)
- I exit Change mode by pressing escape.
- I wait a beat.
- Voila!
Cocoa text editors like TextEdit and Xcode have rectangular selection, but not rectangular editing; you will obliterate all the lines, but only insert your new text into the first line. This is not useful.
An insight into vi
Thursday, November 16th, 2006Here are three of vi's many commands:
- $
- Move to the last column in the line
- %
- Move to the opposite character in a pair (e.g. {→})
- ^
- Move to the first non-whitespace column in the line
On a US keyboard, these three characters are contiguous: shift-4, -5, and -6, respectively.
This means that you can quickly navigate code like this with only your ring, middle, and index fingers on one hand:
⁰if(foo) {¹ doTheThingToTheThing(); ²} else {³ doTheThingToTheOtherThing(); ⁴}
- From 4 to 0: %^%^
- From 0 to 4: $%$%
Neat, huh?
A vim quickie
Saturday, July 22nd, 2006:g/^[-+].\+{\s*$/,/^}/fold ←Folds all Obj-C methods in the current file.
Technorati tags: vim
Making vim grok Obj-C
Monday, April 24th, 2006Like many, I use the popular open-source text editor vim. Earlier, I set about making it more usable through syntax coloring. This included tweaking its support for Objective-C.
First, mkdir ~/.vim, and cd ~/.vim. Now mkdir syntax. cp /usr/share/vim/vim62/filetype.vim . and cp /usr/share/vim/vim62/syntax/objc.vim syntax/. Then select and copy this patch:
Index: filetype.vim
===================================================================
--- filetype.vim 2005-03-21 02:12:47.000000000 -0800
+++ filetype.vim 2006-04-24 03:33:27.000000000 -0700
@@ -262,7 +262,7 @@
" .h files can be C or C++, set c_syntax_for_h if you want C
au BufNewFile,BufRead *.h
- \ if exists("c_syntax_for_h") | setf c | else | setf cpp | endif
+ \ if exists("c_syntax_for_h") | setf c | else | if exists("objc_syntax_for_h") | setf objc | else | setf cpp | endif | endif
" TLH files are C++ headers generated by Visual C++'s #import from typelibs
au BufNewFile,BufRead *.tlh setf cpp
Index: syntax/objc.vim
===================================================================
--- syntax/objc.vim 2005-03-21 02:12:49.000000000 -0800
+++ syntax/objc.vim 2006-04-24 03:19:25.000000000 -0700
@@ -43,6 +43,8 @@
syn match objcDirective "@class\|@end\|@defs"
syn match objcDirective "@encode\|@protocol\|@selector"
+syn keyword objcStorageClass in out inout byref bycopy
+
" Match the ObjC method types
"
" NOTE: here I match only the indicators, this looks
@@ -73,6 +75,7 @@
HiLink objcFactMethod Function
HiLink objcStatement Statement
HiLink objcDirective Statement
+ HiLink objcStorageClass StorageClass
delcommand HiLink
endif
Now type pbpaste | patch -p0.
OK, now you have modified copies of the file type detector and the Obj-C syntax coloring. Now you need a .vimrc. If you already have one, add this to it; otherwise, create it with this:
let c_gnu=1 let c_no_bracket_error=1 let objc_syntax_for_h=1 syntax enable
This makes the following changes:
- c_gnu
- Enables detection of some GCC extensions to C.
- c_no_bracket_error
- Allows
{}inside of[]. This lets you do compound literals inside messages; for example:[self setFrame:(NSRect){ NSZeroPoint, (NSSize){ 128.0f, 128.0f } }]; - objc_syntax_for_h
- Tells vim (with my modifications, that you did above) that a .h file is an Obj-C header, not a C++ header.
- syntax enable
- Enables syntax-highlighting.
Technorati tags: vim, Objective-C.