While uncommenting the order doesn't matter. NOTE : You need to go from the top to bottom while commenting on the block of code, otherwise, it would be a mismatch in commenting for specific language syntax. C / C++ / Java / Javascript / CSS / C# / Rust / Go / PHP / Swift / Dart / Kotlin Multiline Comments in various Programming Languages: 1. The following screencast is of a shell script(BASH) snippet. Similarly for the next screencast is of an Javascript snippet in a file which is getting commented using a mapping with the keys ,m and uncommented using m, again you can put any other keybinding you like. The below screencast is an example of HTML snippet in a file that is getting commented using mapping with the keys ,m you can put any other keybinding you like. "normal dd" endfunction vnoremap, m : call Comment () vnoremap m, : call UnComment () Isn't that cool? Well, you just need to copy-paste the below code to your Vimrc file and source it and you are good to go.įunction ! Comment () let ext = tolower (expand ( '%:e' )) if ext = 'py' let cmt1 = "'''" let cmt2 = "'''" elseif ext = 'cpp' || ext = 'java' || ext = 'css' || ext = 'js' || ext = 'c' || ext = 'cs' || ext = 'rs' || ext = 'go' let cmt1 = '/*' let cmt2 = '*/' elseif ext = 'sh' let cmt1 = ":'" let cmt2 = "'" elseif ext = 'html' let cmt1 = "" elseif ext = 'hs' let cmt1 = "" elseif ext = "rb" let cmt1 = "=begin" let cmt2 = "=end" endifĮxe line ( "'" ). We can simply add those characters to the start of the block and at the end of the block.īut in Vim, we can customize that too, just imagine when you just select the chunk/block of code that you need to comment out and then simply press a few keystrokes (just 2) and the multiline comments are automatically (programmatically) added as per the programming language extension of the file. Well, Of course, you can do that, it's easier for reading the code if syntax highlighting is accurate and greys out the commented part. Now you might say, why use multiple single-line comments when we can use multiline comments in almost all programming languages. Using Multiline Comments for Programming languages We are using the Visual Block mode as we only want the comment to be selected and not the entire code associated with the lines. We can simply use the CTRL + V to select the comment, and then press d to delete all the comment characters. Press CTRL + V to enter Visual Block mode It's even simpler than commenting the code. Now, as we have seen to comment out a large chunk of code, we can even uncomment the code very easily. How to uncomment multiple lines effectively This might look a bit wired on the first try but just try it every day, It is a life-saving and very satisfying experience once applied in a real-world scenario. If you are using some other language that has multiple characters for commenting like //, -, etc, you can type in any number of characters while being in insert mode after selecting the lines. So, using just simple steps you can comment out large chunks of code quite easily and effectively. Press CTRL+V and Select the line using j and kĮnter the comment code ( //, #, or other) To comment on multiple lines of code, we can use the Visual Block mode to select the lines, and then after entering into insert mode, we can comment a single line and it would be reflected on all the selected lines. How to comment multiple lines effectively We will see and use some commands, keybindings for doing so, and also we would add certain components to our vimrc file as well to design some custom key mappings. In this part of the series, I'll cover how to comment/uncomment chunks/blocks of code effectively in Vim. So you can't wait to comment on a large chunk of code manually, as it is quite a tedious thing to do. We as programmers always fiddle with commenting out code for code testing, documenting the function of code, and most importantly debugging. Comment/Uncomment Code: Vim for ProgrammersĪuthor: Meet Rajesh Gor #vim Introduction
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |