I'm wondering if there's a command in Vim I'm missing that does columnar motion. What I'm thinking of is common in some spreadsheets, where there's a key combination that will take you downward in the column to either the next blank cell, or if you're in a blank cell then it will take you to the next non-blank cell. Some text may illustate:
a-------
-------
-------
b-------
c-------
-------
-------
d-------
e-------
-------
What I'm looking for is most efficient way to move from a to b, from b to c, from c to d, from d to e. I know this can be done using / searches, but is there a more efficient way using something analous to the line-based motion commands? Seems like there should be, but I can't find it.
I'm not sure if this fits your request exactly but vim has sentence, paragraph, and section movement keys built-in. In your case, I think you probably want the paragraph ( '{' and '}' ) or section ( '[' and ']' ) motion keys.
Try this to jump between non-empty lines:
:nmap j /^.\+$<CR>
Related
In a general sense, my question is how do I do something like "dw" or "dd", but instead of deleting characters, I want to over-write with spaces?
E.g. lets say I have text:
first second third
if the cursor is on the "s" in second, I can hit "dw" to get:
first third
but what if I want:
first third
Is there a simple way to do that? An ideal solution would be to use the "d" style syntax (e.g. dw, daw, d$, etc.) but with whitespace replacement instead of deletion.
From the start of the word,
Ctrl-v to enter visual block mode,
e to move to the end of the word (highlighting the word in the process),
r[SPACE] to replace the highlighted characters with spaces.
Because of their very nature (the next character must be consumed), r and R can't work like operators. If you want to replace a motion, visually select it first, and then do r<Space> or r_ or whatever.
In this very specific case:
ver<Space>
or:
viwr<Space>
NOTE: I used ve and viw because the semantics of w are inconsistent so I prefer to avoid it when possible.
I have this problem where I try to yank/cut and paste a block of text with Vim and it has inconsistent behavior.
What happens is this:
In this text bellow, I what to cut the middle column and paste after the third. So I block-select (ctrl-v) the text, cut it (x), take the cursor to the end of the third column (now 2nd) and paste (p)
A 1 a
B 2 b
C 3 c
D 4 d
Sometimes Vim does it right and the result is this:
A a 1
B b 2
C c 3
D d 4
and some times, the result is this:
A a
1
2
3
4
B b
C c
D d
I need to control that behavior instead of being a hostage of it... How do I control this behavior?
Well, one answer is the UnconditionalPaste plugin (https://vimawesome.com/plugin/unconditionalpaste)
with a 'gbp' you paste a block inline (I remapped to [LEADER]pb)
it works...
I recommend you reading the help topics for visual selection,
:h visual.txt. The section on visual-operators and its notes might
be particularly useful. Selection behavior is pretty consistent and easy
to understand though. If you cut or copy something, expect it to be
placed in the same mode: characters, lines, or blocks.
I what to cut the middl ecolumn and paste after the third. So I
block-select (ctrl-v) the text, yank it (y), take the cursor to the
end of the third column (now 2nd) and paste (p)
That's not how you should do it. To "cut" the middle column, as you
describe, you came to use x and not y. Yanking will just copy it and
leave it there.
Regarding your last example where content was pasted in a linewise
fashion, that should not happen when using p since it conserves the
blockwise information. The only possible explanation that comes to my
mind is you deleted a column and used :put to paste it. That will
convert it to a linewise paste. Alternatively:
You yanked something likewise (say from V) and you are trying to
paste it blockwise
Your example does not translate well your problem
There is some sort of plugin or configuration affecting this
Random unknown keys were slammed in the process
Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)
using text-objects
determine what text-object would map to what you want to yank:
inner/outer word, iw/aw
inner/outer pair, i'"([{</a'"([{<
inner/outer html tag, it/at
sentence, s
paragraph, p
"block", ]
…
yank that text-object with, say, yip
using other motions
yank to end of function: y]}
yank to end of file: yG
all of the above solutions with visual mode
V'ay
V/foo<cr>y
V?bar<cr>y
Vipy, etc.
V]}y
VGy
:h motion.txt will hopefully blow your mind, like it did to mine.
You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.
Then, you can paste them using p.
You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.
Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.
Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.
When you're using vim, you can move forward word by word with w. How do I go backwards?
Use b to go back a word.
You may also want to check out W and B to advance/go back a WORD (which
consists of a sequence of non-blank characters separated with white space, according to :h WORD).
It helps for me to think of it as:
b to go to beginning of current or previous word
w to go the beginning of next word
e to go to the end of current or next word
ge to go the end of the previous word
Try :h word-motions for more details and how to combine them with operations.
use "b" to move back - just tested in vi - works fine.
Alternatively, if you use w, b, W, and B to navigate lines by hopping over words, consider the following alternatives which can be faster if used correctly.
f<char> # jump to next occurrence of <char> to right (inclusive)
or
F<char> # jump back to next occurrence of <char> to left (inclusive)
If your words are separated by spaces
If your words are separated by <space> you can hop over words by spaces:
f<space>;;;; where ; repeats the previous command, so you hop forward by spaces
F<space>;; to hop backwards by space
If your words are separated by punctuation and not spaces
just replace <char> with punctuation, for example .
The punctuation method is not efficient for scrolling through, but if you know where you want to jump, it can usually get there in a jump or two.
I use the shortcut w to move the cursor one word right. Is there a shortcut to move a word left?
Use b to move back one word.
Use w to move forward one word.
And here is a cheat sheet that might be useful for you:
Source: Graphical vi-vim Cheat Sheet and Tutorial
It's b.
You can see other motions here:
Vim documentation: motion, 4. Word motions
Generally a Vim command consists of:
count action motion
Where:
count is number of times you want it to execute. The default is 1.
action is obviously an action: d for delete, c for change, default is empty, and it means simply move.
motion is the direction. You got that already.
In addition to the b movement mentioned in the other answers, another movement which may be interesting is ge.
It brings you to the last letter of the word on the left. When b is considered as the opposite of w, ge can be considered as the opposite of e which brings you to the end of the current word.
Also note that all of those word-wise movement have a WORD-wise equivalent: W, B, E and gE which are "faster". For the difference between words and WORDS see: :h word.
Yes, you can use "b" to backforward a word, and in advance, "2b" to move back two words.
You want to move left (back). b does it.