vim DelimitMate - vim

With the DelimitMate, it auto generates the closing parentheses. When I'm finished typing inside the parenthesis, what key strokes do I press to quickly go to the right of the closing parenthesis? (Right now I have to manually press ESC then 'a')

The idea of these auto-closing plugins (like the original feature implementation found in IDEs like Eclipse) is that you just type the closing character to go over it. The plugin should detect this situation and instead of inserting the character jumps over the existing, auto-inserted one.
If that's not working for you, there are several plugin alternatives on offer. The Vim Tips Wiki has a list of them.

With delimitMate, Shift-tab will jump out of the current delimiter and Control-G g will get you out of nested delimiters. No need to remap anything.

You could try auto-pairs's Fly Mode
eg:
( hello| world )
press ) at |
( hello world )|
If jump incorrect, use <M-b> to do the back insert.
eg:
(hello| world()
press ) at |
(hello world()|
press <M-b>
(hello)| world()
Repository: https://github.com/jiangmiao/auto-pairs
Plugin: http://www.vim.org/scripts/script.php?script_id=3599
add
let g:AutoPairsFlyMode=1
to .vimrc to turn Fly Mode on

You can do a custom map. I guess you want to go to the right of the closing parenthesis while you're in insert mode. Just add to your .vimrc this mapping:
:inoremap <F8> <ESC>f)a
In this way, while your in insert mode and you've finished to write inside the parenthesis, F8 will bring your cursor ad the right of the closing parenthesis.
If you want you can change the mapped key, using another key instead of F8.
As Kent said in the comment a more general solution would be:
:inoremap <F8> <ESC>%%a
Which will work for [ and { brackets.

I kinda agree with Atropo on this one: if you want to stick with DelimitMate then the least disruption to your workflow might be to make a custom imap to get to the other side of the auto inserted character.
Personally I prefer to have more control over where/when the characters are auto-inserted, and how I can navigate around the auto-inserted characters; UltiSnips or SnipMate does that for me. Maybe they're more what you're looking for.

If you typing on a new line, you try A which will append text at the end of the line.

I have autoClose installed. what I am doing currently is ("I" is cursor)
- (xxxxI)
- ( xxxxI )
- ( xxxxIxx )
- text (xxxxI) other text
- text ( xxxxI ) other text
- text ( xxxxIxx ) other text
I just make a mapping, to <esc>%%a then in above case, the cursor will move to (...)I..whatever
it doesn't work for quotes.

A little late to the party, but note that it can be done easily without any custom mappings. In insert mode, you can press <C-O> (a default mapping) to enter a "one shot" normal mode where you can enter a single normal mode command.
So to answer your question, what you could do is <C-O>a.

Related

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

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?

How to efficiently add parentheses or a string in vim?

In traditional text editors, whenever I needed to open a string or parentheses and type something between it I used to do:
Type () or ""
Press left
Type in what I need
Press right
But in vim (that is if I followed the vim way) the process becomes quite tedious as I have to enter the normal mode to move a whole bunch of times:
Type () or ""
Press <ESC>
Press i
Type what I need
Press <ESC>
Press l
Press a
If it is not a good practice to use the arrow keys at any time, is there a more efficient way of doing this kind of task in vim?
It is actually quite easy to automatically append those closing characters in a mapping, and put your cursor where you want it. The trick is to do that, without also messing up the undo/redo/repeat actions. The problem is that cursor movement commands in insert mode will break the "undo sequence" so that any change you make after moving the cursor is undone separately from changes made before moving the cursor.
Warning: the following information may become dated
There are plenty of plugins available to automatically append these characters (see the partial list at the Vim wiki page for appending closing characters), and prior to Vim 7.4, some of them even had complicated workarounds for keeping the undo sequence intact. Unfortunately, they all relied on a bug in Vim that got fixed in version 7.4 for this.
A patch is available to add a cursor movement that does not break undo, so if you want to compile Vim yourself, you can grab that patch and use mappings like the following (no plugin required!) to do what you want:
inoremap ( ()<C-G>U<Left>
inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<C-G>U\<Right>" : ")"
These mappings will insert "()" when you type an opening (, placing the cursor in between the parentheses. When you type ')' and there is already a closing ')' after the cursor, Vim will skip over the parenthesis instead of inserting a new one. Cursor movement is preceded by <C-G>U which is the feature the aforementioned patch adds, allowing the following cursor movement to not break the undo sequence (as long as the movement is all in a single line).
As of Vim 7.4.663, this patch has still not been officially included.
No. Doing it in Vim is exactly the same as in your "traditional" editor:
Type () or ""
Press left
Type in what you need
Press right
But… why don't you type the opening character, what you want inside the pair and then the closing character?
Type ( or "
Type what you need
Type ) or "
Too simple?
I think using arrow keys to move around is bad practice in normal mode but in your case; moving one space while in insert mode, I would hazard to say using the arrow keys is probably best practice.
That being said if you are dead set on avoiding them you could use <i_ctrl-o>.
:help i_ctrl_o
CTRL-O execute one command, return to Insert mode *i_CTRL-O*
So, while in insert mode, you could type: ()<ctrl-o>h<xxx><ctrl-o>l, where <xxx> is whatever you want in the brackets.
Unfortunately that doesn't work if you cursor is on the last character of the line, which if you are typing it most likely is.
To solve that problem do :set virtualedit+=onemore or add it to your ~/.vimrc file.
Note that this solution is more keystrokes than simply using the arrow keys but you don't need to move your hands away from the home row so it may be faster anyway.

PhpStorm and IdeaVim - shortcut to complete line?

I'm using the current PHP Storm EAP (138.1505) and IdeaVim 0.35.
Suppose I just want to type:
print_r($foo);
I press i to go into Vim insert mode.
I type p r and I can choose print_r via the autocomplete.
It'll add open and close braces () for me.
I start typing my variable, it'll let me autocomplete that too.
At this stage I have print_r($foo) with my cursor positioned before the closing bracket.
What I'd like now is a single key combination that'll finish the line for me - i.e. add a semi-colon and a carriage return. At the moment I either have to type );[return] by hand as if the autocomplete wasn't there, or for some reason I've got in the habit of doing esc to leave insert mode then A to continue at the end of the line and ;[return][esc]to finish it off, which is even longer.
Any better way?
I don't use PHP Storm. but I do use Intellij + ideavim.
Here I can press Ctrl-Shift-Enter to complete a line. It is a kind of default setting. You can find it under menu: Edit: complete current statement
You may want to give it a try.
You can add this line to ~/.ideavimrc:
imap <C-e> <Esc>A;<CR>
Replace <C-e> with your desired key.

Using alt+backspace key in vim command line to delete by words

Is there a way to use the alt+backspace in vim command line? It gets unruly when having to backspace /very/long/file/path individually instead of using alt+backspace to delete by words.
try using instead <c-w> (that is ctrl+w) to erase words or <c-u> (ctrl+u) to delete lines.
http://vim.wikia.com/wiki/Map_Ctrl-Backspace_to_delete_previous_word
:imap <C-BS> <C-W>
sets ctrl backspace, i have to look at how to do alt
If you are at the end of the path you can hit B followed by a dW (case matters). This will jump you to the beginning of the word (ignoring the slashes) and subsequently delete the word (again ignoring the slashes).
Hope this helps.
Vim is unable to receive alt input. skeept's answer seems to be the best alternative.
See this answer:
The Alt/Meta key is problematic in Vim and most terminals, see this answer of mine for an overview of the situation (the situation is the same for Meta and Alt).
In short, Vim doesn't receive Alt at all: hitting Alt+Backspace is exactly the same as hitting Backspace.
Anyway, it will be better for you in the long term to learn and get accustomed to Vim's default key-mappings.
The answer marked as right does not correspond to the behaviour in most UI editors for Alt + BackSpace. The vim shortcut which correspond to this behaviour is db - aka delete back ( a word ?! ), dw would delete word forth, which would be the (Altr or Ctrl ) Del shortcut in most ui programs.
Those work basically the same way as the w - move the cursor to the words beginnings and b, move the cursor back to the words beginning ...
Disclaimer: I have used for more than 10 years my .vimrc. , which might have some freaky twist which changes the default behaviour as well ...
Sure, it's as easy as:
if has('gui_running')
imap <M-BS> <C-W>
else
imap <Esc><BS> <C-W>
endif
The trick here is to know, given a hypothetical foo key, that after pressing a Alt+foo combination, many terminals will send an Escape code followed by foo. Apparently there are exceptions — some terminals do send something that vim can recognize as Alt. But if a imap <M-BS> <C-W> mapping doesn't work for you in terminal, then most likely your terminal sends an Esc instead, so the combination imap <Esc><BS> <C-W> should work for you.
You can read more about that in vim documentation by evaluating :help map-alt-keys
x then w should backspace per word as well.
d then w will also delete the current word the cursor is on.

Resources