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,637
  • 3
  • 22
  • 28
  • By looking at the current answers, I realized I should have specified further - I do NOT want to use magical numbers. In the case of `4j`, 4 is a magical number. I want a quick way of doing this without having to spend time doing the manual counting and that jazz. – krystah Dec 28 '13 at 13:30
  • I've provided a solution without "magic numbers," but you didn't prohibit searching for letters, which I still employ. You're going to have to make some kind of concession in terms of finding the longest word in the column so that the selection block is the right size (if you want the whitespace to be perfect as well). – treddy Dec 28 '13 at 14:32
  • I was referring to the `9999999j` part of your answer. There are several reasons why I don't want to use magic numbers like that. The main reason is obviously that it doesn't play well if there is content below the data-table. If there were only a way to move down until an empty line.. Hm. – krystah Dec 28 '13 at 15:04
  • 4 or 9999 are not magical numbers (they are obviously different and context-dependent so there's nothing magical with them) but what you *want* is a magical method: it would work the same in any context. If you didn't find anything in `:help`, on vim.org or on the vim wiki it simply don't exist. Are you asking us to write that function for you? – romainl Dec 28 '13 at 16:40
  • That's exactly what I mean by 'magical'. Non-flexible, context-dependent numbers. I am trying to get around this WITHOUT resorting to a grand function or an existing plugin. So basically what I'm asking for is the following: Does there exist a known Vim textobject that encompasses the current block? If yes, I'd love to hear it. If not, I'll resort to a plugin. – krystah Dec 28 '13 at 16:46
  • Well, that's exactly the opposite of a magical number but whatever, there's no native "jagged column of text" text-object (as you already know if you have read `:help motion.txt`) but there's a plugin for that, of course, see my answer. – romainl Dec 28 '13 at 16:52

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
  • 22,554
  • 2
  • 49
  • 59
  • All of these solutions seem too manual for my original intent, so I'll go for the plugin, thanks. – krystah Dec 28 '13 at 17:02
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
  • 2
  • Yeah, both you and ygonchos answers reach the goal in the end, but I am looking for a way to do this without "magical numbers". I specified further in a comment below my question. – krystah Dec 28 '13 at 13:31
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
  • Yeah, both you and treddy's answers do the job, but I am looking for a way to do this without "magical numbers". Look at the comment below my question :) – krystah Dec 28 '13 at 13:32
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

jgeralnik
  • 1
  • 1
  • That was my initial thought when reading the solution, but G pays no regard to your current horizontal position and may very well move you to the first character of the last line, if that is indeed the last character in the file. Kind of icky – krystah Dec 28 '13 at 19:45
  • First off, neither does 99999j. But assuming your data is formatted as in the question without an empty line at the end it works just fine. – jgeralnik Dec 28 '13 at 19:51
  • The thought use-case for this question was to easily format and work with static arrays and such, so I would practically never have nothing below it. But check out the plugin romainl linked, it's really neat! – krystah Dec 28 '13 at 20:08