Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
44

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 ...


32

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


31

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 ...


26

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


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.


23

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 (...


21

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 ...


21

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.


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.


18

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 ...


18

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

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


16

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)....


15

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

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

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.


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}


11

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 ...


10

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 ...


10

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 ...


10

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


9

Doing it automatically would require you to add something like the following to your vimrc file (source): au FileType haskell,vhdl,ada let b:comment_leader = '-- ' au FileType vim let b:comment_leader = '" ' au FileType c,cpp,java let b:comment_leader = '// ' au FileType sh,make let b:comment_leader = '# ' au FileType tex let b:comment_leader = '% ' noremap ...


9

The '< and '> symbols mark the beginning and end of the last selected Visual area. For commands that take a range of lines or characters, '<,'> means that the range of the command is the selected area. jjaderberg suggests that the '>,'> range expression following a Visual selection would mean that the last line of the selection alone ...


9

The secret is to press $ after you have expanded your block vertically: <C-v>jj$ or to press $ before expanding your block vertically: <C-v>$jj Well, $ is the secret. …which is not that surprising, after all.


9

: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 ...


9

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 last line. See :h gv for more info.


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