Vim does not like brackets - vim

I have a problem that have been bothering me for days now.
Whatever I do, I simply cannot place any single {,},[ or ].
Every time I try, it takes me up to the next or previous paragraph. I've looked it up and I can't find a good way to unmap it and finally have my azerty keyboard to behave correctly so I can peacefully code

In the normal mode, press 'i' to enter the insert mode.
And you can start to edit your file.
After everything is done, press the 'ESC' key and then ':wq' to save your modification.

Vim / vi has several different modes. This dates back to the times when you would edit using a teletyper, i.e. no "visual" representation of the data you are editing, no mouse etc.
With only the "usual" keys at your disposal, you need to navigate in the data, you need to insert data, and you need to execute commands on the data. Not so coincidentially, these are the three modes of Vim / vi, and in each mode, the keys do something differently.
By default, the editor starts in the "normal mode", which you will use to navigate, and enter the other modes from.
In this mode, ) moves you to the end of the sentence, ( to the previous sentence. } and { do the same for paragraphs, combinations of [ and ] work with chapters.
If you enter "insert mode" (most simply by pressing i in "normal mode"), you can enter all the above letters normally. You exit "insert mode" by pressing <Esc>. You will recognize "insert mode" by -- INSERT -- being displayed in the bottommost line of the screen.
If you are in "insert mode", and pressing any of the above keys does move your cursor instead of entering the corresponding symbol, there is some (broken) configuration at work. Check your ~/.vimrc, and if necessary, rename it and try again with a "clean" configuration.
It is next to impossible to do Vim / vi justice in the scope of a SO answer. It is very much an expert-friendly editor, not a novice-friendly one. You need to actually learn how to use this editor, but it is absolutely worth it in the long run.

In insert mode, you should be able to insert brackets without trouble.
In normal mode, those correspond to motions.
To go to insert mode from normal mode, press i.
See: :h object-motions

Alright, Problem Solved ! I had to uninstall and reinstall the Ubuntu bash on windows 10 multiple times and it worked ! Thanks for the help everone !

Related

vi replace visual mode selection with clipboard

I know that this question has probably already been answered but I can't find it.
Here is what I want to do, imagine I have world in my clipboard.
And my file looks like :
Hello guys
I want to replace guys by world.
I know that :%s/guys/world/g would work but my section is actually huge.
I would like to select my selection with v (visual mode) then when I have the proper selection highlighted, paste my clipboard. I tried p it's not working.
Any help ?
I just did this on gvim 8.0.2. This post explains how to do it: https://www.reddit.com/r/vim/comments/337cxj/yankpaste_tofrom_system_clipboard/
From that post:
Firstly, execute the command
:echo has('clipboard')
If it returns 1 you're good to go, otherwise check if your distro has a more full-featured vim package (or compile it yourself)
To copy to the clipboard, enter either visual, visual-block , or visual-line mode, select your text and press the sequence (not combo):
"+y
and your text is ready to be pasted into any other program or window.
Additionally you can execute that sequence in normal mode, however its behavior is not ideal (in my opinion). It yanks the remaining characters on the current line, after the cursor plus the characters on the next line up until the column of the cursor. Decent if your cursor is at column 0, otherwise kind of annoying.
By replacing the 'y' with a 'p' you get the same behavior you would expect from just 'p' in normal mode, however pasted from the system clipboard. On a modern setup, this is probably not as useful, as we have Ctrl-Shift-v, but I can certainly think of some use-cases for this.
Have you tried the normal paste command of terminal ctrl+shift+v? It works for me after selecting content through v.

How to use vim autowrap correctly?

I am trying to use the vim autowrap functionality to automatically wrap my paragraph into lines no longer than 80 letters in real time as I type. This can be done by set textwidth=80 and set fo+=a. The a option of the vim formatoptions or fo basically tells vim to wrap the entire paragraph while typing.
However, there is a very annoying side-effect, that I can no longer break a line by simply pressing enter.
This is a sample sentence.
Say for the above sentence, if I want to make it into:
This is
a sample sentence.
Usually I can just move the cursor to "a" and enter insert mode and then press enter. But after set fo+=a, nothing will happen when I press enter in the insert mode at "a". One thing I do notice is that if there is no space between "is" and "a", pressing enter will insert a space. But nothing else will happen after that.
So what do I miss here? How do I stop this annoying behavior?
You can run :help fo-table to see explanations of the options:
a Automatic formatting of paragraphs. Every time text is inserted or
deleted the paragraph will be reformatted. See |auto-format|.
When the 'c' flag is present this only happens for recognized
comments.
This means that every time you insert a character, vim will try and autoformat the paragraph. This will cause it to move everything back onto the same line.
I don't think you need to add a at all. I use neovim, but the behavior here should be the same. The default values are, according to the help pages:
(default: "tcqj", Vi default: "vt")
Try removing set fo+=a entirely from your .vimrc. Keep set textwidth=80. That should fix your issue.
EDIT: Once you have set textwidth=80, if you want to format an existing paragraph, you can highlight it in visual selection and press gq.
The following allows me to use the enter key to start a new line while setting the text width to be 79 characters:
set tw=79 "width of document
set fo=cqt
set wm=0 "# margin from right window border
After some exploration, I find a workaround that can solve the problem to some extent, though not perfect.
The basic idea is that when entering a line break, disable the auto-wrapping temporarily when sending <CR> and resume auto-wrapping after that. There are multiple ways of doing that. And the best one as far as I know is using the paste mode, since you don't have to exit insert mode when entering paste mode. So just make the following commands into any key binding you like in insert mode. The one I am using right now is inoremap <C-N> <F2><CR><F2>
The reason why I think this one is not optimal is that for some reason I cannot bind <Enter> in this way, but have to use another key.
If <Enter> or <CR> can be configured in this way then the problem is 100% solved.

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 run Vim shortcuts without exiting insert mode

For instance, I'd like to use $ to move to the end of the line, without going through Esc for it.
Just $, and not Esc then $.
Is there a way?
One solution could be: press another key(/combo), say, Ctrl+Shift, do what I want in "temporary normal mode", then when I release those keys I'm back in insert mode... if that's possible.
If you wanna perform one normal mode command in insert mode you can use Ctrl+O and then normal mode command. So Ctrl+O$ will move you to the end of line in insert mode (like End or Ctrl+OShift+A).
Is there any particular reason why you want to go to the end of the line? Vi is built to mainly be operated in normal mode, not insert mode (especially when it comes to movement).
For example, if you wanted to go to the end of the line and start adding text, you would type A (or ESCA if you were in insert mode at the time).
If you wanted to go to the end of the line so you could add a newline, you could type ESCo to start a new line and place you in insert mode.
If you gave us more context as to what you are ultimately trying to accomplish, we may be able to come up with a more efficient workflow.
inoremap $ <ESC>A
give this a try. It may do what you want, but make inserting $ harder.
Ctrl + Right
map! <C-Right> <Esc>A

Return to normal mode without leaving the home row

I've been trying out vim, and the emphasis on speed and accessibility makes sense for the veteran programmer. I've previously used emacs, and currently use a combination of nano, and stuff like gedit or geany.
I'm confused by the need to constantly switch modes, and that returning from insert mode to normal mode requires leaving the home row to press Esc. I've read that previously, this key was Tab on some systems where vim was first used, which makes a lot more sense, but not on current systems where tab is expected to participate in smart indenting when coding.
Returning to normal mode is an operation that you need to perform all the time. Is there some alternative key mapping that makes this quicker, or mappings that I simply don't know about that do this?
Ctrl+C also return you to normal mode
You can also use ^[. If you've mapped your caps lock key to control (highly recommended), this becomes a rather easy keystroke.
I personally use the Capslock key as the second Esc key, so it is very comfortable. You can do it by adding the following lines to your .xmodmap file:
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
You can map whatever you like to escape:
imap kj <Esc>
I haven't yet run across a situation where I need to type kj next to each other, besides when I talk about this mapping. I've seen other people use jk or jj, but kj works best for me.
I personally use ii to get out of insert mode while staying on the home row, but it is only a matter of personal preference:
inoremap ii <Esc>
i to go in insert and ii to go out. This is easy for your finger to remember. (I tend to still use <Esc> though)
I personally use Left Control as escape and Caps Lock as Left Control. It is not vim mapping, but some xmodmap+setxkbmap magic:
! ~/.Xmodmap
! Control_R
keycode 37 = Escape
and ctrl:nocaps in X keyboard options. You may have different keycode though.
Use Alt/Meta In a Terminal
If you use Vim in a terminal, simply press alt/meta+normal_mode_key. Most terminals send an escape character followed by the normal_mode_key that you pressed, removing the need to press escape yourself.
The terminals konsole and gnome terminal send the escape by default when you press alt/meta+normal_mode_key. For Xterm you can ctrl+click and select the option "Meta sends escape" or "Alt sends escape".
Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction. You can hold down alt when moving even while in normal mode since the additional ESC that is sent does no harm.
The advantage of using this scheme is that you can use the alt/meta+key combination with any normal mode key, e.g.
Alt+o opens a new line below the one you are currently editing,
Alt+A appends to the end of the current line,
Alt+p pastes at the current insert location.
Alt+k moves up
Thus using vim via a terminal gives you these short cut powers on any stock standard system without the need to edit each systems vim mappings.

Resources