Auto closing curly quotation marks in Vim - vim

I'd like to set up Vim to auto close the pair of ‘ and ’ (curly quotation marks).
I've tried to configure all the five plugins I could find (two autoclose's, surround, closepairs and delimitmate) but with no success. I don't even seem to be able to remap ‘ at all (with :imap ‘ ‘’<left> or similar).
I use Vim 7.3 on Arch Linux and uim 1.7.0 input method; I insert ‘ and ’ through a shortcut defined in .XCompose. Remapping works just fine for my other compose-key shortcuts, like ¡! or ¿?.

It looks much like a vim bug, in particular, bug with internal vim escape sequences which all start with \x80 (second byte of the character in question is \x80) and encode various things like NULLs, function keys or \x80 byte itself. I guess you can dig into vim source code and find there how this byte is escaped and try to replace last byte of ‘ with this escape code or wait until this will be fixed (though I won't expect fix to come soon: here is quote from todo.txt
UTF-8: mapping a multi-byte key where the second byte is 0x80 doesn't appear
to work. (Tony Mechelynck, 2007 March 2)
So, you see problem is known for four years and is not yet fixed.)

Avoid recursion with
inoremap ' ''<left>

You can achieve this with a small function:
function! CloseQuotes()
normal! i''
startinsert
endfunction
and then map it to ' in the following way:
inoremap ' <ESC>:call CloseQuotes()<CR>
The important thing is the exclamation mark in normal!, which prevents the mapping being recursive.

Related

Vim normal editing in Visual line not working as expected?

