Automatically capitalizing letters

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Automatically capitalizing letters

Halim, Salman
Hi,

I was wondering if there might be a way to automatically capitalize as I
type; I can have an iab for mapping i, of course, but I was wondering if
something could be done like this.

Imap a <is the last non-whitespace character is a ., ? or !> ? A : a

Anybody?  Thanks very much!

Salman.
Reply | Threaded
Open this post in threaded view
|

RE: Automatically capitalizing letters

Halim, Salman
Sorry to reply to my own script, but the simple answer of:

Imap .<space><space>a .  A

Occurred to me; I rejected it because it restricts the number of spaces
as well as not taking newlines into consideration.

Thank you,

Salman.

> -----Original Message-----
> From: Halim, Salman
> Sent: Wednesday, February 01, 2006 3:58 PM
> To: vim mailing list
> Subject: Automatically capitalizing letters
>
> Hi,
>
> I was wondering if there might be a way to automatically
> capitalize as I type; I can have an iab for mapping i, of
> course, but I was wondering if something could be done like this.
>
> Imap a <is the last non-whitespace character is a ., ? or !> ? A : a
>
> Anybody?  Thanks very much!
>
> Salman.
>
Reply | Threaded
Open this post in threaded view
|

Re: Automatically capitalizing letters

Tim Chase-2
In reply to this post by Halim, Salman
> I was wondering if there might be a way to automatically
> capitalize as I type; I can have an iab for mapping i, of
> course, but I was wondering if something could be done like
> this.
>
> Imap a <is the last non-whitespace character is a ., ? or !> ?
> A : a

Well, a quick 5-minute hack seems to have this one-line work for me:

inoremap <space>
<space><esc>cb<c-r>=substitute(@",'^.','\U&','')<cr><right>

It only triggers on a <space> and there might be some funky edge
cases, but it might be the sorta thing you're aiming for.
Similar mappings could be created for punctuation that you want.

-tim




Reply | Threaded
Open this post in threaded view
|

Re: Automatically capitalizing letters

Tim Chase-2
In reply to this post by Halim, Salman
>> inoremap <space>
>> <space><esc>cb<c-r>=substitute(@",'^.','\U&','')<cr><right>
>
> This seems to capitalize the first letter of many words, but
> not after the end of a sentence (I want to capitalize the
> first word of new sentences, basically).  However, the ability
> to capitalize the current word when the word is done might set
> me on the right track.

Ah...I understand what you're trying to do...sorta.

Well, changing the "b" to a "(" does what you describe, though it
has some odd side effects of dinging/vis-belling.  It seems that
it *does* eat the space (while the "cb" didn't).  If you remove
the <right> at the end, it then works as I understand you want
it.  Making the resulting one-line mapping:

inoremap <space> <space><esc>c(<c-r>=substitute(@",'^.','\U&','')<cr>

There could well be better ways to do it, but it seemed to handle
most of the cases I threw at it, including typing in the middle
of a sentence that didn't already have a capital.

Hope it helps,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: Automatically capitalizing letters

Benji Fisher
In reply to this post by Halim, Salman
On Wed, Feb 01, 2006 at 03:58:23PM -0500, Halim, Salman wrote:

> Hi,
>
> I was wondering if there might be a way to automatically capitalize as I
> type; I can have an iab for mapping i, of course, but I was wondering if
> something could be done like this.
>
> Imap a <is the last non-whitespace character is a ., ? or !> ? A : a
>
> Anybody?  Thanks very much!
>
> Salman.

     One approach (overkill, perhaps) is to map a (and every other
alphabetic character) to call a function that peeks at the text and
returns either "a" or "A".  This strategy is implemented in
word_complete.vim:

http://vim.sourceforge.net/scripts/script.php?script_id=73

HTH --Benji Fisher
Reply | Threaded
Open this post in threaded view
|

Re: Automatically capitalizing letters

Marian Csontos
In reply to this post by Halim, Salman
Hi,

> I was wondering if there might be a way to automatically capitalize as I
> type; I can have an iab for mapping i, of course, but I was wondering if
> something could be done like this.
>
> Imap a <is the last non-whitespace character is a ., ? or !> ? A : a

it could be done by kind of post-processing:

%s/\([,.!?]\_s\+\)\(\S\)/\1\u\2/ge

or if you need more control:

%s/\([,.!?]\_s\+\)\(\S\)/\=submatch(1).ChangeCase(submatch(2))/ge

Regards,

-- Marian


________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
  part000.txt - is OK
http://www.nod32.com
Reply | Threaded
Open this post in threaded view
|

RE: Automatically capitalizing letters

Halim, Salman
In reply to this post by Halim, Salman
Thanks to both you and Benji for your responses.  This works quite well,
unless I'm in the middle of a sentence that continues onto the second
line and I'm on the second line.  I'm sure that can be worked out -- I
have this:

imap <space> <space><esc>mz(gUl`za

The advantage of using imap vs. inoremap is that existing iab (spelling
corrections etc.) still work as expected.

Actually, I don't clobber the z mark in my stuff -- I use Benji's (he
strikes again) Mark() function.

Thanks again, guys,

Salman.

> From: Tim Chase [mailto:[hidden email]]
>
> >> inoremap <space>
> >> <space><esc>cb<c-r>=substitute(@",'^.','\U&','')<cr><right>
> >
> > This seems to capitalize the first letter of many words,
> but not after
> > the end of a sentence (I want to capitalize the first word of new
> > sentences, basically).  However, the ability to capitalize
> the current
> > word when the word is done might set me on the right track.
>
> Ah...I understand what you're trying to do...sorta.
>
> Well, changing the "b" to a "(" does what you describe,
> though it has some odd side effects of dinging/vis-belling.  
> It seems that it *does* eat the space (while the "cb"
> didn't).  If you remove the <right> at the end, it then works
> as I understand you want it.  Making the resulting one-line mapping:
>
> inoremap <space> <space><esc>c(<c-r>=substitute(@",'^.','\U&','')<cr>
>
> There could well be better ways to do it, but it seemed to
> handle most of the cases I threw at it, including typing in
> the middle of a sentence that didn't already have a capital.
>
> Hope it helps,
>
> -tim
Reply | Threaded
Open this post in threaded view
|

Re: Automatically capitalizing letters

Benji Fisher
On Thu, Feb 02, 2006 at 10:22:05AM -0500, Halim, Salman wrote:
>
> Actually, I don't clobber the z mark in my stuff -- I use Benji's (he
> strikes again) Mark() function.

     Thanks for the plug, but please add the URL for foo.vim:

http://www.vim.org/script.php?script_id=72

                                        --Benji Fisher