How to use visual modes in Vim

vim.png

There are three different types of highlighting in visual mode. Each allows the user to highlight text in different ways. Commands that normally only affect one character, line, or area will affect the highlighted text (such as changing text to uppercase (<Ctrl-~>), deleting text (<d>), indenting lines (>>, <<, and =), and so forth).

There are three (sub)types of the visual modes which are visual, block-visual , and linewise-visual

plain visual mode

The plain visual mode is started by pressing ‘v’ in normal mode. At any point, pressing ESC or <v> will leave VISUAL mode without performing an operation. Movement commands change the selection area, while other commands will generally perform the expected operation on the text (there are some exceptions where the behavior will change or where the command won’t work, but if it doesn’t do what you hoped you can always undo with <u>).

block visual mode

block-visual is started by pressing <Ctrl-V> (or <Ctrl-Q> in some windows versions. If neither of these works use “:help visual-block” to find out how). Visual blocks always maintain a rectangular selection, highlighting only specific columns of characters over multiple lines. In this following example the user wants to put a dash in each phone number between the second and third number fields:

The user first moves the cursor to the top of the column (you could start at the bottom if you want).

vim_block_change_example

Next, press <Ctrl-V>. This puts you in block-visual mode (VISUAL BLOCK appears at the bottom to tell you what visual mode you’re in). Next, move down to the bottom desired line. You can see a single column highlighted in this example, but you could move right or left and highlight more columns.

vim_block_change_example2In this case, the user wants to change the spaces to dashes. To change text, we press ‘c’. The spaces all disappear, and the changes are shown only in the current line while we type:

vim_block_change_example3

when we press <ESC>, though, the change is duplicated on all the lines.

vim_block_change_example4

(Note: if you simply want to insert text rather than change it, you will need to use ‘<I>‘ or ‘<A>‘ rather than ‘<i>‘ or ‘<a>‘.)

linewise visual mode

In linewise-visual mode, enterd by <Shift-V>, entire lines are highlighted. Otherwise, it generally works like the plain visual mode.

select

like the visual mode but with more CUA like behavior. This means that if you type a single character it replaces the selection. Of course you lose all the one key operation on selection like <U> to make a selection uppercase.

This mode is usually activated by:

:behave mswin

which is default for MS-Windows installations. You can get the normal mode with

command-line

Within the command-line you can run Ex commands, enter search patterns, and enter filter commands. At the bottom a command line appears where you can enter the command. Unlike vi – vim supports cursor keys which makes entering commands a lot easier. After one command the editor returns into normal mode.

You can enter an Ex command by typing a : in normal mode. Some examples include:

:set number
:substitute/search/replace/ig

You can enter a search pattern by typing / to search forward, or ? to search backward. You can use vim’s expanded regular expressions in these search patterns. For example,

/word

will jump to the next occurrence of “word” (even if it is “sword” or “wordlessly”), but

/\<word\>

will jump only to a complete word “word” (not “sword” or “wordless”).

You can enter a filter by typing ! followed by a motion command, then a shell command to run on the text captured by the motion. For example, typing

!22jsort

in Linux will sort the current and 22 following lines with the sort system command. The same thing can be done with

:.,.+22!sort

As a matter of fact, vim creates the above command for you if you follow the first example!

Ex-mode

The Ex mode is similar to the command line mode as it also allows you to enter Ex commands. Unlike the command-line mode you won’t return to normal mode automatically. You can enter an Ex command by typing a Q in normal mode and leave it again with the :visual command. Note that the Ex mode is designed for Batch processing and as such won’t support mappings or command-line editing.

For batch processing the Ex-mode is normally started from outside by calling the editor with the “-E” option. Here are real live example form a RPM Package Manager specification:

vim -E -s Makefile <<-EOF
   :%substitute/CFLAGS = -g$/CFLAGS =-fPIC -DPIC -g/
   :%substitute/CFLAGS =$/CFLAGS =-fPIC -DPIC/
   :%substitute/ADAFLAGS =$/ADAFLAGS =-fPIC -DPIC/
   :update
   :quit
EOF

The RPM uses Bash as script language which make the example a little difficult to understand as two different script languages are mixed in one file.

vim -E -s
starts vim in improved Ex mode which allows for more advanced commands than the vi compatible Ex-mode (which is started with vim -e -s).
<<-EOF
tells bash to copy all lines that follow into the standard input of the external program just started.
:
are lines with Ex commands which vim will execute. The : is optional but helpful when two script languages are mixed in one file
:update
A beginners mistake is to forget to actually save the file after the change – falsely assuming that this happens automatically.
:quit
Last not least: don’t forget to actually exit vim again.
EOF
marks the end of the standard input redirection – from now on bash will execute the command itself again.

If your shell does not allow such nifty redirection of standard input then you can always use a more classic approach to I/O redirection using two files:

vim -E -s Makefile <Makefile-Fix1.vim

And if have no standard input redirection available then you can try the -c option in combination with the source command:

vim -E -s -c "source Makefile-Fix1.vim" Makefile

With the improved Ex mode many tasks classically performed by awk or sed can be done with vim and often better so:

  • awk and sed are stream oriented – they only read the file forward from beginning to end while vim is buffer oriented – you can move forward and backward in the file as you like.
  • vim’s regular expressions are more powerful than awk’s and sed’s expressions – for example vim can match over several lines and supports zero matches.

Source, Related

Related Posts

Comments are closed.