vim 7.4 comment/uncomment lines in Vim without plugins - vim

I tried out answers in What's a quick way to comment/uncomment lines in Vim?, but they don't work. Pressing shift i on visual mode after selection just brings me in insert mode. I run it with --noplugin, no use.

You might need to take visual block before proceeding , Ctrl-v to use it. More at help blockwise-visual
Then, use any nagivation keys like h,j,k,l, followed by I, then insert your comment character like # or // and finalize by Esc.
This should comment whole block.
To uncomment, repeat the same operation of visual block and navigation but replace I by any delete operation like x for # or 2x for //.
Hope this helps

There are many ways to comment/uncomment in Vim. Anyway, assuming "C++-style" comments, eg. // foobar…
Commenting a single line
I// <Esc>
Uncommenting a single line
^dw
Commenting several lines
vjjj
:'<,'>norm! I// <CR>
or
<C-v>jjj
I// <Esc>
Uncommenting several lines
vjjj
:'<,'>norm! ^dw<CR>
or
<C-v>jjjllld

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/.*/(&)

VIM move in Insert mode

Why I ask:
I use to enter code for example if(condition){}, in following step:
if(){
}
move cursor back into () to complete condition
move cursor into {} to add task
I have read Traversing text in Insert mode, and I add follow code into my $HOME/.vimrc
" key mapping
inoremap <A-h> <C-o>h
inoremap <A-j> <C-o>j
inoremap <A-k> <C-o>k
inoremap <A-l> <C-o>l
now I can use Alt+h and Alt+l, but the rest of two new map had no effect, then I test: Ctrl+oj and Ctrl+ok, both of them work.
Is there any mistake when I do the key mapping?
How to check if my new mapping is conflicted with other or not?
UPDATE: 2nd/Nov/2016
I buy a new keyboard with cursor key...
Install auto pair
However, I found one interesting thing, when I in Linux, there is ok for all above mapping just except Alt+h, because it conflicted with the ubuntu current open window help menu. I only meet my problem when I use ssh via MobaXerm application.
I have read Traversing text in Insert mode, and I add follow code into
my $HOME/.vimrc
You should carefully read the accepted answer for that answer, specially this part:
The right way is to press Esc, go where you want to do a
small correction, fix it, go back and keep editing. It is effective
because Vim has much more movements than usual character
forward/backward/up/down. After you learn more of them, this will
happen to be more productive.
The answer where you borrowed the mappings also mentions this:
Notwithstanding what Pavel Shved said - that it is probably more
advisable to get used to Escaping Insert mode - here is an example set
of mappings for quick navigation within Insert mode: (...)
Anyway, if you want to understand the problem with the Alt+j and Alt+k, you should first ensure that the mapping is still defined in Vim (they could have been erased or overwritten). You can use :imap to list them; try these:
:imap <A-j>
:imap <A-k>
If your mappings are correctly defined each one will list its target (e.g.: * <C-O>j). In this case you should check if Vim is receiving these combinations correctly; try inserting then in the text (insert mode) by using Ctrl+V (or Ctrl+Q if you mapped that to paste from clipboard) and the Alt combinations. You can get more details at the Vim FAQ "I am not able to create a mapping for the key. What is wrong?".
Edit:
If your issue is mainly related with closing parenthesis, then there are several other options, which I believe that are more practical. I quick internet search returned the following:
SO - Automatic closing brackets for Vim
Vim wiki - Making Parenthesis And Brackets Handling Easier
plugin - Auto Pairs
I also think that you misuse Vim.
I know that the question was about something else but here is my idea of how you should move around in vim.
You have 3 steps:
1. Insert some empty loop / condition
2. Insert a condition
3. Insert a body of the loop / condition
This should represent 3 changes, each separated by leaving the insert mode.
To do it properly you can perform step 1 and then leave insert mode by using either Esc or Ctrl+[ (with the second one- which is also vim default- you do not have to reach for escape key).
Then you should navigate to the place where you want to insert your change using h,j,k or l and follow it by starting insert mode.
There are several ways to start insert mode:
I - start insert mode at the beginning of the line (omitting whitespaces at the beginning)
i - start insert mode before the cursor
a - start insert mode after the cursor
A - start insert mode at the end of the line
s - change the sign under the cursor (can be combined with visual mode)
c - change text from under the cursor until place you have specified with the movement (e.g. ce - change until the end of the word, cl - the same as "s")
C - change everything from cursor until the end of the line
S - replace the whole line
o - start insert mode in the new line below
O - start insert mode in the new line above

Vimrc settings to emulate Sublime's parenthesis auto-completion

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)

