This is controlled with the formatoptions setting; from :help fo-table:
You can use the 'formatoptions' option to influence how Vim formats text.
'formatoptions' is a string that can contain any of the letters below. The
default setting is tcq. You can separate the option letters with commas for
Note that the statement ...
If you want language specific commenting you will need a plugin like nerdcommenter.
Alternatively, although it doesn't answer your actual question, you can use builtin vim actions and your knowledge of each language's comment characters...
Option #1: V-blocks
:1 Enter (Go to line 1)
Ctrl-V (V-Block mode)
jjj (Down 3 more lines)
Shift-I (Enter insert mode ...
Using Visual Block mode (CtrlV), select the beginning of the lines. Then hit I# (that's a capital letter I) to insert the hash character on each of those lines. Then press Esc to return from Insert Mode to Normal Mode.
If you just need a quick solution for whatever language you're currently in, and you already have the text selected in visual mode, then
does the job.
(For each line, in normal mode, go to the first column and insert #. Using :norm I# will insert it before the first non-whitespace character, which may not be what you want.)
Using :norm i# will ...
Here's a slightly more efficient / easier method. Type it with the cursor at the beginning of the line of text (i.e. LOAD) that you want to center.
:center 80<cr>: center the text with Vim's built in function
hhv0r#: add the #s on the left
A<space><esc>40A#<esc>: add plenty of #s on the right
d80|: delete excess #s on the right
One way would be to map the keystrokes you would use to a key combination:
nnoremap gcc I/* <ESC>A */<ESC>
In normal mode, when pressing gcc, this will go to the beginning of the line and insert /* , then goes to the end of the line and insert */.
This might be already enough for you, but if you want more I would recommend just using a plugin ...
Doing it automatically would require you to add something like the following to your vimrc file (source):
au FileType haskell,vhdl,ada let b:comment_leader = '-- '
au FileType vim let b:comment_leader = '" '
au FileType c,cpp,java let b:comment_leader = '// '
au FileType sh,make let b:comment_leader = '# '
au FileType tex let b:comment_leader = '% '
It all depends on how extensive you want it to be. What type of files are you editing? I have the following in my .vimrc that works with python files:
xnoremap # :norm 0i<C-r>=Comment<CR><CR>
xnoremap & :norm ^<C-r>=len(Comment)<CR>x<CR>
This works well, but you have to manually setup each filetype, ...
The method in my earlier answer doesn't transform well to dynamic content. This is where snippet plugins like UltiSnips and SnipMate come in. I'll provide a demo of UltiSnips here. Install it using your favourite method from How do I install a plugin in vim/vi?
Now, make an UltiSnips directory in your .vim or _vimfiles directory. In it, place a c.snippet ...
I do approve @mMontu comment suggesting to use a comment plugin (vim-commentary is an option but NERDCommenter has my preference over vim-commentary).
But you could do it in other ways:
First if all your # are aligned on the same column you can block select them with ctrl+v52j and then delete your selection with x.
If they are not aligned you could use a ...
Please don't do that: This type of title comments is just too heavy and
doesn't improve the readability of your code. If you really need to create a
section use light multi lines comments like:
" Solving a big problem
That's much easier to read. So now that I gave my totally subjective opinion,
which you don't have to agree with:
Here's a function to ...
Not directly. But: if the comment applies to the whole line, you better place it above the line. If the comment(s) are each to indicate something about a portion of the line, you could consider splitting the line into exactly those pieces, and use variables named such as that they can replace your comments. It's better (IMHO) when the code "speaks" directly. ...
This has nothing todo with the normal command or the visual block. This is due to a setting 'formatoptions', see :help 'formatoptions'. In particular, the mentioned behaviour comes from the flags r and o. If you don't like the behaviour, you can turn it off with :set fo-=ro.
See :help fo-table for the available flags.
I don't know how to achieve exactly what you want, only a part.
You want to hide some text inside ~/.vimrc (for example).
First we need to know which highlight group handles the text you want to hide.
Add this mapping to your ~/.vimrc :
map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(...
if you're willing to use a plugin, there are many commenting plugins out there. Some of the most popular examples are:
All of these have mappings for toggling comments. You can remap <C-m> to this command. For example, if you go with tpope/commentary, you could do:
nmap <C-m> ...
Alternatively, you could use the :g command. I would do:
to uncomment and
to comment. This is nice because it's more readable (and shorter) than a search and replace command. Explanation:
:g/\s*#pdb.set_trace()/ "For every line matching this regex
I like the approaches statox suggested. Here's another one:
This only works if the # is the first character on each line. Otherwise, I would do
These two work very similarly. Essentially, what <count>: is doing, is setting up a range so that the next ex command is applied to the next <count> lines. norm x means
Press 'x' ...
After you have selected the lines, simply type
: will automatically put '<,'> on your command line, which is a range from the start of your selection to the end; norm executes a normal mode command, and will act on that range; I# is the normal mode command which inserts a '#' at the start of the line.
As explained in :help 40.1 (SPECIAL CHARACTERS):
It is not possible to put a comment directly after a mapping, because the "
character is considered to be part of the mapping. You can use |", this
starts a new, empty command with a comment. Example:
:map <Space> W| " Use spacebar to move forward a word
So, you can use | in order to ...
One way would be to create a file containing this snippet, and read it when you type ///.
For example, create ~/.vim/snippets/my_header.snip containing this header. Then define this mapping:
inoremap /// <esc>:r ~/.vim/snippets/my_header.snip<cr>i
inoremap /// <esc>:call append(line('.')-1, readfile(expand('~/.vim/snippets/my_header....
This is a perfect use case for ultisnips with Python interpolation. A sketch of a snippet for the first header might look like this:
from math import floor, ceil
def left(lng, fill, s):
count = int(floor((lng - len(s) - 6) / 2.0))
if count < 0: count = 0
return fill * count
def right(lng, fill, s):
count = int(ceil((lng - len(...
You might be interested by the tabular plugin it allows to align text based on patterns.
In your case if you have
some text %comment
some more text %another comment
with your cursor on the first line you can use 3V to select the lines you want to treat and then :Tabular /% and tabular will automatically indent the text like that:
You might have to set a custom comments. For me, comments in a Python file is:
With some experimenting, I found that, for Vim to insert # after lines with ##, I needed to use a three-piece comment (or part of one, anyway):
- Three-piece comments that have a start string, an end string, and optional
lines in between. The strings for ...
There is a solution which can be pretty simple if you are willing to use a plugin: NERDCommenter.
This plugin is made for "intensely orgasmic commenting" according to their github page. More precisely it creates several mappings to handle comments and one of the mappings is <Leader>ci which allows to toggle the comments of a line.
Combined to a ...
You should tell Vim what a comment is using:
The comments option contains a comma separated list of strings that can start a comment line.
:-- specifies the comment string (-- in that case) repeats at the start of each line.
b:-- specifies a blank (<Space>, <Tab> or<EOL>) is required after --.
You can use:
vim +'normal! 2GI;' +'x' path/to/your/file
The + parameter allows to execute a command after opening the buffer.
The first command normal! 2GI; goes to line 2 and add a ; at the beginning of the line
The second command saves and exit.
Bonus point: To uncomment the same line:
vim +'normal! 2G^x' +'x' path/to/your/file
The culprit is $VIMRUNTIME/ftplugin/c.vim, and likely all other standard ft plugins. If you want everything they define but the setting for 'formatoptions', I don't see any simple solution. (Just in case, $VIMRUNTIME is set within vim)
May be, you could listen for OptionSet to prevent inserting cro in &fo. But beware of possible infinite loops. I've ...