This morning I woke to find that a number of blogs out there there have picked up an article called "100 Vim commands every programmer should know" over at CatsWhoCode.com. Of course, I had to go over there and scan down the list to see if there was something there that I might need to know.
Search
Commands under the 'Search' category are simple enough: straightforward word boundaries and regular expressions mixed in.
Replace
The 'Replace' category is much the same. Ranges are covered extensively. Unfortunately, I think it got a bit too general. The 'global' command was mixed in here, and only glossed over with the following paltry commands:
:g/string/d Delete all lines containing “string”
:v/string/d Delete all lines containing which didn’t contain “string”
Interestingly I'd never heard of the ':v' command, but now I know why...its equivalent to the ':g!' command.
An oldy but goody was included in the 'Replace' category as well, to with XML tags:
:%s#<[^>]\+>##g Delete HTML tags but keeps text
At the bottom of this section I saw "Change text to Rot13." Rot13?? Ah. To be honest I haven't in my paltry 14 years on the internet ever found myself using Rot13 for anything, so I have to say I'm a little surprise this makes it into the "Top 100". Onward.
Case
Amazing! I have a big black hole in my head when it comes to case commands. Oh no, nevermind. Funny how it is that you sometimes actually have to use the command sequences before you realize, oh...yeah, I know that. Duh. To deal with words I prefer the 'inner word' type commands. Rather than doing 'vE' to select a word as is done a couple times here, I would do 'viw'. I think you would find 'viw' more forgiving than 'vE' if you were whipping up a recording, say, as it'll actually select the whole word. 'vE' only selects the current position to the end of the word. Minor detail I suppose. I didn't know about this setting though. Sweet:
:set smartcase Ignore case in searches excepted if an uppercase letter is used
There were also a number of case replacements using sequences like:
:%s/\<./\u&/g Sets first letter of each word to uppercase
I'd tend to categorize this as 'Replace', but thats just me. Also, I don't often use the these generic sub replace commands in favor of the generic sub replace method. I'd do this more generic replace:
:%s/\<./\=toupper(submatch(0))/g
Hmm, yes. I should learn to use those other matches. Mines a bit verbose.
Okay, so I've only reviewed about 1/3'rd of the entire list. On the whole I think its a good list, though the categorization is a bit loose. There were a few good comments on the article (and the author acknowledged them as well; they should get a pat on the back for listening to the readers). Worst offense: leaving out recording macros. Definitely should have been in the top 100.
Tuesday, July 01, 2008
Wednesday, June 27, 2007
dip and vip it.
Thats right, paragraph manipulation is as easy as three keys. I tend to just use 'dip' and 'vip', but there have gotta be other good ones out there...
yip? yep.
grip? Rip? darn! No, that doesn't work. Changes you back into insert mode.
cip? okay, yes.
g?ip? Yes, kinda silly though.
>ip? yes.
'ip' is just inner paragraph. You could do various kinds of inner blocks and quotes.
yip? yep.
grip? Rip? darn! No, that doesn't work. Changes you back into insert mode.
cip? okay, yes.
g?ip? Yes, kinda silly though.
>ip? yes.
'ip' is just inner paragraph. You could do various kinds of inner blocks and quotes.
Wednesday, May 23, 2007
Convenient tab commands
I was conversing with a friend about how to turn a split into a new tab, and vice versa this morning. You can call IMing 'conversing', right? Yes. At this day and age, I'd hope so. Anyway. We came up with a couple convenient commands/abbreviations. Take your pick:
" Close a split and then open it in a new tab:
:ca tb let a=bufnr("%") q tabnew exe "b ". a
:command! Tb :let a=bufnr("%") | q | tabnew | exe "b ". a
" Close the current tab and open as a split in another existing tab:
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q | tabprev
:ca ts :let a=bufnr("%") tabprev sp exe "b ". a tabnext q tabprev
Note these obviously aren't that smart. If you have no splits, its not gonna act quite right. I've made the order open X, then close Y so that vim still has *something* open. Hopefully that means your Vim session won't disappear out from underneath you.
Yep, I just tested it out. Vim doesn't close, and the 'tb' command works fine regardless of the # of windows open. The 'ts' command acts a little funny when there is more than one split open in a tab. It removes one split, sticks it in another tab but doesn't completely remove the other tab b/c there's still a window in it. Not so bad. Just a little wierd.
" Close a split and then open it in a new tab:
:ca tb let a=bufnr("%")
:command! Tb :let a=bufnr("%") | q | tabnew | exe "b ". a
" Close the current tab and open as a split in another existing tab:
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q | tabprev
:ca ts :let a=bufnr("%")
Note these obviously aren't that smart. If you have no splits, its not gonna act quite right. I've made the order open X, then close Y so that vim still has *something* open. Hopefully that means your Vim session won't disappear out from underneath you.
Yep, I just tested it out. Vim doesn't close, and the 'tb' command works fine regardless of the # of windows open. The 'ts' command acts a little funny when there is more than one split open in a tab. It removes one split, sticks it in another tab but doesn't completely remove the other tab b/c there's still a window in it. Not so bad. Just a little wierd.
Monday, May 21, 2007
Recursive Diffs?
It appears that, yes, someone has already gone out and written a plugin for recursive diffing two folders. According to one person, it works relatively well. What I'm wondering is, could this plugin be used to diff two revisions in say, SVN?
Well, I had a chance to try the thing out and I have to say that I am happier doing:
:r ! diff -r --brief
and then making my own quick macro to diff any files mentioned in the diff results. I just like the ability to filter out any lines I already know aren't a factor in my diffing. The DirDiff plugin doesn't allow you to change the contents of the 'diff results'. Well, I was at first. Until I figured out all I had to do was type:
:set modifiable
Then I could delete the 'results' screen willy nilly. Much better. It still complained if I tried to insert text or what not...so its kind of a toss up. If I needed to quickly copy the contents back and forth then this DirDiff tool would be just the thing.
The trouble is most of the time I'm actually looking for a needle in the haystack. Some little change in a text file is causing a world of havoc for me. I don't want to filter out 'types' of files, and copy whole swathes of differences between the trees. In this case, I think manually reading in the diff and recording your own little quick diff macro is the way to go. DirDiff is slightly too constrained in what it wants to let you do.
In summary: a good plugin if you need to copy back and forth changes, or you don't want to spend the extra 3 minutes it takes to record your own diffing macro. I've rated this script 'Helpful'.
Well, I had a chance to try the thing out and I have to say that I am happier doing:
:r ! diff -r --brief
and then making my own quick macro to diff any files mentioned in the diff results. I just like the ability to filter out any lines I already know aren't a factor in my diffing. The DirDiff plugin doesn't allow you to change the contents of the 'diff results'. Well, I was at first. Until I figured out all I had to do was type:
:set modifiable
Then I could delete the 'results' screen willy nilly. Much better. It still complained if I tried to insert text or what not...so its kind of a toss up. If I needed to quickly copy the contents back and forth then this DirDiff tool would be just the thing.
The trouble is most of the time I'm actually looking for a needle in the haystack. Some little change in a text file is causing a world of havoc for me. I don't want to filter out 'types' of files, and copy whole swathes of differences between the trees. In this case, I think manually reading in the diff and recording your own little quick diff macro is the way to go. DirDiff is slightly too constrained in what it wants to let you do.
In summary: a good plugin if you need to copy back and forth changes, or you don't want to spend the extra 3 minutes it takes to record your own diffing macro. I've rated this script 'Helpful'.
Tuesday, May 15, 2007
Quickly delete several random lines
I just couldn't think of a quick way to take a subset of a file, where some # of lines had been randomly deleted. Since VIM doesn't have its own random() command you have to turn to some external method of determining which lines to delete. My solution was ruby:
:ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")
To repeat the command several times I couldn't think of anything better than a macro recording:
:let @l=':ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")^M'
Where '^M' is really Control-J cariage return. Then to do it...uh, 30,000 times I just typed 30000@l
:ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")
To repeat the command several times I couldn't think of anything better than a macro recording:
:let @l=':ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")^M'
Where '^M' is really Control-J cariage return. Then to do it...uh, 30,000 times I just typed 30000@l
Saturday, February 17, 2007
Indent level highlighting
I have finally begun to delve into the world of VIM syntax highlighting. The syntax used by VIM that give all my different files such pretty colors have always been something of a mystery to me. Recently a problem caused me to make the jump into the :help syntax pages.
There was on my part some assumption that someone had already done what I wanted to do, which is to highlight each successive indent level in a file. This would be particularly helpful for python files, but other programming languages as well. The BlockHL looked like just what I was looking for. Interesting, and helpful, but not quite.
Delving into the documentation I eventually found after much scratching of the head how to do exactly what I wanted to. Piddling around with the highlight colors I came up with three variations. I think I like the right most one best.
I've posted the very simple coloring script on vim, as a script plugin. See its page for further details.
*** update ***
Well well well, apparently some poeple actually read my postings sometimes and call me out on my shoddy programming skillz. Yes, its true. This plugin only works with TABs. Maybe if I get less lazy, I'll make it work with the tabstop/shiftwidth settings for you non tab users.
There was on my part some assumption that someone had already done what I wanted to do, which is to highlight each successive indent level in a file. This would be particularly helpful for python files, but other programming languages as well. The BlockHL looked like just what I was looking for. Interesting, and helpful, but not quite.
Delving into the documentation I eventually found after much scratching of the head how to do exactly what I wanted to. Piddling around with the highlight colors I came up with three variations. I think I like the right most one best.
![]() | ![]() |
| BlockHL syntax plugin | Progressively darker indenting, kinda loud. |
![]() | ![]() |
| Alternate coloring. Better. | Alternate color, with progressively darker shading. Best. |
I've posted the very simple coloring script on vim, as a script plugin. See its page for further details.
*** update ***
Well well well, apparently some poeple actually read my postings sometimes and call me out on my shoddy programming skillz. Yes, its true. This plugin only works with TABs. Maybe if I get less lazy, I'll make it work with the tabstop/shiftwidth settings for you non tab users.
Tuesday, February 06, 2007
switching between bash and batch scripts.
Were it only as easy as s/sh/tch/! Well, it almost is.
For the most part only a few search replaces are required. Most of the tme I need to convert a dos batch to a bash script. The first round of conversion includes:
:set ft=unix
:%s/%\(\w\+\)%/$\1/g
:%s/set //ig
:%s/rem/#/ig
:%s/;/:/g
:%s/\\/\//g
There is always additional work to do, but these replaces do most of the tedious portion for me.
For the most part only a few search replaces are required. Most of the tme I need to convert a dos batch to a bash script. The first round of conversion includes:
:set ft=unix
:%s/%\(\w\+\)%/$\1/g
:%s/set //ig
:%s/rem/#/ig
:%s/;/:/g
:%s/\\/\//g
There is always additional work to do, but these replaces do most of the tedious portion for me.
Subscribe to:
Posts (Atom)



