79 votes
Accepted

Expand a visual selection in the other direction

You can use the o command, from :help v_o: Go to Other end of highlighted text: The current cursor position becomes the start of the highlighted text and the cursor is moved to the ...
user avatar
  • 4,492
54 votes
Accepted

How to replace only within visual selection?

:'<,'>s/\%V\ /\,0x/g %V matches inside the visual area. See :help %V.
user avatar
  • 664
37 votes
Accepted

How to comment out a set of lines which are selected in visual mode?

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 ...
user avatar
  • 1,037
37 votes
Accepted

What is the select mode and when is it relevant to use it?

The whole point of select mode is to provide a selection behavior similar to the one used in conventional editors. On the same note, there's mswin.vim which changes a bunch of options and mappings to ...
user avatar
  • 32.8k
36 votes
Accepted

How to block replace code in visual mode?

Visual-block selection: <C-v> jjl c* <Esc> :normal: :,+2norm! cE* Substitution: :,+2s/^.\{2}/* Dot formula: cE*<Esc> j. j.
user avatar
  • 32.8k
30 votes
Accepted

Fold selected lines

You can press V to go into Visual Line mode, select the lines to be folded, then zf to make a manual fold. You'll need to :set foldmethod=manual if it isn't already set. You can open the fold with ...
user avatar
  • 8,779
28 votes

Git Fugitive how to git add a visually selected chunk of code

fugitive.vim now allows for staging a selection of a hunk with visual mode. Open the git summary with :Git (or :G) Expand the file which contains the lines you want to stage with > (or = to toggle)...
user avatar
  • 413
26 votes

How to comment out a set of lines which are selected in visual 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 ...
user avatar
  • 8,779
25 votes
Accepted

How do I visually select the block of text I just pasted in Vim?

The '[ and '] marks delimit the first and last line of the previously changed or yanked text. The `[ and `] counterparts delimit the respective lines & columns. Using that, you could visually ...
user avatar
  • 10.4k
25 votes
Accepted

How to partially replace text in a selected text-block?

Typing c after selecting your block instead of x will remove the text in the selection block and will enter insert mode. Once you type something and exit insert mode you should have the same change on ...
user avatar
  • 670
25 votes

How to add space on multiple lines when pressing spacebar (without extra configuration)?

You were right about visual mode, but you should have tried blockwise visual mode by pressing <C-V>. In blockwise visual mode you can select the lines and columns of your selection. When you ...
user avatar
  • 15.2k
25 votes
Accepted

Git Fugitive how to git add a visually selected chunk of code

There might be other ways, but this approach lets you to do more than adding a chunk, which is why I tend to use it quite a lot. Run :Gdiff command. It will open a split with version of current file ...
user avatar
  • 1,628
22 votes

How to block replace code in visual mode?

Here is more efficient way of doing this: Having cursor on the first item and 1st letter (0), enter 'blockwise' visual mode Ctrl+v and select the 1st column of your current paragraph by } (if your ...
user avatar
  • 16.6k
22 votes
Accepted

Visual select from cursor to next empty line?

Vim understands the concept of a "paragraph". Vim's definition of a paragraph is a block of text surrounded by blank lines*. There are several motions and text objects to work with this. [count]} – ...
user avatar
21 votes

Quickly calculate the total of a column of numbers

If you don't want to use plugins or drop to a bash script, you can do something like the following: c-V {motions} "ay copy column into "a :let @a = substitute(@a, 'c-V c-J', '+', 'g') replace the ...
user avatar
21 votes
Accepted

How do you indent > < without leaving visual mode and losing your current selection?

I have this in my vimrc: "keep visual mode after indent vnoremap > >gv vnoremap < <gv Note that you could also simply use . (dot) to repeat the last indent action.
user avatar
  • 9,310
20 votes

How to replace only within visual selection?

There’s \%V for that, see :h %V: %V Match inside the Visual area. When Visual mode has already been stopped match in the area that gv would reselect. This is a /zero-width match. To make sure the ...
user avatar
  • 325
19 votes

How to comment out a set of lines which are selected in visual 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 :norm 0i# does the job. (For each line, in normal mode, go to ...
user avatar
  • 2,527
18 votes
Accepted

Quickly calculate the total of a column of numbers

I wrote a plugin: https://github.com/sk1418/HowMuch which supports visual selection and does math calculations. By default the plugin supports three math-expression evaluation engines: Gnu bc, ...
user avatar
  • 1,240
18 votes
Accepted

How can I source a visual selection of vimscript?

On the command line hit <C-u> then type @* and enter. <C-u> will delete the visual range '<,'> that has been automatically inserted if some text is visually selected. :@* will ...
user avatar
  • 6,426
17 votes
Accepted

Mapping a command in Visual Mode results in error E481: No range alllowed

I'm not sure, but I think that when you hit <leader>l to call :REPLSendLine, Vim automatically inserts the visual range '<,'>, and since your command is not defined with the attribute -...
user avatar
  • 5,888
17 votes
Accepted

How to run a substitute command on only a certain part of the line

Yes, this is a long answer. That's because I try to cover the sometimes intimidating sub-replace expression as clearly as I can. Don't be frightened...please dive in! :) This can be looked at as a ...
user avatar
  • 18.3k
15 votes

How do I visually select the block of text I just pasted in Vim?

This mapping allows you to reselect the text you just pasted: nnoremap gV `[v`] But you should have used [p and ]p instead. See :help [p.
user avatar
  • 32.8k
15 votes

How to achieve selection different length of texts on different lines?

Vim only supports rectangular blockwise selections, with the exception of the jagged right border when you press $. There are plugins (like vim multiple cursors) that can emulate multiple selections, ...
user avatar
  • 16.7k
15 votes
Accepted

Perform previous substitution for different selection with single keystroke

I think you want to do @: which replays the last ex command. See :h @: Note that this command can be called from normal mode and from visual mode. Also in this case it is not useful but you can also ...
user avatar
  • 46.3k
14 votes
Accepted

Is there a command to enter Visual Block mode?

There is no built in command to start visual block mode in vim, but you can define one yourself: command! Vb normal! <C-v> Here is a breakdown of how it works: command! Vb - This creates a ...
user avatar
14 votes

Visual select from cursor to next empty line?

A shortcut for next empty line is }. So you just might want to use SHIFT+v}
user avatar
  • 243
14 votes
Accepted

Visual Block Mode edit with sequential number

Depending on your usecase the following might be useful: Create the entries all with the number "1": - "1" - "1" - "1" - "1" Then go to the second "1" and press V to start line-wise visual. Then ...
user avatar
  • 8,585
13 votes
Accepted

Faster way to move a block of text

There's a plugin for that: dragvisuals.vim. It doesn't bind any keys by default, so you'll have to do that yourself. I like to map them to Control+Arrow key: xmap <expr> <C-Left> DVB_Drag('...
user avatar
13 votes

Quickly calculate the total of a column of numbers

:r!awk '{sum+=$6} END {print "Total: "sum}' % Explanation: :r ........... read (put result in this file) ! ............ external command awk .......... external tool {sum+=$6} .... sixth field (awk ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible