<p>Selecting a large amount of text that extends over many screens in an IDE like Eclipse is fairly easy since you can use the mouse, but what is the best way to e.g. select and delete multiscreen blocks of text or write e.g. three large methods out to another file and then delete them for testing purposes in Vim when using it via putty/ssh where you cannot use the mouse?</p>

<p>I can easily yank-to-the-end-of-line or yank-to-the-end-of-code-block but if the text extends over many screens, or has lots of blank lines in it, I feel like my hands are tied in Vim. Any solutions?</p>

<p>And a related question: is there a way to somehow select 40 lines, and then comment them all out (with "#" or "//"), as is common in most IDEs?</p>

[{'answer_id': 58585, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 3, 'selected': False, 'text': '<p>Use the visual block command v (or V for whole lines and <kbd>C</kbd>-<kbd>V</kbd> for rectangular blocks). While in visual block mode, you can use any motion commands including search; I use } frequently to skip to the next blank line. Once the block is marked, you can :w it to a file, delete, yank, or whatever. If you execute a command and the visual block goes away, re-select the same block with gv. See :help visual-change for more.</p>\n\n<p>I think there are language-specific scripts that come with vim that do things like comment out blocks of code in a way that fits your language of choice.</p>\n'}, {'answer_id': 58586, 'author': 'fijter', 'author_id': 3215, 'author_profile': 'https://Stackoverflow.com/users/3215', 'pm_score': 2, 'selected': False, 'text': '<p>Use <kbd>Shift</kbd>+<kbd>V</kbd> to go in visual mode, then you can select lines and delete / change them.</p>\n'}, {'answer_id': 58588, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 6, 'selected': True, 'text': "<p>Well, first of all, you can set <code>vim</code> to work with the mouse, which would allow you to select text just like you would in <code>Eclipse</code>.</p>\n\n<p>You can also use the Visual selection - <kbd>v</kbd>, by default.  Once selected, you can <code>yank</code>, <code>cut</code>, etc.</p>\n\n<p>As far as commenting out the block, I usually select it with <code>VISUAL</code>, then do</p>\n\n<pre><code>:'&lt;,'&gt;s/^/# /\n</code></pre>\n\n<p>Replacing the beginning of each line with a <code>#</code>. (The <code>'&lt;</code> and <code>'&gt;</code> markers are the beginning and and of the visual selection.</p>\n"}, {'answer_id': 58590, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 0, 'selected': False, 'text': "<p>v enters visual block mode, where you can select as if with shift in most common editors, later you can do anything you can normally do with normal commands (substitution :'&lt;,'>s/^/#/ to prepend with a comment, for instance) where '&lt;,'> means the selected visual block instead of all the text.</p>\n"}, {'answer_id': 58592, 'author': 'Swaroop C H', 'author_id': 4869, 'author_profile': 'https://Stackoverflow.com/users/4869', 'pm_score': 2, 'selected': False, 'text': '<p>Press <code>V</code> (uppercase V) and then press <code>40j</code> to select 40 lines and then press <code>d</code> to delete them. Or as @zigdon replied, you can comment them out.</p>\n'}, {'answer_id': 58598, 'author': 'luntain', 'author_id': 5978, 'author_profile': 'https://Stackoverflow.com/users/5978', 'pm_score': 2, 'selected': False, 'text': '<p>The visual mode is the solution for your main problem. As to commenting out sections of code, there are many plugins for that on vim.org, I am using tComment.vim at the moment. </p>\n\n<p>There is also a neat way to comment out a block without a plugin. Lets say you work in python and # is the comment character. Make a visual block selection of the column you want the hash sign to be in, and type I#ESCAPE. To enter a visual block mode press C-q on windows or C-v on linux.</p>\n'}, {'answer_id': 58610, 'author': 'Rob Wells', 'author_id': 2974, 'author_profile': 'https://Stackoverflow.com/users/2974', 'pm_score': 4, 'selected': False, 'text': '<p>Use markers.</p>\n\n<p>Go to the top of the text block you want to delete and enter</p>\n\n<pre><code>ma\n</code></pre>\n\n<p>anywhere on that line. No need for the colon.</p>\n\n<p>Then go to the end of the block and enter the following:</p>\n\n<pre><code>:\'a,.d\n</code></pre>\n\n<p>Entering <code>ma</code> has set marker <code>a</code> for the character under the cursor.</p>\n\n<p>The command you have entered after moving to the bottom of the text block says "from the line containing the character described by marker <code>a</code> (<kbd>\'</kbd><kbd>a</kbd>) to the current line (<kbd>.</kbd>) <code>d</code>elete."</p>\n\n<p>This sort of thing can be used for other things as well.</p>\n\n<pre><code>:\'a,.ya b     - yank from \'a to current line and put in buffer \'b\'\n:\'a,.ya B     - yank from \'a to current line and append to buffer \'b\'\n:\'a,.s/^/#/   - from \'a to current line, substitute \'#\' for line begin\n(i.e. comment out in Perl)\n:\'s,.s#^#//#  - from \'a to current line, substitute \'//\' for line begin\n(i.e. comment out in C++)\n</code></pre>\n\n<p><strong>N.B.</strong> <code>\'a</code> (apostrophe-a) refers to the line containing the character marked by <code>a</code>.  ``a<code>(backtick-a) refers to the character marked by</code>a`.</p>\n'}, {'answer_id': 58696, 'author': 'Jagmal', 'author_id': 4406, 'author_profile': 'https://Stackoverflow.com/users/4406', 'pm_score': 2, 'selected': False, 'text': '<p>Or you may want to give this script a try...</p>\n\n<p><a href="http://www.vim.org/scripts/script.php?script_id=23" rel="nofollow noreferrer">http://www.vim.org/scripts/script.php?script_id=23</a></p>\n'}, {'answer_id': 58727, 'author': 'Jonas Engman', 'author_id': 4164, 'author_profile': 'https://Stackoverflow.com/users/4164', 'pm_score': 4, 'selected': False, 'text': "<p>To insert comments select the beginning characters of the lines using <kbd>CTRL</kbd>-<kbd>v</kbd> (blockwise-visual, not 'v' character wise-visual or 'V' linewise-visual). Then go to insert-mode using 'I', enter your comment-character(s) on the first line (for example '#') and finally escape to normal mode using 'Esc'. Voila!</p>\n\n<p>To remove the comments use blockwise-visual to select the comments and just delete them using '<kbd>x</kbd>'.</p>\n"}, {'answer_id': 58882, 'author': 'amrox', 'author_id': 4468, 'author_profile': 'https://Stackoverflow.com/users/4468', 'pm_score': 2, 'selected': False, 'text': "<p>My block comment technique:</p>\n\n<p><kbd>Ctrl</kbd>+<kbd>V</kbd>  to start blockwise visual mode.</p>\n\n<p>Make your selection.</p>\n\n<p>With the selection still active, <kbd>Shift</kbd>+<kbd>I</kbd>.  This put you into column insert mode.</p>\n\n<p>Type you comment characters '#' or '//' or whatever.</p>\n\n<p>ESC.</p>\n"}, {'answer_id': 65510, 'author': 'JP Lodine', 'author_id': 8998, 'author_profile': 'https://Stackoverflow.com/users/8998', 'pm_score': 1, 'selected': False, 'text': '<p>My usual method for commenting out 40 lines would be to put the cursor on the first line and enter the command:</p>\n\n<p>:.,+40s/^/# /</p>\n\n<p>(For here thru 40 lines forward, substitute start-of-line with hash, space)\nSeems a bit longer than some other methods suggested, but I like to do things with the keyboard instead of the mouse.</p>\n'}, {'answer_id': 65593, 'author': 'shyam', 'author_id': 7616, 'author_profile': 'https://Stackoverflow.com/users/7616', 'pm_score': 0, 'selected': False, 'text': "<p>marks would be the simplest <strong>mb</strong> where u want to begin and <strong>me</strong> where u want to end once this is done you can do pretty much anything you want</p>\n\n<pre><code>:'b,'ed\n</code></pre>\n\n<p>deletes from marker <strong>b</strong> to marker <strong>e</strong></p>\n\n<p>commenting out 40 lines you can do in the visual mode</p>\n\n<pre><code>V40j:s/^/#/\n</code></pre>\n\n<p>will comment out 40 lines from where u start the sequence</p>\n"}, {'answer_id': 274837, 'author': 'Dergachev', 'author_id': 9621, 'author_profile': 'https://Stackoverflow.com/users/9621', 'pm_score': 1, 'selected': False, 'text': '<p>You should be aware of the normal mode command [count]<kbd>CTRL</kbd>-<kbd>D</kbd>.\nIt optionally changes the \'scroll\' option from 10 to [count], and then scrolls down that many lines. Pressing <kbd>CTRL</kbd>-<kbd>D</kbd> again will scroll down that same lines again.</p>\n\n<p>So try entering </p>\n\n<pre><code>V     "visual line selection mode\n30    "optionally set scroll value to 30\nCTRL-D  "jump down a screen, repeated as necessary\ny      " yank your selection\n</code></pre>\n\n<p>CTRL-U works the same way but scrolls up.</p>\n'}, {'answer_id': 560369, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to perform an action on a range of lines, and you know the line numbers, you can put the range on the command line.  For instance, to delete lines 20 through 200 you can do:</p>\n\n<pre><code>:20,200d\n</code></pre>\n\n<p>To move lines 20 through 200 to where line 300 is you can use:</p>\n\n<pre><code>:20,200m300\n</code></pre>\n\n<p>And so on.</p>\n'}, {'answer_id': 570665, 'author': 'Jeremy Cantrell', 'author_id': 18866, 'author_profile': 'https://Stackoverflow.com/users/18866', 'pm_score': 2, 'selected': False, 'text': '<p>For commenting out lines, I would suggest one of these plugins:</p>\n\n<p><a href="http://www.vim.org/scripts/script.php?script_id=23" rel="nofollow noreferrer">EnhancedCommentify</a></p>\n\n<p><a href="http://www.vim.org/scripts/script.php?script_id=1218" rel="nofollow noreferrer">NERD Commenter</a></p>\n\n<p>I find myself using NERD more these days, but I\'ve used EnhancedCommentify for years.</p>\n'}, {'answer_id': 3007803, 'author': 'Evgeny', 'author_id': 331701, 'author_profile': 'https://Stackoverflow.com/users/331701', 'pm_score': 1, 'selected': False, 'text': "<p>First answer is currently not quite right?\nTo comment out selection press ':' and type command\n:'&lt;,'>s/^/# /g</p>\n\n<p>('&lt;, '> - will be there automatically)</p>\n"}]
['https://Stackoverflow.com/questions/58584', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4639/']