9

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). This will only show the changed hunks plus some extra lines of context above and below the changed lines. v to start visual mode and select the lines you ...


6

Command o will get you to the "other" end of the Visual selection. See :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 other end of the highlighted text. The highlighted area remains the same. When you're in Visual Block mode, there's also O ...


5

I think your best bet is to use a plugin like vim-indent-object it defines some new text objects to handle indentation levels: Other alternatives are either: Creating your own text objects manually as described here which is more or less reimplementing the plugin I mentioned before Using a library like vim-textobj-user which helps you creating new text ...


5

The reason your command doesn't work is <cr> is not automatically expanded. Instead you need to use double quotes and a backslash. execute "normal! jV/ddd\<CR>y" Actually you don't really need to bother with visual mode here; what you want can be done using 2,/ddd/y This means from second line, up till and including search for ddd, ...


4

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 into insert mode for the very first line, but after you press Esc everything that was entered will also be copied to all the other lines of the selection. ...


4

A working, hacky solution function! Visual_indent_with_space() range abort '<,'>g/./exe "normal! " v:count1 . "I " endfunction vnoremap <leader><space> :call Visual_indent_with_space()<cr> Breaking down: '<,'>g/./exe "normal! " v:count1 . "I " '<,'>: use the selected range g/./: ...


4

jump to function call 16k is not ideal, it only works if you turn on 'relativenumber' and target function is visible. by search. /func_name or ?func_name . If they share the same name, you can place cursor at function name and use * or #. by lsp reference. This works if target function is the same as current one. If you use coc.nvim, you can use this ...


4

