Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 5 users not shown)
Line 4: Line 4:
 
|previous=480
 
|previous=480
 
|next=482
 
|next=482
|created=June 3, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=maurice
 
|author=maurice
 
|version=6.0
 
|version=6.0
 
|rating=14/11
 
|rating=14/11
|category1=
+
|category1=Abbreviations
 
|category2=
 
|category2=
 
}}
 
}}
Line 16: Line 16:
 
I made the following (example) to have a unique abbreviation:
 
I made the following (example) to have a unique abbreviation:
   
iabbrev date^A &lt;c-r&gt;=strftime("%F")&lt;cr&gt;
+
iabbrev date^A <c-r>=strftime("%F")<CR>
   
Note that ^A is Control-A (insert with &lt;c-v&gt;&lt;c-a&gt;).
+
Note that ^A is Control-A (insert with <c-v><c-a>).
   
 
This is to avoid the completion if you really want to write 'date'.
 
This is to avoid the completion if you really want to write 'date'.
Line 24: Line 24:
 
And now this mapping:
 
And now this mapping:
   
inoremap &lt;c-b&gt; &lt;c-v&gt;&lt;c-a&gt;&lt;c-[&gt;a
+
inoremap <c-b> <c-v><c-a><c-[>a
   
Now type date&lt;c-b&gt; in insert-mode and you will get the date.
+
Now type date<c-b> in insert-mode and you will get the date.
   
 
==Comments==
 
==Comments==
 
So why not just use this?
 
So why not just use this?
   
iabbrev date^B &lt;c-r&gt;=strftime("%F")&lt;cr&gt;
+
iabbrev date^B <c-r>=strftime("%F")<CR>
   
 
Admittedly, it requires you to do something after the control-b (such as another key or escape), but you would do precisely the same thing anyway with the original procedure.
 
Admittedly, it requires you to do something after the control-b (such as another key or escape), but you would do precisely the same thing anyway with the original procedure.
   
  +
:The point of this tip seems to be to make a simple abbreviation such as "date" which does not require special entry such as <C-V><C-A> and which you can choose to expand with a shortcut key (<C-B>) or to NOT expand it as you choose. This suggestion defeats the purpose of the tip.
 
----
 
----
  +
  +
The mapping in this tip (to expand the abbreviation) does not work for me. This does:
  +
  +
<pre>
  +
imap <c-b> <c-v><c-a><c-]>
  +
</pre>
  +
  +
Note the use of "imap" rather than "inoremap". I'm not sure if this is needed or not, but it seemed to be for me.
  +
  +
<C-]> is also used rather than <c-[> (note <c-[> is a synonym for <ESC>) because <C-]> is "expand abbreviation without inserting another character" whereas the <ESC>a didn't seem to expand the abbreviation for me (although that may have been because I tried it with inoremap).
  +
  +
--[[User:Fritzophrenic|Fritzophrenic]] 22:13, 5 August 2009 (UTC)
  +
----
  +
This tip seems needlessly complicated, and I think the title is unclear. ''Shortcut'' can mean many things. Abbreviations I'd suggest are something you'd be unlikely to type in text, without resorting to control sequences, like <code>iabbr date_ <C-R>=strftime(...)<CR></code> or <code>iabbr ddate <C-R>=strftime(...)</code>. Both are faster for me to type. The abbreviation will be expanded with the next character, or when leaving insert mode. ([[User:Spiiph|Spiiph]] 22:40, 5 August 2009 (UTC))
  +
----
  +
I have not tried it, and I have only looked very superficially, but I think the original tip did intend Ctrl-[ (Esc). The user is supposed to type "date" then press Ctrl-b. The second mapping converts that ^B to ^A then exits insert mode (which should cause the abbreviation to be expanded to the date), then the mapping uses 'a' to re-enter insert mode. I haven't decided if this is clever or not, but it does overly tricky and I guess it needs some brutal fixing.
  +
  +
*[[VimTip610|610 Use abbreviations for frequently-used words]]
  +
*[[VimTip650|650 Abbreviation that prompts whether to expand it or not]]
  +
*[[VimTip912|912 Smart abbreviation]]
  +
*[[VimTip1382|1382 Auto spelling correction using abbreviations]]
  +
Above are some candidates for possible merging. Tip 912 has some comments relevant to the above. [[User:JohnBeckett|JohnBeckett]] 01:48, 6 August 2009 (UTC)
  +
----
  +
I kind of like the ''idea'' behind the tip. One of the reasons that I don't use abbreviations very often is that I feel I would often take longer to remember the particular abbreviation I used for an item than I would to just type it (or use insert-mode completion on it). If all the "mangling" needed to make the abbreviation unlikely to occur in normal text was taken care of by a common mapping rather than me needing to remember it, it might be easier to use. I think this tip is rather clever, though I admit it isn't necessarily that useful to a wide audience. I'm stumped as to why it didn't work with the <ESC>a...I'm pretty sure you're right as to the intentions there, John, that's what I thought myself. But when I tried it with gvim -N -u NONE, it only inserted ^A^[a instead of inserting ^A, expanding the abbrev, and re-entering insert mode. It was then that I found the i_CTRL-] command and decided it was better than leaving and reentering insert mode anyway.
  +
--[[User:Fritzophrenic|Fritzophrenic]] 15:43, 6 August 2009 (UTC)

Latest revision as of 05:33, 13 July 2012

Tip 481 Printable Monobook Previous Next

created 2003 · complexity basic · author maurice · version 6.0


This is just a more flexible completion.

I made the following (example) to have a unique abbreviation:

iabbrev date^A <c-r>=strftime("%F")<CR>

Note that ^A is Control-A (insert with <c-v><c-a>).

This is to avoid the completion if you really want to write 'date'.

And now this mapping:

inoremap <c-b> <c-v><c-a><c-[>a

Now type date<c-b> in insert-mode and you will get the date.

Comments[]

So why not just use this?

iabbrev date^B <c-r>=strftime("%F")<CR>

Admittedly, it requires you to do something after the control-b (such as another key or escape), but you would do precisely the same thing anyway with the original procedure.

The point of this tip seems to be to make a simple abbreviation such as "date" which does not require special entry such as <C-V><C-A> and which you can choose to expand with a shortcut key (<C-B>) or to NOT expand it as you choose. This suggestion defeats the purpose of the tip.

The mapping in this tip (to expand the abbreviation) does not work for me. This does:

imap <c-b> <c-v><c-a><c-]>

Note the use of "imap" rather than "inoremap". I'm not sure if this is needed or not, but it seemed to be for me.

<C-]> is also used rather than <c-[> (note <c-[> is a synonym for <ESC>) because <C-]> is "expand abbreviation without inserting another character" whereas the <ESC>a didn't seem to expand the abbreviation for me (although that may have been because I tried it with inoremap).

--Fritzophrenic 22:13, 5 August 2009 (UTC)


This tip seems needlessly complicated, and I think the title is unclear. Shortcut can mean many things. Abbreviations I'd suggest are something you'd be unlikely to type in text, without resorting to control sequences, like iabbr date_ <C-R>=strftime(...)<CR> or iabbr ddate <C-R>=strftime(...). Both are faster for me to type. The abbreviation will be expanded with the next character, or when leaving insert mode. (Spiiph 22:40, 5 August 2009 (UTC))


I have not tried it, and I have only looked very superficially, but I think the original tip did intend Ctrl-[ (Esc). The user is supposed to type "date" then press Ctrl-b. The second mapping converts that ^B to ^A then exits insert mode (which should cause the abbreviation to be expanded to the date), then the mapping uses 'a' to re-enter insert mode. I haven't decided if this is clever or not, but it does overly tricky and I guess it needs some brutal fixing.

Above are some candidates for possible merging. Tip 912 has some comments relevant to the above. JohnBeckett 01:48, 6 August 2009 (UTC)


I kind of like the idea behind the tip. One of the reasons that I don't use abbreviations very often is that I feel I would often take longer to remember the particular abbreviation I used for an item than I would to just type it (or use insert-mode completion on it). If all the "mangling" needed to make the abbreviation unlikely to occur in normal text was taken care of by a common mapping rather than me needing to remember it, it might be easier to use. I think this tip is rather clever, though I admit it isn't necessarily that useful to a wide audience. I'm stumped as to why it didn't work with the <ESC>a...I'm pretty sure you're right as to the intentions there, John, that's what I thought myself. But when I tried it with gvim -N -u NONE, it only inserted ^A^[a instead of inserting ^A, expanding the abbrev, and re-entering insert mode. It was then that I found the i_CTRL-] command and decided it was better than leaving and reentering insert mode anyway. --Fritzophrenic 15:43, 6 August 2009 (UTC)