vim command-line snippets possible? - vim

Is there any way to define and use snippets on the vim command-line? For example, when I want to select a portion of text I want to replace a string of text within, excluding the rest of the line, I have to change:
:'<,'>
to
:%s\%VsPat/rPat/g
which once in awhile might be alright, but lately I find myself performing this kind of s/S/R/ often enough to make it a PITA; yet still more efficient than making each change in the selection manually.
Actually there are quite a few regular editing command I use in VIM that would increase my efficiency if I could tie them to a snippet somehow. So is there any way to use snippets at vim command-line?

What about just going on with your substitution without changing anything?
:'<,'>s/\%VsPat/rPat/g
Or using a visual mode mapping?
xnoremap <key> :s/\%V/g<left><left>

There is build-in feature cnoreabbrev it is simple replacement, so if you define it for example
:cnoreabbrev ss %s\%VsPat/rPat/g
then when you type :ss and press <space>, it will automatically replace ss with following
:%s\%VsPat/rPat/g
Check it out, it should solve at least half of your problems :-)

Related

Effective way to put ; at the end of line

Sorry for a noob question, but i find it struggling to just put a ";" at the end of line after writing a function. For example, I am coding in C and many time i need to write things like:
f(a);
what i usually type is (from normal mode, using bracket autopair-like feature):
if(a<ESC><SHIFT-a>;
and it need changing mode twice! Comparing to normal editor (sublime):
f(a<right>;
does anyone have more efficient way do do those typing? thanks for any help.
I think you have some "auto-close" plugin installed.
I have that kind of plugin too, and I don't press arrow keys either, since I don't have them on my keyboard. I have this:
" moving cursor out of (right of ) autoClosed brackets
inoremap <c-l> <esc>%%a
So with your example: it would be (assume already in INSERT mode)
f(a<ctrl-l>;
Thus, your fingers never leave the home row.
If you're a vim user, you can hit Shift-a.
Shift-a takes you from normal mode to insert mode, and starts your cursor at the end of the line.
(If you want to be an efficient vim user, you should remap esc to something like caps-lock.)
Comparing to normal editor (sublime):
f(a<right>;
Well… that's exactly how you would do it in Vim if you use Delimitmate or some other "autoclosing" plugin. Why do you insist on making things more complicated than they are?

vim supertab, toward a best way to insert a tab

I really like the supertab plugin. Reaching <C-x><C-p> is a bit too long and mapping it to tab gives me a better workflow. However, sometime I just want to insert a regular tab and for this I need to insert a space first, i.e. foo<Space><Tab>bar.
This pollute my sources with useless spaces before each tab char (this assuming I don't use expandtab).
I am wondering is there is a smarter way to use auto-completion ?
Going back to <C-x><C-p> which is much slower
Using another key mapping like
Using supertab with a wiser feature:
i. Automatically insert a <Tab> if I press a char that doesn't match any of the possible completions.
ii. Inserting a real tab with
???
Any ideas?
You can suppress the Supertab trigger and insert a literal tab by pressing <C-v><Tab>. (On Windows, that frequently is <C-q><Tab>.)
Supertab also provides a mapping for direct input, by default <C-Tab>; see :help supertab-mappingtabliteral. That default key combination probably only works in GVIM, though.
Just another try.
You can have a global substitute after editing work like:
:%s/ \t /\t/g

how to add vim keymap

While programming I am regulary using the following two lines:
sprintf(buff,"%s", __func__);
putrsUART(buff);
Is it possible to set any keyboard shortcut to insert these two lines?
E.g. when I type \sp in command mode, these functions get added at the cursor position in my file. Is this possible? And if so, how do I map my keys?
Thanks in Advance.
You can use abbreviations, which are designed for this.
:abbr spb sprintf(buff,"%s", __func__);
:abbr uart putrsUART(buff);
Use :help abbr for the gory details. Note that you need to type another character after the abbreviated form for vim to recognize them. This comes naturally for these as you will type ENTER as the next character. It is also possible to enter more than one line with abbreviations. Simply use <CR> where you want a new line.
Here's an easy mapping for normal mode that lets you hit \sp (unless you've remapped leader, in which case use that instead of \) in order to insert the sprintf statement.
map <Leader>sp isprintf(buff,"%s", __func__);<Esc>
That being said I think abbreviations are the way to go here
As already mentioned, abbreviations (which I would limit to insert mode (:iabbr), because you probably won't need them in the command-line) are best for simple expansions; you can also define them only for certain filetypes only (via :iabbr <buffer> ...).
Your __func__ looks like a template parameter that you need to adapt each time. You cannot do this via abbreviations, but there are various plugins (many inspired from functionality in the TextMate editor) that offer template insertion with parameter expansion and several advanced features. Check out one of snipMate, xptemplate, or UltiSnips.
Try snip-Mate for inserting regularly used codesnippets. http://www.vim.org/scripts/script.php?script_id=2540
Wrong answer, Sorry:
Try this in your vimrc:
map <c-w> :sprintf(buff,"%s",func)<cr>
This means mapping to Ctrl-W.

VIM latex-suite inserts macro code instead of executing macros

I have a problem that's been bugging me for quite a while and that I can't find the solution to.
I want to use the feature where I can press <C-j> and the cursor moves to the next placeholder.
This works for regular files, but when I edit .tex files (i.e. latex-suite is enabled), this inserts:
\right=IMAP_Jumpfunc('', 0)
instead of actually jumping (which I presume is done by the above mapping somehow).
There's no problem with regular mappings (that I've made myself like so: map rhs lhs), but it doesn't work for any latex-suite macros. Other example: if I insert figure (via menu), it just inserts the following inside the text:
\right=Tex_DoEnvironment(``figure'')
Sorry I can't solve this problem myself, which is probably trivial for an experienced user. But I have no-one around to ask.
Looks like you forgot the <c-r>= before the call to the function.
EDIT: I think I understand. Whem IMAPS is installed, it quickly parasites all our mappings. You will have to use IMAP() to define you own mappings. I had to do it in my bracketing-system in order to be robust to IMAP/LaTeX-suite presence.
Gah, I found the error!
I had defined a key mapping like so:
:imap <C-r> \right
(for adding to brackets in latex). This was then called by the pre-defined mappings ...
What a quagmire
Lesson taken: always comment-out entire or parts of the settings files, and then see if things start working.

How to create short snippets in Vim?

I have recently started using Vim as my text editor and am currently working on my own customizations.
I suppose keyboard mappings can do pretty much anything, but for the time being I'm using them as a sort of snippets facility almost exclusively.
So, for example, if I type def{TAB} (:imap def{TAB} def ():<ESC>3ha), it expands to:
def |(): # '|' represents the caret
This works as expected, but I find it annoying when Vim waits for a full command while I'm typing a word containing "def" and am not interested in expanding it.
Is there a way to avoid this or use this function more effectively to this end?
Is any other Vim feature better suited for this?
After taking a quick look at SnippetsEmu, it looks like it's the best option and much easier to customize than I first thought.
To continue with the previous example:
:Snippet def <{}>():
Once defined, you can expand your snippet by typing def{TAB}.
Snipmate - like texmate :)
http://www.vim.org/scripts/script.php?script_id=2540
video:
http://vimeo.com/3535418
snippet def
""" ${1:docstring} """
def ${2:name}:
return ${3:value}
As another suggestion (although slightly different) using vim's built in functionality:
:iabbrev def def(): #<LEFT><LEFT><LEFT><LEFT><LEFT>
Now whenever you type def followed by a space or other non-word character, it will expand to the same as what you've given as the output of SnippetsEmu (the space comes from the space you entered to trigger the completion).
This approach doesn't suffer the "lag" issue you encountered using :inoremap, and is built-into vim. For more information on this feature, look at :help abbrev.
You may be concerned that being triggered by space not tab it will trigger unnecessarily, but in general vim is pretty smart about when to trigger it. The issue can be additionally mitigated by enabling the abbreviation only for certain file-types (eg, python):
au filetype python :iabbrev ... etc
Snip[ets] (Manager|Emu|Mate|.vim) is of course also a perfect solution, but it's nice to be aware of the alternatives (especially when they are built in).
If SnippetsEmu is too heavy or ambitious for what you need (it was for me), I wrote a plugin that manages snippets based on filetype. It even has tab completion when picking the snippet! :)
Get it here: snippets.vim
I just installed UltiSnips. There’s a good article that explains why you might choose UltiSnips: Why UltiSnips?
I haven’t used any of the other snippet plugins; I decided to take the plunge with one that seemed full-featured and would be able to accommodate me as I gain more Vim skills and want to do more sophisticated things.
SnippetsEmu is a useful snippets plugin.
As noted by MDCore, SnippetsEmu is a popular Vim script that does just that and more. If you need only expanding (without moving back the caret), you can use the standard :ab[breviate] command.
:ab[breviate] [<expr>] {lhs} {rhs}
add abbreviation for {lhs} to {rhs}. If {lhs} already
existed it is replaced with the new {rhs}. {rhs} may
contain spaces.
See |:map-<expr>| for the optional <expr> argument.

Resources