One great thing about vim is how you can search/replace with just a few keystrokes. Being able to search and replace at light speed makes you feel very wizardly. Here are some commands I've been using frequently to do so.
(Note that these are all regexes, and furthermore vim regexes, which differ slightly from other regex implementations, and I won't get into that.)
/foo(enter)
(just type this, in insert mode, and you're instantly at the next instance of "foo". Then hit "n" to go to the next instance of "foo".)
/foo\c(enter)
same as the above, but case insensitive. (I guess you could do :set ic first instead of the \c there, but I don't like doing things that leave state lying around if I don't have to)
:s/foo/bar/g(enter)
Replace all "foo"s with "bar" on this line only.
:%s/foo/bar/g(enter)
Replace all "foo"s with "bar" in the entire file.
:%s/foo/bar/gc(enter)
Replace all "foo"s with "bar in the whole file, but ask for confirmation at each one. I like this one a lot.
Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts
Wednesday, July 6, 2011
Wednesday, June 1, 2011
.vimrc: colorcolumn
80 character line? No problem!
" displays a red column at 80 characters
set colorcolumn=80
Thanks to this stack overflow post.
EDIT: you should probably surround it with an "if exists" to avoid annoyance if you port your .vimrc to another machine that has an older version of vim (colorcolumn is new in 7.3). Here's the syntax:
if exists('+colorcolumn')
set colorcolumn=80
endif
Thanks to this answer on that same stack overflow post.
" displays a red column at 80 characters
set colorcolumn=80
Thanks to this stack overflow post.
EDIT: you should probably surround it with an "if exists" to avoid annoyance if you port your .vimrc to another machine that has an older version of vim (colorcolumn is new in 7.3). Here's the syntax:
if exists('+colorcolumn')
set colorcolumn=80
endif
Thanks to this answer on that same stack overflow post.
Wednesday, May 25, 2011
tComment
Another nice thing in IDE's is auto-commenting and uncommenting text. It's surprisingly complicated when you consider that most languages have both line-style (//) and block-style (/* */) comments, and the particular characters used differ from language to language. So there's no one-liner to robustly comment things in Vim.
I just got tComment, though, and I like it. I've learned a couple things:
- installing vim plugins is often as easy as downloading a .vba file, opening it in vim, and typing ":so %".
- you can't remap the slash key. I'd like it to be the same as Eclipse, so ctrl-/ (or command-/ on mac) would toggle comments. But I don't think that can be done. (do correct me if I'm wrong.)
I just got tComment, though, and I like it. I've learned a couple things:
- installing vim plugins is often as easy as downloading a .vba file, opening it in vim, and typing ":so %".
- you can't remap the slash key. I'd like it to be the same as Eclipse, so ctrl-/ (or command-/ on mac) would toggle comments. But I don't think that can be done. (do correct me if I'm wrong.)
.vimrc: switching tabs in MacVim
I'm not yet all-command-line all-the-time. (ACLATT?) Sometimes I use, say, browsers. So I've been liking MacVim.
I also like how alt-tab (or command-tab) switches between windows, while control-tab switches between tabs in my browser window. When I found out that MacVim windows can have multiple tabs too, I wanted control-tab to work there too. Hence, the newest (MacVim-specific) addition to my .vimrc:
" In MacVim, you can have multiple tabs open. This mapping makes
" ctrl-tab switch between them, like browser tabs.
" I don't think it matters whether I use noremap or map, unless
" :tabnext gets bound to something else, which would be weird.
noremap <c-tab> :tabnext<cr>
I also like how alt-tab (or command-tab) switches between windows, while control-tab switches between tabs in my browser window. When I found out that MacVim windows can have multiple tabs too, I wanted control-tab to work there too. Hence, the newest (MacVim-specific) addition to my .vimrc:
" In MacVim, you can have multiple tabs open. This mapping makes
" ctrl-tab switch between them, like browser tabs.
" I don't think it matters whether I use noremap or map, unless
" :tabnext gets bound to something else, which would be weird.
noremap <c-tab> :tabnext<cr>
Tuesday, April 19, 2011
Building a .vimrc: tabs
Just nuked my hard drive and am explicitly pulling data back from my Time Machine as I need it. I could grab my old .vimrc, but I think I'll build a new one instead. (the old one wasn't very big.)
First things first: tabs.
" whenever I hit the tab key, it now inserts spaces instead.
set expandtab
" whenever I hit << or >>, it now indents this many spaces instead.
set shiftwidth=2
" tabstop is how many spaces a tab character looks like.
set tabstop=2
Note that if you set expandtab, and if you never open a file that already contains tab characters, you wouldn't need tabstop.
More info: http://tedlogan.com/techblog3.html
First things first: tabs.
" whenever I hit the tab key, it now inserts spaces instead.
set expandtab
" whenever I hit << or >>, it now indents this many spaces instead.
set shiftwidth=2
" tabstop is how many spaces a tab character looks like.
set tabstop=2
Note that if you set expandtab, and if you never open a file that already contains tab characters, you wouldn't need tabstop.
More info: http://tedlogan.com/techblog3.html
Saturday, July 31, 2010
In which I chronicle my descent into vim
Remember when I said, two posts ago, that IDE's were the only sane way to write code? Well, maybe. But I'm starting to think it may be a good idea to learn vim or emacs, like really learn them, because:
- sometimes you just need to edit text that's not code. Maybe more often than code.
- IDE's are still great for autocomplete and stuff, especially in Java. But the higher-level the language, the less good they get. Like python or javascript. When it becomes a cute little syntax checker instead of a big auto-complete auto-import magic-everything Blendtec mixer ("will it refactor? that is the question."), then the overhead starts to kill it. Speaking of which:
- the overhead in IDE's can be killer. Especially on a big project. Especially especially over NX or VNC or Remote Desktop or something.
- I can imagine vim/emacs as a web app (and therefore as the future). I can't imagine Eclipse in the cloud yet.
- sometimes you just need to edit text that's not code. Maybe more often than code.
- IDE's are still great for autocomplete and stuff, especially in Java. But the higher-level the language, the less good they get. Like python or javascript. When it becomes a cute little syntax checker instead of a big auto-complete auto-import magic-everything Blendtec mixer ("will it refactor? that is the question."), then the overhead starts to kill it. Speaking of which:
- the overhead in IDE's can be killer. Especially on a big project. Especially especially over NX or VNC or Remote Desktop or something.
- I can imagine vim/emacs as a web app (and therefore as the future). I can't imagine Eclipse in the cloud yet.
- learning keyboard shortcuts for anything makes you instantly more badass
And given that, I'm going for vim, because I like its style: super minimal, instead of trying to do everything. (if I want it to do that, I'll use an IDE, where ctrl-c is copy and ctrl-s is save.) The dreaded modes are not really that daunting: you're just always in command mode, and sometimes you can press i to blip into insert mode, but only for a minute and then you escape back out into command mode. Text reading is more common than inserting text.
So far, I think my favorite quirk is "." (do whatever you just did, again).
Subscribe to:
Posts (Atom)