|
Home > Archive > Unix Programming > February 2005 > Vi command to replace all but last 4 characters
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Vi command to replace all but last 4 characters
|
|
| Deepesh Garg 2005-02-24, 5:58 pm |
| Hi,
I was trying to form a command in Vi(vim) to replace all but the last
4 characters of a word with a given character. Couldn't do it.
Help anyone please?
-Deepesh
| |
| Rich Teer 2005-02-24, 5:58 pm |
| On Thu, 24 Feb 2005, Deepesh Garg wrote:
> Hi,
> I was trying to form a command in Vi(vim) to replace all but the last
> 4 characters of a word with a given character. Couldn't do it.
> Help anyone please?
Are the words variable length or the same length?
--
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
| |
| Preben 'Peppe' Guldberg 2005-02-24, 5:58 pm |
| [I find this topic better suited for comp.editors, Followup-To: set]
Deepesh Garg wrote:
> I was trying to form a command in Vi(vim) to replace all but the last
> 4 characters of a word with a given character. Couldn't do it.
Which characters do you think of as word characters? And is it of a
specific word, or all words.
I'll go with a-z in a vi solution for all long words:
:%s/[A-Za-z]*\([A-Za-z]\{4,4\}\)/\1/g
Vim has a lot of special character classes (see ":help /\k" and
surrounding text) that can be used. It's syntax can also be more
compact:
:%s/\w*\(\w\{4}\)/\1/g
Here \w is different and short for [0-9A-Za-z].
Peppe
--
se nocp cpo=BceFsx!$ hid bs=2 ls=2 hls ic " P. Guldberg /bin/vi@wielders.org
se scs ai isf-== fdo-=block cino=t0,:0 hi=100 ru so=4 noea lz|if has('unix')
se sh=/bin/sh|en|syn on|filetype plugin indent on|ono S V/\n^-- $\\|\%$/<CR>
cno <C-A> <C-B>|au FileType vim,mail se sw=4 sts=4 et|let&tw=72+6*(&ft=~'v')
|
|
|
|
|