20

Sometimes I want to insert a # to comment out a line and test it quickly. Currently I do:

i#ESC:w

Is there something shorter I can do?

James Haigh
  • 1,192
  • 1
  • 12
  • 25
alejandro
  • 1,234
  • 1
  • 9
  • 19
  • 1
    possible duplicate of [What's a quick way to comment/uncomment lines in Vim?](http://stackoverflow.com/questions/1676632/whats-a-quick-way-to-comment-uncomment-lines-in-vim) – ben Jan 17 '14 at 23:25
  • 2
    There are many comment scripts. I use commentary.vim: https://github.com/tpope/vim-commentary – Peter Rincker Jan 17 '14 at 23:40
  • 2
    As others have said, you might want to try a plugin for comments. More generally, though, if you have a simple action that you need to do often or repetitively, you can easily create a mapping or macro. – Pandu Jan 18 '14 at 01:56
  • using neovim 0.5 in 2021, i make little todo items in readme files and i often have `- [ ] ` in markdown, and i'd like to add a `x` to signify i've completed my item. so i use, `r` to go into replace char mode and then type `x` to fill in the empty char (if you will) seems to work quite well. – ipatch Aug 12 '21 at 20:25

6 Answers6

9

Although I agree with others that there are better ways to comment and uncomment code, it seems that people have gotten distracted and forgotten to actually answer the question.

This is my approach to inserting a single character:

:noremap <key> i <Esc>r

I tend to find that I need to replace, remove, or add single characters very often if I'm correcting typos, so (resp.) r, x, and whatever is chosen for <key> in the above become very handy.

Note that . is also particularly handy for this sort of task. It repeats the previous action.

Personally though, I only map this function to a valuable key when I'm doing a task where I use it frequently enough to justify occupying a prime spot on the keyboard (such as correcting typos), because really, it only saves one keystroke per use and that's only when <key> is not a combination, which of course limits availability.

Community
  • 1
  • 1
James Haigh
  • 1,192
  • 1
  • 12
  • 25
  • maybe it only saves one key stroke, but it also saves the mental overhead of switching modes, which should feel significant in vim. – Adam Tolley Jun 10 '16 at 14:35
  • This mapping [doesn't work](https://vi.stackexchange.com/questions/5176/is-there-a-way-to-insert-a-single-character-and-then-exit-insert-mode#comment9880_5177) with `.` – Sparhawk May 29 '17 at 07:28
3

I map a couple of things to my <leader> key (\ by default):

" # comment the current line
nnoremap <leader>d I#<ESC>

" block comment in visual mode
vnoremap <leader>c <ESC>'<O/*<ESC>'>o*/<ESC>V'<k

If you want to add a # to the start of a group of lines, then do this:

  1. <ctl-v>
  2. j (as many times as necessary
  3. I#
  4. <esc>
Wayne
  • 59,728
  • 15
  • 131
  • 126
2

You could use a recording. From normal mode, type:

qlml0i#<press escape>`lq

Then to comment out a line, just press @l

2

Mapping in vim is so easy that I might do something like

:nmap CC I#<Esc>:w<CR>

on the fly. If I get used to it, then I will add it to my vimrc file.

:help key-mapping
:help usr_40.txt
benjifisher
  • 5,054
  • 16
  • 18
0

Actually there is a plugin you might wanna take a look at:

http://www.vim.org/scripts/script.php?script_id=1218

It is specifically designed for that purpose.

ben
  • 5,671
  • 4
  • 27
  • 55
0

I'm particularly fond of the tComment plugin. gcc to comment a line, repeat to uncomment, multiple lines, motions, etc.

rojs
  • 649
  • 3
  • 5