I want to have the matching bracket\parenthesis highlighted when I have the cursor over the closing one.
I got the parenthesis highlighting using (show-paren-mode t) but this seems to highlight the parenthesis only when I am over the opening side; how do I fix that?
Using Gnu Emacs from the terminal.
If I can the same functionality that comes by default in vim that'd be really good because it does the above and avoids the latency (highlights right away, (setq show-paren-delay 0) seems to do nothing)
show-paren-mode does that, the difference from vim's behaivor is that it'll highlight the matching opening parenthesis if the cursor is AFTER the closing parenthesis instead of highlighting it when the cursor is at closing one.
Related
I have mapped this feature where I put any bracket and vim autocompletes the second bracket and places the cursor inside the brackets so I can fill it. Now, does anyone of you have an idea how to avoid always hitting ESC and navigating outside the bracket again after finishing the text inside? How do you handle "skipping" the closing bracket?
You can tell vim not to auto complete by ctrl - v before typing the bracket.
Or as -> or plugin dependent as suggested by romainl
In emacs, as you type, it will show you the matching parentheses, but there is a separate setting to highlight matching parentheses that the cursor is on. Is there something I can write in .vimrc to make this happen? Everything I've found completely turns off parenthesis matching, which is not quite what I want.
Thanks for any help!
EDIT: Addressing the comment about a possible duplicate, that answer explains how to turn on what I want to turn off. I want paren matching as I type, but I don't want highlighting when my cursor is on a parenthesis. Thanks!
" Briefly jump to the matching bracket when typing
set showmatch
" Turn off the default matchparen plugin (on demand):
NoMatchParen
" Alternatively (in your .vimrc, to completely disable the plugin):
let loaded_matchparen = 1
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.
How do I emulate Sublime text's auto complete behavior for curly braces {} on vim? Basically, when a parenthesis is opened, it should auto close in the same line, and when <CR> is pressed the cursor should go to the next line with a block indentation and } should fall in line with the original indention of the line containing the {. If my question is not clear, this is the default behavior of most code editors when dealing with {}.
The Automatically append closing characters page on the Vim Tips Wiki has everything from simplistic mappings to complete plugin solutions. There seem to be issues with the latest Vim 7.4 version, though.
There exist many plugins with similar features as Ingo pointed out.
lh-brackets, that I'm maintaining, has the features you describe:
{ inserts {} and moves the cursor in between (and also inserts a placeholder after the closing bracket
hitting <cr> while within a pair of curly-brackets will insert another newline in-between (and indent correctly)
I've used the Windows version of Vim in the past, but am
new to MacVim. In MacVim, when I switch to visual mode, use the $
motion (highlighting from my cursor to the end of the line), and yank,
when I paste the copied content, it includes the carriage return,
bumping everything after the paste point down to a new line.
This behavior seemed unfamiliar (not to mention annoying) to me. Is there any way to change this behavior to match the Windows version, where the newline is not included in the yank?
Is just copying the text until the end of the line inappropriate? y$ will just copy from your current cursor until the end of the line, without the newline.
There's a little-known motion that serves this need regardless of configuring Windows behaviors and can generally be useful in other contexts: g_. Quoth :help g_:
g_ To the last non-blank character of the line and
[count - 1] lines downward |inclusive|. {not in Vi}
Personally I don't tend to use this for yanking because I avoid the extra visual mode keystroke and use y$ (which doesn't copy the newline, as #zigdon suggested). Or better yet, nnoremap Y y$ so that Y works consistently with C and D.
I do however often use g_ with surround.vim where the mappings to add surrounds are often harder to remember for me than using visual selection. If you want to select until the end of the line and surround with parens, for instance, it would be ys$), which isn't bad but I tend to forget the ys mnemonic. v$S) seems natural but has the same problem as your question: it includes the newline, and that's a total mess when adding surrounds. vg_S) is exactly what you usually want.
One nice thing about doing it visually is that you can correct mid-selection: I still tend to hit v$ by muscle memory a lot, but if you see that you've overshot before acting, you can still hit g_ and fix the selection.
You may try Du. Effectively it does exactly what you want and it is more finger-friendly if you intend to use it in raw editing.
I discovered that the option that was causing the behavior I'm used to seeing is behave mswin, which I believe is on by default in GVim for Windows.