62

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 other end of the highlighted text. The highlighted area remains the same. So when you have finished selecting the lines you want press o, the ...


42

:'<,'>s/\%V\ /\,0x/g %V matches inside the visual area. See :help %V.


35

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 of each language's comment characters... Option #1: V-blocks :1 Enter (Go to line 1) Ctrl-V (V-Block mode) jjj (Down 3 more lines) Shift-I (Enter insert mode ...


33

Visual-block selection: <C-v> jjl c* <Esc> :normal: :,+2norm! cE* Substitution: :,+2s/^.\{2}/* Dot formula: cE*<Esc> j. j.


28

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 make Vim feel like a conventional editor. I can only assume those were added to please a subset of Vim users, probably at the time Vim was ported to Windows (...


26

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 Mode to Normal Mode.


24

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 zo, or delete the fold with zd.


22

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 select the last changed block of lines with '[V'] and then apply the = command. However, since a paste leaves your cursor at the first line of the content that ...


22

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 every line. Try Ctrl-v -- select block -- c -- insert whatever -- Esc


19

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 list is finished by new line), otherwise use different methods like arrow keys (to move around the selection), or pattern search /c) where c is your last element)....


19

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 the first column and insert #. Using :norm I# will insert it before the first non-whitespace character, which may not be what you want.) Using :norm i# will ...


19

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.


19

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 that's currently in the index to the left/top of the original window. While in original window (right or bottom one), perform visual selection of changes you'd ...


19

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]} – Move [count] paragraphs forward. You can use <S-v>} to select all lines from the current line to the next blank line. This will include the blank line, so ...


17

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, python, and vimscript. You can do the calculations on a certain one or let the plugin automatically choose one for you. It works with your example like this: For ...


15

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.


15

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 selected the desired lines of the block you want, you can press I (that is Shift + i), you are now on insert mode, where you can add space in front of your line. ...


15

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 column newlines with + ic-R=c-Ra run the replaced "a through the expression register Alternatively: make the expression history entry re-usable for further sums ...


15

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 prefix it with a count. Edit In the comments @Antony pointed out an interesting command: :& (:h :&): & can be used in normal mode to repeat the ...


14

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, but you should also be able to achieve your actual goal (which you unfortunately didn't mention) with pure Vim commands, for example :global and :substitute (...


13

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('left') xmap <expr> <C-Right> DVB_Drag('right') xmap <expr> <C-Down> DVB_Drag('down') xmap <expr> <C-Up> DVB_Drag('up') ...


12

You can set the showcmd option. From Vim's help: Show (partial) command in the last line of the screen. Set this option off if your terminal is slow. In Visual mode the size of the selected area is shown: - When selecting characters within a line, the number of characters. If the number of bytes is different it is also displayed: "2-6" means two ...


12

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 command called "Vb". The ! after command means that vim will not throw an error if the command is already defined. normal! <C-v> - This command tells vim ...


12

A shortcut for next empty line is }. So you just might want to use SHIFT+v}


12

This can be looked at as a search-replace where the search is multi-level or nested. That is, first you need to match part of the line (the quoted string in this case). Then you need to match part of that string and do the replacement on that (underscores for us). Basic regexes weren't really meant to handle nested searches (there are methods, often complex,...


11

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 whole pattern is inside the Visual area put it at the start and end of the pattern, e.g.: > /\%Vfoo.*bar\%V To replace foo by ...


11

: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 considers spaces as field separator) END .......... at the end {print "Total: "sum} --> string "Total: " plus your result % ............ current file I have ...


11

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 execute the content of the * register which contains whatever is visually selected at the moment. Even if the text that is selected / highlighted is in your web ...


11

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 -range, it raises an error. In your visual mapping, you should probably add the keycode <C-U> to remove the visual range (it won't affect the marks '< and ...


11

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 register as Ex commands. See help :@ and https://stackoverflow.com/questions/20262519/vim-how-to-source-a-part-of-the-buffer


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