How to automatically remove newline at end of yanked linewise visual selection?

Is there any way to yank a linewise visual selection without the newline at the end of the last line of the selection? More specifically, I'd like to be able to copy a line or lines from vim to the system clipboard and paste it elsewhere without the last command being executed without a chance to edit it.
I can get the desired effect by executing this command on the register in question after yanking:
:let #*=substitute(#*,'\n$','','g')
but is there any way to execute that command automatically? I am using MacVim at the moment and there doesn't appear to be a way to map an extra command to ⌘-C, so I'd have to have another mapping to remember to execute after copying if I can't find another solution.
My original answer to myself has been working well enough that I pretty much forgot about it, but now I've decided to set clipboard=unnamed to always yank to the system clipboard. A side effect is that yanking to the clipboard also affects the unnamed register, from which I don't want the trailing newline to be removed.
The new (and more Vim-ish) answer is a mapping to convert the linewise visual selection to characterwise before yanking it:
vnoremap <C-c> <Esc>'<0v'>g_y
Now I copy with <C-c> if I intend to paste in a command line or just y any other time. Pretty much what user Explosion Pills suggested but some added stuff to include the first character of the first line and last of the last line.
I came up with a solution that works for me, inspired by one of the answers here: Run command when vim enters visual mode
There is no real equivalent to a hypothetical VisualEnter or VisualLeave, but the CursorHold event along with changing updatetime will trigger after exiting visual mode. I also added the gv map to make it work when copying the previous selection.
" Remove last newline after copying visual selection to clipboard
function! RemoveClipboardNewline()
if &updatetime==1
let #*=substitute(#*,'\n$','','g')
set updatetime=4000
endif
endfunction
function! VisualEnter()
set updatetime=1
endfunction
vnoremap <expr> <SID>VisualEnter VisualEnter()
nnoremap <script> V V<SID>VisualEnter
nnoremap <script> gv gv<SID>VisualEnter
autocmd CursorHold * call RemoveClipboardNewline()
I don't like the idea of writing a swap file every 0 milliseconds, but I tested this in both Windows and OS X, and it works and causes no noticeable performance problems. It doesn't appear to write the swap file unless there are changes.
Also, I'm a little confused as to why this works at all, since it should trigger the function and set updatetime back to 4000 while still in visual mode, but apparently the value of updatetime doesn't change until after exiting visual mode, which works out nicely here.

Issue with smartindent in Vim

In vim with smartindent on:
Press Enter after say an if-statement
Type in {
Press Enter twice
Type in }
If you hit ↑ and go to the previous line, indentation is removed from the blank line.
Even the vim documentation says that:
If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again.
Is there any way to keep this indentation and not have it deleted?
Use Shift+S to start editing on a blank line (from command mode, obviously). This will start your cursor off with the expected level of indentation.
Another doesn't-answer-the-question-as-asked-but-is-a-better-solution-overall:
When typing an opening brace in insert mode, this will insert a matching set of braces
and leave the cursor on a new line in the middle.
:imap { {<CR>}<Esc>O
Similarly, this will auto-insert matching parens and square brackets.
:imap ( ()<Left>
:imap [ []<Left>
(Strip off the leading : when adding to vimrc.)
As I commented on Victor's answer, changing Vim's indentation behavior will leave "empty" lines containing extraneous spaces throughout your files. IMO, this is completely intolerable.
When this happens to me, I sometimes use ddko (or ddO) to delete the line without enough spaces and open a new line with the correct indent. Or, I'll just press A and then Tab enough times to get to the correct indent.
the article here talks about you're very same problem, and what to put in vimrc to fix it.
inoremap <CR> <CR><Space><BS>
nnoremap o o<Space><BS>
nnoremap O O<Space><BS>
I havn't exactly tested this tho.
also the same article links to a shorter alternate solution.
My preferred method is {<CR>}<esc>shift+o as it outpaces {<CR><CR>}<esc>k shift+s by several strokes. I get in a rut with it, though, and end up just using o or O to grab new, properly-indented lines off an empty when I should be using S.
That is, set up your bracing structure and open line-above:
if (true) {
}//cursor here, press shift-o
And you get the indenting you expect.
The open-above trick isn't any fewer keypresses than <up><end><cr>, but with escape remapped and shift being chorded, you can throw it in quite fast.
Also, don't forget your manual indent reset and block-movement. If you're inside a mangled curly brace block, simply use ={ (or =i{ if you're on top of one of the braces). I use that when I have a Good Idea that needs to see text asap, and I don't worry about any formatting frippery until I take a breather.

Resources