40 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 ...
romainl's user avatar
  • 36.9k
37 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)...
Kaio's user avatar
  • 503
33 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 ...
200_success's user avatar
  • 9,189
28 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 ...
nobe4's user avatar
  • 15.6k
28 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 ...
xaizek's user avatar
  • 1,708
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 ...
Dumitru's user avatar
  • 730
24 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]} – ...
Martin Tournoij's user avatar
23 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 ...
Hovercouch's user avatar
22 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 ...
dessert's user avatar
  • 345
22 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.
VanLaser's user avatar
  • 9,530
21 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 ...
saginaw's user avatar
  • 6,626
19 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 ...
B Layer's user avatar
  • 19.1k
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, ...
Kent's user avatar
  • 1,260
18 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 -...
user9433424's user avatar
  • 6,068
17 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 ...
SergioAraujo's user avatar
  • 1,087
16 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 ...
EvergreenTree's user avatar
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, ...
Ingo Karkat's user avatar
  • 17.4k
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 ...
statox's user avatar
  • 48.4k
15 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}
marderh's user avatar
  • 253
15 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 ...
Ralf's user avatar
  • 8,957
14 votes
Accepted

Which text object defines text enclosed in space?

I believe you are looking for the WORD text object. From :h WORD: A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a ...
Tumbler41's user avatar
  • 7,646
14 votes
Accepted

Passing visual range to a :command as its argument

See this answer on stackoverflow. The example given is: function! PrintGivenRange() range echo "firstline ".a:firstline." lastline ".a:lastline " Do some more things endfunction ...
statox's user avatar
  • 48.4k
13 votes

How do I restore visual mode selection highlighting?

I had this issue as well because I had switched to the "evening" color scheme in my ~/.vimrc file so that my comments were easier to read on a dark background: :colorscheme evening To restore visual ...
Integrator's user avatar
13 votes

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

Vimcasts has a great series on Fugitive.vim. The episode Fugitive.vim - working with the git index would be the the most helpful for your question. You can use :Gdiff to stage only portions of a file....
Peter Rincker's user avatar
13 votes
Accepted

Key binding to select the current paragraph

See: :h ip :h ap You are looking for the built in vip. Also, :h text-objects is a good start to find the text objects you are looking for. Edit To address your comment: to move the cursor to one ...
statox's user avatar
  • 48.4k
13 votes
Accepted

How to visually select based on indentation?

I've used vim-indent-object for a while with some success. It provides text objects like ii, ai, iI, and aI, each with slightly different semantics. For your sample code def some_method if @foo ...
D. Ben Knoble's user avatar
  • 23.9k
12 votes
Accepted

Select text till and including pattern

Just append /e to your search pattern. v/pattern/e
cee's user avatar
  • 301
12 votes

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

Stage Complete or Partial Hunks with vim-gitgutter Stage Complete Hunk <Leader>hs or :GitGutterStageHunk Stage Part of an Additions-only Hunk (since 8/2019) Visual mode {Visual}<Leader&...
Hotschke's user avatar
  • 4,500
12 votes

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

Since you mentioned visual mode... You can also use the \%V match to only match inside the current visual selection (or last one, if not in visual mode.) Using \%V would allow you to use the same ...
filbranden's user avatar
  • 27.5k
11 votes
Accepted

How to source a part of a file

After you've done your Visual selection run this: y:@"<CR> y copies the selection to the unnamed register (") since we didn't explicitly name a register. Then :@" executes the contents of that ...
dedowsdi's user avatar
  • 6,058

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