On 08/07/10 13:09, Jim Green wrote:
I used to count one by one which field I am on in order to use awk to
print this field. do we have a quick way to do this in vim?
Well, as a quick-n-dirty solution to the problem you can do
something like
" set the delimiter to a colon because
" I tested on my /etc/passwd
:let b:delim=':'
" then all on one line, create this mapping
:nnoremap <f4> :echo strlen(substitute(matchstr(getline('.'),
'^.*\%'.col('.').'c'), '[^'.(b:delim).']\+', '', 'g'))+1<cr>
(you don't mention what your delimiter is, so I made it a
variable). The "+1" makes it one-based instead of zero-based, so
season-to-taste by removing if you want 0-based column indexes.
The synopisis: extract the portion of the current line before
the cursor (matchstr), replace everything in it that isn't your
delimiter (substitute), and then count the number of delimiters
that happened (strlen) in that resulting string. If there were
0, you're in the 1st column; 1 delim, you were in the 2nd column,
etc.
Caveat, certain character-class characters may have special
meaning, so things like using "]" as a delimiter may break. As a
prize, you get to keep all the parts if it does break. :)
There may be a much nicer solution to this, but as mentioned,
this was a quick-n-dirty solution. It also requires a manual
keystroke as opposed to having something auto-update in a ruler
or title-bar.
Hope this helps,
-tim
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php