So this is the first time i am experimenting around visual line editing in vim. I have a couple of words as following
cat
dog
and i want to surround them with brackets (without using surround.vim) for learning. The commands that i use after visual selection is as follows
:'<,'>norm 0i(<esc>$a)
and also
:'<,'>norm 0i(<C-v><esc>$a)
The problem is that vim seems to print out as well and the output i get is
(<esc>$a)cat
(<esc>$a)dog
Any hint to where i might be going wrong will be greatly appreciated.
Also worth noting that i am running vim using vim -u NONE so vimrc file doesn't get loaded.
The :normal command does not parse special characters. As you may have guessed, <esc> is inserted literally as five different characters.
You will need to wrap it in :execute with an escape on those special characters:
:exe "'<,'>norm 0i(\<esc>$a)"
Alternative ways of doing this (using the full buffer range %):
:exe "%norm I(\eA)" - an alternative way of writing the esc character
:%norm I(^[A) - where ^[ is ctrl-v and then esc, to insert a literal esc character which doesn’t need to be parsed.
:%s/.*/(&)

extra space added when going into insert mode in an abbreviation

I'm trying to create an abbreviation which replace typed text by what I want but also move the cursor and enters insert mode.
the ab is as following:
:abbreviate MSG `MSG(("")); <Esc>F"i
everything works fine except when entering insert mode, I have to extra spaces before the cursor.
I've tried then to add <BS><BS> but it's leading to delete the first quote. Same thing if I'm putting only one <BS> (which is really strange, it seems the second <BS> has no effect at all...)
I guess I'm missing something but I can't figure out what...
Thanks for your help !
A citation from Vim help system (:help abbreviations):
An abbreviation is only recognized when you type a non-keyword
character. This can also be the <Esc> that ends insert mode or the
<CR> that ends a command. The non-keyword character which ends the
abbreviation is inserted after the expanded abbreviation. An
exception to this is the character <C-]>, which is used to expand an
abbreviation without inserting any extra characters.
Example:
:ab hh hello
"hh<Space>" is expanded to "hello<Space>"
"hh<C-]>" is expanded to "hello"
So if you press <Space> after entering MSG a space is inserted after expanding your abbreviation.
To avoid adding a needless space you can invoke the abbreviation with pressing <C-]> after entering MSG or you can try elaborate a mapping like this:
:inoremap MSG `MSG(("")); <C-O>F"
But IMHO such a mapping is very inconvenient.
Another option may be to use use one of the many abbreviation plugins like this (first shown by Google).

Vim normal! sending literal <Space> string instead of whitespace character

I'm not sure what I'm doing wrong here.
:normal! I###<Space><Esc>
The end result I want when I execute this command over a word is this:
### word
But instead I get this:
###<Space>word
Vim's :normal is a nightmare with anything moderately complicated. I don't remember why, but this is the easiest way to get what you want. You need to escape the opening < in a string passed to execute.
execute "normal! I###\<Space>\<Esc>"
Edit: Vim doesn't really explain why, but :help normal
An alternative is to use |:execute|, which uses an
expression as argument. This allows the use of
printable characters to represent special characters.
Lets first explain the vim command:
:normal! run the rest of the commands as normal mode
I enter insert mode from the beginning of the line <shift> + i
###<Space><Esc> - type out the characters ###<Space><Esc> literally since we are in insert mode (notice that if you just enter insert mode, and type ###<space><esc>, you'll get just that literally.)
When this command finishes, you'll notice how it's still in normal mode. This means that your command can just omit the <space><esc> and enter the space literally:
:norm! I###
(There is an extra space after the last hash)

vim command for adding Parentheses efficiently

what is the vim command can put something into Parentheses efficiently and then I can use . to repeat it? ?
for example, data['max'] to (data['max'])
visually select your text:
viW
change it with the opening parenthesis, followed by the selected text, followed by the closing parenthesis:
c(<C-r><C-o>")<Esc>
If you are confident with text-objects, this can be done in one step:
ciW(<C-r><C-o>")<Esc>
which can be repeated with ..
I'd recommend using two plugins for this. Tim Pope's vim-surround and vim-repeat.
Just follow the links and install those plugins.
Inorder to put parenthesis around a word, just do ysiw)
Otherwise, select text in visual mode, press S(Capital S) and type in paranthesis
You can use a map for that.
:nmap \. I(<ESC>A)
You can put this line in your ~/.vimrc file.
When you press \ and . in normal mode, it will add a opening bracket at the start of the line and closing bracket at the end of the line.
Assuming that you are using a language like Tcl and surround the hash element with a bracket,
You can try this by keeping the cursor anywhere on the hash name,
: nmap \. bi(<Esc>f]li)
This will surround the expected one with circular braces.
You can use a map in vimrc:
xnoremap <leader>a <ESC>`>a)<ESC>`<i(<ESC>
And use <leader> and a to add parentheses efficiently

vim not accepting abbreviation insertion before text

i have this problem: i have this in my _vimrc:
iab dd \mathrm{d}
and it works fine, except when I am editing a line and there is text on the right of the cursor (space or no space in between). Is there anything I can do to fix this? I couldn't find anything in the help files (or here!)..
thanks!
s.franky
This is not directly related to your question but regarding easy text completion, if you don't know it already, you should have a look at the SnipMate plugin.
It inserts snippets of code depending on the context by a simple press of <Tab> key.
There is already a Tex snippet file that you can easily expand with your own abbreviations.
Your solution works fine, except that I want this to work only after I press space (this is what is meant by non-word character, right? So, I am typing a non-word character, only it doesn't work in all cases)
I don't get it. Why does it matter that other text follows, if I still press space?
thanks though, this is much better than the previous one not working at all!
#chris morgan: oddly, it's working for me too, sometimes, in an empty document. If I load up a tex document (latex-suite package), it's impossible. But I m not sure it's latex-suite specific. Sometimes this happens for .txt documents, too..
abbreviations like this will trigger when you enter a non-word character. Maybe what you want is rather an inoremap?
From the documentation:
An abbreviation is only recognized
when you type a non-keyword character.
This can also be the <Esc> that ends
insert mode or the <CR> that ends a
command. The non-keyword character
which ends the abbreviation is
inserted after the expanded
abbreviation. An exception to this is
the character <C-]>, which is used to
expand an abbreviation without
inserting any extra characters.

Resources