7

Considering we have the current data:

ID     NAME          AGE 
1      Joan          29
2      Peterson      16
3      Hunt          47
4      Wenche        12
5      Kennedy       29
6      Lottie        31

And the cursor is the on the N in NAME, how would I go about selecting the text so that, if | is the delimiters of the visual block, the selection should be like this

ID    |NAME    |     AGE 
1     |Joan    |     29
2     |Peterson|     16
3     |Hunt    |     47
4     |Wenche  |     12
5     |Kennedy |     29
6     |Lottie  |     31

The trailing whitespace after each element to match the width of Peterson is not necessary, but I need a quick way to highlight the current block, if it exists.

krystah
  • 1,697

4 Answers4

1

With the cursor on the N of NAME, I would simply do the following without much care for golfing:

<C-v>}hhhhhhhh

But the textobj-column plugin does exactly what you want. Magically.

romainl
  • 23,415
0

Here's one way to do it:

  1. First use control-v to enter visual block mode
  2. Then type 2j to go down two rows to the Peterson entry
  3. Use lower case e to move the block selection to the end of the word Peterson
  4. The type 4j to move the selection down the final four rows

Edit: If you really insist on avoiding "magic numbers" you can do something as described here: https://stackoverflow.com/questions/3736678/how-do-you-select-a-whole-column-in-visual-block-mode

  1. :set nosol (so that G command will not wrap back to first line in visual block selection)
  2. control-v
  3. /P (you said no magic numbers, but didn't prohibit searching for letters--if the whitespace isn't crucial you can ignore this)
  4. e (go to end of word)
  5. G (move block selection to the bottom)

Other tips:

  • if you need to reselect the same block later you can type gv
  • v is used for character-wise visual mode and V for line-wise, while control-v as above is for block-wise Visual mode

Useful references (apart from Internet):

  • The book Practical Vim by Drew Neil is really well-written
treddy
  • 101
0

Scripts exist to make this easier, but assuming you want to use raw vim here's my best known method to do this:

a. Make sure your last line of the file is full of whitespaces, something like this:

"______________________________________".

then go to the first letter N in "Name". Hit ctrl-v, w, 999999j and the column will be chosen.

You need the last line to be full of spaces in case your file ends in a newline because visual mode cannot select parts of the file that have not been created. if the last line is a viable content, you don't need this. ctrl-v+w selects a word, then 999999j simple takes the cursor "down" 99999 lines (or less if the file is smaller).

Note: You will see that this is an entire column selection and the whitespaces are a bit different from what you stated (but you said this was unimportant). As far as my knowledge goes, if you want the column to be aligned to the end of the largest word and not to the start of the next one, you will need to manually find the largest word size, select visual that length and then do something like 99999j.

ygoncho
  • 101
0

The same as ygoncho's answer, but without "magic numbers"

ctrl-v, w, G

G goes straight to the last line of a file without having to smash '9' a few times