Skip to main content
50 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
  • 633
31 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,808
27 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
26 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
  • 385
26 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,770
22 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 ...
SergioAraujo's user avatar
  • 1,185
20 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
  • 20k
17 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
  • 273
16 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
  • 9,287
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
  • 50.1k
15 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,756
15 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
  • 26.6k
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
  • 50.1k
14 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
  • 50.1k
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

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,790
13 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
  • 29k
12 votes
Accepted

Extend visual selection until the last character on the line (excluding the new line character)

Your first and third points are answered by g_, which moves to the last non-whitespace character on the line. So, vg_ will visual select to the end of the line, excluding any whitespace and the ...
Doorknob's user avatar
  • 15.4k
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,298
10 votes
Accepted

How to I get around "normal" exiting visual mode?

There are two things we need to do: Since you are using a mapping to execute your function, the mapping rightly has to do :<c-u> in order to reliably call the function. But calling a function ...
joeytwiddle's user avatar
  • 3,652
10 votes

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

I know this isn't an automatic solution, but you can accomplish this by using gv after the visual indent. This will go into visual mode with the previous selection selected, and your cursor at the ...
Tumbler41's user avatar
  • 7,756
10 votes
Accepted

How to sort lines by the right most characters?

If you can use the rev command, you could reverse each line, sort and reverse again, using external commands in a pipe: %!rev | sort | rev This can be done entirely in Vimscript, but it's way more ...
muru's user avatar
  • 25k
10 votes
Accepted

How to prevent Vim from clearing the system clipboard (:reg +) on exit?

Solution Install a clipboard manager. For example, I use clipmenu. I start it whenever X is started by putting this CM_OWN_CLIPBOARD=1 clipmenud & in my ~/.xinitrc. Why Yours is a common ...
Quasímodo's user avatar
  • 2,466
9 votes
Accepted

Expand visual selection to inner curly braces

It's trying to select the block that encompasses all of the selection. Since your selection is "leaking" out of the block and there's no other block containing it, it fails. Try Vja{ on this to see ...
Tumbler41's user avatar
  • 7,756
9 votes
Accepted

Get 2 lines yanked or 1 line yanked confirmation

You are looking for the 'report' option: 'report' number (default 2) global Threshold for reporting number of lines changed. When the number of changed lines is more than ...
mMontu's user avatar
  • 6,670
9 votes
Accepted

selecting a range of lines from command line

The boundaries of a visual mode selection are defined by the < and > marks. Thus, the following command will enter visual mode with lines 5 through 10 (inclusive) selected: :5mark < | ...
Rich's user avatar
  • 32.2k
9 votes
Accepted

How do I add text before and after the visual selection?

Use c and <c-r> to replace the text and "surround" it. From visual mode: c(<c-r>") This will surround the text with ( and ). Alternative: If you find yourself doing many "surrounding" ...
Peter Rincker's user avatar
9 votes
Accepted

Why can't I paste commands into vi?

set mouse= is the fix for the problem, with set t_BE= substituted in an xterm environment. The 'bug' these fixes solve was actually intended as a 'feature': creating an obstacle to pasting vi ...
user19858's user avatar
  • 171
9 votes
Accepted

Comment-out in vim with Shift-i not working

It depends on the way you entered visual mode. If you used V for line based visual selections, I will only enter insert mode for the very first line. However when you used Ctrlv, I will also take you ...
Christian Brabandt's user avatar

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