Vim place comment uncomment in very beginning of line shortcut - vim

I know how to comment a line in vim by doing Shift-I-//. This ends up in something like:
//int a = 1;
Instead, I would like to put comment in the very beginning of line, like:
// int a = 1;
Currently I can do this by 0-i-//.
May I ask for a shortcut like i.e. Shift-0-// (or even shorter)?
I installed nerdcommenter, tried all the default maps and none does that?
I did:
let g:NERDDefaultAlign = 'left'
And its not bad, but I still would like to have the comments on the very beginning of the line.

Why not pick a popular comment plugin? I use nerdcommenter, and I am satisfied with it.
If you want to do the comment/uncomment toggle on your own, you may want to know gI. Like:
nnoremap whatever gI//<esc>
I still recommend the plugin, because even if build your own function check if there is // on BOL, to toggle comment, it adds/removes only //. If you opened a python file, or shell script or vimscript, you cannot use this mapping any longer. The plugin checked the filetype, it is convenient. Well you can of course write all things by yourself, to reinvent the wheel.

put this in your vimrc
nnoremap <leader>/ 0i//<esc>
Now every time you press <leader>/ you will comment the line the way you want.
(If you don't have a <leader> yet, look up :h leader and carry on from there).

Related

Nerdcommenter -- only comment out selected text

In nerdcommenter for VIM, I can easy toggle a comment by doing:
[count]<leader>c<space> |NERDCommenterToggle|
Toggles the comment state of the selected line(s). If the topmost selected line is commented, all selected lines are uncommented and vice versa.
However, I find that I'll often way to only comment a visually-selected block of text, for example something like:
Is it possible to do this in nerdcommenter, or what might be a good way to do this in vim?
The problem is that :-commands can accept only line-range. That's in Vi/Vim design. A workaround is possible, but it could seem a bit complicated, as you can see from this question.
For this reason, I recently wrote a plugin vim-opera which implements such trick and also simplifies making mappings. For example, in my vimrc I have
nnoremap <expr><silent>gc opera#mapto('Comment!')
xnoremap <expr><silent>gc opera#mapto('Comment!')
nnoremap <silent>gcc :Comment!<CR>
Here :Comment is my own "commenter", but any line-range command will do.
The whole implementation takes more than 100 lines of code, so for those interested in details, I suggest to browse the source.

Show comments for specific mappings in .vimrc

I know that I can use :nmap, :vmap, :imap commands to display all the mapping for those 3 modes. However, I would like to display comments that I have for a single mapping.
So suppose I have the following entry in my vimrc:
" Execute current line in bash
nmap <F9> :exec '!'.getline('.')<CR>
This could also look like this:
nmap <F9> :exec '!'.getline('.')<CR> " Execute current line in bash
I would like to have a command that for this mapping that would look something like this:
<F9> Execute current line in bash
As this probably can only be done using custom function, where do I start? How do I parse .vimrc to strip only key mappings and their corresponding comments (I have only done some very basic Vim scripts)?
Do I understand correctly from :help map-comments that I should avoid inline comments in .vimrc?
It's very hard to "embed" comments into a mapping without affecting its functionality. And this would only help with our own mappings, not the ones from plugins.
Rather, I'd suggest to learn from (well-written) plugins, which provide :help targets for their (default) key mappings. So, to document your mapping(s), create a help file ~/.vim/doc/mymappings.txt with:
*<F9>*
<F9> Execute current line in bash
After :helptags ~/.vim/doc, you will be able to look up the comments via :h <F9>. By using the help file, you can use syntax highlighting, longer multi-line comments, and even examples, so I think that'a better and more straightforward solution.
Personally, I wouldn't have much use for such a feature for two reasons: the first one, is that I always have a vim session with a note taking buffer that contains all the tips/commands not yet in my muscle memory. Secondly, you can list all the maps using :map, and it's always a great exercise for your brain in learning vim to literally read vim commands.
That being said…
As this probably can only be done using custom function, where do I start?
…well I guess you could imagine using a function with a prototype such as:
AddMap(op, key, cmd, comment)
used like:
call AddMap("nmap", "<F9>", ":exec '!'.getline('.')<CR>", "Execute current line in shell")
which implementation would add key and comment to an array, so that you could list the array to get your own mappings declared. Using a function like ListMap().
let map_list = []
function! AddMap(op, key, cmd, comment)
" add to map list
insert(map_list, [op, key, comment])
" execute map assign
exec op.' '.key.' '.cmd
endfunction
function! ListMap()
for it in map_list
echo it[0] . '\t' . it[1] . '\t' . it[2]
endfor
endfunction
Of course, one could elaborate on the ListMap() function implementation to generate a more "proper" help file that you could open in a buffer. But that's an idea of how you could do an implementation for a feature close to what you're looking for.
N.B.: this snippet is mostly an idea of how I'd tackle this problem, and it's a one shot write that I did not actually try.
How do I parse .vimrc to strip only key mappings and their corresponding comments (I have only done some very basic Vim scripts)?
I don't think that would be a good solution, I think the better way to achieve your goal is my suggestion. Simpler and more explicit, though it won't show off all mappings. But you don't have all your mappings in your .vimrc.
Do I understand correctly from :help map-comments that I should avoid inline comments in .vimrc?
To be more precise you shall not use inline comments after maps, because they will be interpreted part of the mapping.
how to get mapping created by plugins?
Besides, using :map, you just don't. You look at the sources/documenation of your plugins and find out about them. There's no such thing as a reverse lookup from runtime to declaration point.

VIM keybinding to jump back to initial position after indenting the whole file

I have created a keybinding that should indent a whole file.
My first solution looked like this:
map <F4> gg=G
The problem is that after pressing F4, the cursor jumped to the first line of the file. So I have tried to improve my solution with the feature of markers, look like this:
map <F4> mzgg=G'z<CR>
I expected this would have resolved my problem, but the command do the same as the first. When I try to jump to the z marker manually vim told me "marker not set".
After changing the keybinding, I have or course restarted vim! I am using the GVIM 7.3 on a WIN 7 machine.
Thank you in advance for your Help!
Edit:
After trying to get my keybinding working by tipping it directly to vim commandline. I find out that the keybinding was working quite nice. I think problem is that I create a session some times ago (with mksession) and if you load a session I think vim ignores the vimrc-file. Is this assumption right?
Solution:
In these thread I find a soultion to make mksession save less options.
Another lightweight approach: set the ` mark, format the buffer and jump back to the mark afterwards.
:nnoremap <key> m`gg=G``
I would recommend the use of CTRLo and CTRLi which allow to go respectively backward and forward in the jump list. See :help jumps.
The following mapping
map <F4> gg=G2<C-o>
works. (It jumps back two times in the jump list)
But generally, the jump list is a great way to navigate in a file, this is likely the shortcuts that use the most in my daily use. It is also works if you jump to a tag to go back to your original location.
You might also want to use nnoremap rather than map, this way it will only work in normal mode, and you could potentially reuse F4 in combination in another key binding without having recursive mappings.
so
nnoremap <F4> gg=G2<C-o>

vim: Check if a line is wrapped in a comment (/* */) and toggle it on and off

Just asked a question a few moments ago about how to wrap a line in a comment which yielded this fantastic snippet:
nnoremap - mzI/* <esc>A */<esc>`z
I wanted to open another thread to ask how I can turn this into a toggle. Meaning it first checks if the line is wrapped in /* */ and removes the comment or adds it if it is not there.
Does this have to be a script or can I do this with a map? Also I don't want to use a plugin for this because its simple and I would like to see how its done.
Here you go:
nnoremap <expr> - getline('.') =~ '^\s*/\*.\+\*/$' ? '^3x$daw' : "I/* \<esc>A */\<esc>"
The un-commenting is done by ^3x$daw which deletes the beginning and ending portions of the mapping. The detection is done via a regex on the current line, getline('.'). I have removed the z mark from the comment portion of the mapping as it does a poor job of keeping cursor in the "same" spot.
This is a great example of a fancier mapping. However there are somethings to think about:
This mapping pollutes the . and " registers.
Only works on filetypes with c-style comments
No visual or operator like mappings, meaning you can not mass toggle.
Does not repeat via .
I highly recommend a comment plugin. I currently use Tim Pope's vim-commentary plugin.
For more help see:
:h :map-<expr>
:h getline(
RE I don't want to use a plugin for this because its simple.
It's only simple until you use this heavily; Peter Rincker's answer already lists some issues. As you probably rely on (un-)commenting a lot, this is really a good indication for a robust, proven plugin.
Let me throw in a recommendation for The NERD Commenter; it has the toggle mapping you're asking for, and supports several languages. Apart from commentary, also have a look at tComment.

Vim: NERD_tree Plugin. Need help understanding a bloggers .vimrc addition to streamline this plugin

So I'm basically a beginner when it comes to Vim, nonetheless I do know the basic things (open files, edit, move around, basic grep, .vimrc, etc)
I would submit this link first
http://weblog.jamisbuck.org/2008/11/17/vim-follow-up
If you scroll down to where it says "NERD___tree", it explains what it is and gives a link to the home page. I have already gotten NERD_tree installed, so far so good.
Only thing is, this guy (JamisBuck) adds a line to the .vimrc file to streamline it's usage (I'm guessing to toggle between NERD_tree and the actual file, because as far as I can tell, there is no quick way to do it other than typing in:
:NERDTree
Every time which is less than desirable. The follwing is the code he adds to the .vimrc file:
map <leader>d :execute 'NERDTreeToggle ' . getcwd()<CR>
He doesn't explain exactly what is is and/or how to use it, so If someone could give me a short explanation and/or point me towards a resource to learn more about this, that would be appreciated.
I'd say :help leader will give you what you need, is an anti-slash by default.
Thus, map <leader>d will be launched when you do \d.
According to the vim documentation, the
<Leader>
Is a special variable that is replaced with the value of "mapleader" at the time the mapping is defined. So:
map <leader>d :execute 'NERDTreeToggle ' . getcwd()<CR>
Is mapping the mapleader and "d" to the toggle. If you look at the page you linked, earlier in the page he says:
I’ve got my <Leader> character (:h mapleader) mapped to the comma
(since it’s easier to reach than the backspace character).
let mapleader = ","
So the toggle should be ",d" as far as I can tell.
In addition to what others have said (d mapped to the command), the command, itself:
:execute 'NERDTreeToggle ' . getcwd()<CR>
Is simply executing the NERDTreeToggle command with the first argument as the current working directory. The at the end is a carriage return, and is just simulating a press of the enter key.
This means that when NERD tree opens, it will be in the current working directory.

Resources