One thought that occurs to me: yank the text (y), and then paste it at the :grep command: :grep <C-r>" ... Note that this doesn't work too well for multi-line selections, and you may want something more like <C-r>=escape(@", '/\')<CR> to handle some escape sequences. I'm making use of the "insert register" keystroke (:...


4

You can use blockwise Visual mode to select a block of columns and insert text (such as one or two spaces) in front of the block. Likewise, you can also use blockwise Visual mode to delete one or two columns of spaces in order to unindent lines. To add indentation: Go to the first line of the block. Use 0 (or ^) to move to the first column (or first ...


3

A range with commands is always about lines. It seems you want to work with line/column positions. Whether a range was given can be checked by using <range>: command! -range Test call TestFunc(<range>,<line1>,<line2>) function! TestFunc(r,l1,l2) abort if a:r == 0 echo "No range" elseif a:r == 1 echo "Single ...


3

I don't think there's much to improve your sequence. You could use <c-o> to jump back to the first function instead of 16j0. This only works with jumps though (e.g /, check :h :jump-motions), so 16j won't do. If you use coc, you can use [m and ]m to jump to the next/previous method. Also, once you've searched a parenthesis with f(, you could just ...


3

There's no history for Normal/Visual mode in Vim. The only thing I can remember of is v:operator which is set to the last Normal/Visual mode operator executed.


3

During development of vimscript code, having to source the whole file where it resides just to try out a snippet is sub-optimal. In my experience, there are always a bunch of variants of the code we're trying to make work that we don't want to throw out (yet), but we need to keep commenting it out (or in) so that the :so % doesn't trip up all over the place. ...


3

Given "b contains what you wish to append, I would select V + movement use :s on the end of line with :s-\= to insert the content of @b " '<,'> will be automatically added when hitting `:` from the visual mode :'<,'>s/$/\=@b/ As @D. Ben Knoble pointed out, if the lines that need to be completed are exactly all the lines without a ...


3

You can press o while in visual mode to switch to the opposite end of a visual selection, thus using it to switch direction of your highlight.


2

There's no single "clipboard" in Vim. These things are called registers. Make sure you read the docs about them, it's essential. This behaviour is documented under :h v_p (as you "put" while being in "visual" mode). "Visual put" consists of two actions: a) put new text from register, and b) delete the old text into a register. Here deleted text is put ...


2

In step 7 you delete the text you selected in step 6, so it is moved to the unnamed register ("clipboard"). This the normal behavior of Vi(m). If you want to copy something multiple times, you should put it into a named register, e.g. into register 'a'. In step 3 you would use "ay and in step 7 you would use "ap. The unnamed register still changes in ...


2

Is the clipboard overwritten in Visual Mode each time? Yes it is. How do I get "sane" behavior? That is make the clipboard persist after the first paste. To get "sane" behavior you can remap p and P with: " now it is possible to paste many times over selected text xnoremap <expr> p 'pgv"'.v:register.'y`>' xnoremap <expr> P 'Pgv"'.v:...


2

In general, visual block selections must be blocks (rectangles). The only "jagged" selections are when you hit <C-v>$ to select to the end of multiple lines. However, depending on what you want to do, there are often several approaches. In this case, to copy: " clear register A let @a = '' " yank all the words global/=/normal! 0"Ayaw Using a capital ...


2

As jecxgo points out, the problem is that the obvious solution of recording a macro that processes a single line and then applying that to the selection of lines with :normal fails because, when the lines are expanded, this messes with the other lines in the range. jecxgo solves this by not expanding the lines within the macro but instead doing so in a ...


2

Using yank commands from normal mode is actually a common practice. The alternative is to use getline() to get the contents of a line and then parse it, possibly using regex matches or other Vimscript string functions. But it's often easier to use normal mode commands, especially when you're matching around the cursor position or when the text you're ...


2

A couple of other non-visual options: [range]!cut -c1,4- Removes the 2nd and 3rd characters of each line in range using cut(1) (*nix only). [range]substitute/^.\zs..// Does the same, but using the :substitute command instead. There is a solution using getbufline and setbufline in a loop or with map, but it's a bit nasty and not worth it when these will do. ...


2

You can use getpos() to save those marks. You could restore them using straight setpos() on "'<" and "'>" directly, but that might not be enough, since you're not restoring the Visual mode you were in, so using a command such as gv will restore the initial selection but use blockwise visual mode (assuming that's what you used in the ...


2

Instead of vi(, the sequence vib is faster. b is more reachable and doesn't require shift. Some Lisp programmers remap their [] keys to insert parentheses. The problem with 16k0f(lyi(16j0f(lvi(p isn't the length so much as the specificity. For each instance where you want to do this, you have to measure the correct distance. Maybe it will be 14 lines next ...


2

What you want is to replace the text, not select it 1,4normal! ct.Whatever


2

The automatic bookmarks '< and '> get updated only after the Visual mode is left. Hence <Cmd> is not useful for your example, as you effectively apply the previous selection range, not the current one. You really should use "normal colon"-mapping here: xnoremap <Leader>c :w !xsel<CR> Note (1) use xnoremap instead of ...


2

The problem occurred because there is an inconsistency between the spaces and tabs used for indentation in your code. You have probably opened a tab indented code, and started editing it, while vim was set to indent using spaces. To solve this issue, first, set your tabbing space setting in your ~/.vim/vimrc by set softtabstop=4 set tabstop=4 set shiftwidth=...


2

Yes, pressing some keys in Vim could switch the current mode. Still surprised? This is documented under :h mode-switching. Obviously, here you have even two transitions: one by colon, and another one by enter key. But, I admit, it's confusing on the first encounter. In Neovim they even invented :h <Cmd> to deal with this. But in plain Vim you have only ...


2

If you want to move to the beginning or end of a paragraph of text, you can use } and { respectively. When in visual block mode, you can use { to move back to the beginning of the selection, but this deselects the text. The command to reselect the previous visual block gv can be used. For reference, the vim manual entry.


2

This is indeed a tricky one, since the column at both start and end will be the index of the first byte of a multi-byte character and subtracting them will give you a length in bytes and not in characters. The way I got to find the contents of the selection was to use strpart() twice, the first time using the start and end index to get all characters before ...


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