0

I'm completely new to Vim and finding that most users assume that everyone instinctively knows how to use it ;)

I'm using it with Git and trying to figure out how to move a line up or down in a rebase command.

This answer says that I can do the following

ddkP

but I don't understand what to do. If I just type it in, how will Vim know which line I want to move?

Clarification appreciated!

half of a glazier
  • 1,864
  • 2
  • 15
  • 45
  • 1
    A good starting point is [The Vim Book](https://iccf-holland.org/vim_books.html#oualline), and the official web site: [https://www.vim.org/](https://www.vim.org/). – mouviciel Apr 06 '21 at 09:14

2 Answers2

4
ddkP

is actually three low-level commands put together into a single high-level one, performed in normal mode (the mode you are in by default):

  • dd cuts the current line,
  • k moves the cursor up by one line,
  • P puts the line that was cut above the current line.

So, you have done $ git rebase -i, which opened Vim with something like the following, in normal mode:

pick 07c5abd Introduce OpenPGP and teach basic usage <-- current line
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend

and you want to move 3e7ee36 up.

This is done by moving the cursor down two times with jj, making 3e7ee36 the current line:

pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting     <-- current line
pick fa20af3 git interactive rebase, squash, amend

then cutting the line with dd, making fa20af3 the current line:

pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick fa20af3 git interactive rebase, squash, amend   <-- current line

then moving the line up with k, making de9b1eb the current line:

pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls              <-- current line
pick fa20af3 git interactive rebase, squash, amend

then putting the previously cut line above the current line with P, effectively switching the two lines and thus moving 3e7ee36 up:

pick 07c5abd Introduce OpenPGP and teach basic usage
pick 3e7ee36 Hey kids, stop all the highlighting     <-- current line
pick de9b1eb Fix PostChecker::Post#urls
pick fa20af3 git interactive rebase, squash, amend

And that's about it.

Vim is no different than other professional tools like Photoshop or Blender or what have you. Anyone can start them and poke around without any prior experience but there is some learning and training necessary to extract the most value from them.

In Vim's case, you have two invaluable built-in resources at your disposal:

  • If you only use Vim casually, for quick edits like $ git commit or $ rebase -i, then going through $ vimtutor at least one time is pretty much required. Case in point, the command that is giving you trouble is explained under lesson 3.1.

  • If you plan to use Vim as your main editor, then the most sensible thing to do is to start an active reading of the user manual, :help user-manual. It is actually a thorough tutorial that takes you from "noob" to "advanced" in a very approchable, iterative manner.

If you think all of this is too much work for too little gain, then I would suggest you stick to nano or some similar, more comfortable, editor. There is no point picking up a notoriously weird and complex tool if you don't want to put in the required effort.


Sample rebase taken from this article.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    `If you think all of this is too much work for too little gain` - On the contrary, I enjoy learning to use new tools. In this case it was just that the task seemed too simple to have to start looking up tutorials and whathaveyous ;) – half of a glazier Apr 06 '21 at 11:01
2

These are Normal mode commands. Normal mode is what makes Vim different from other editors.

Say, in a "regular" editor if you press d you type "d" letter; so to delete the current line you need some special key combination, e.g. Ctrl-Y.

In Vim you press simply dd (without any controls, alts or such) to delete the current line. The drawback is that you have to switch to special Insert mode to be able to type letters. (And hence at some point also to switch from Insert back to Normal). So using Vim effectively implies "mode switch optimization", that is, a vimmer should have a few dozen of (Normal mode) tricks at his "fingertips". In fact, this is not so uncommon for an experienced user of any application, but in Vim beginners really suffer until they are able to type ddkP and other simple stuff without even thinking.

So typing ddkP in Normal mode means: "delete (cut) current line" (dd); then "move cursor up" (k); then "put (paste) the (last cut) line before current cursor (line)" (P).

Please, also note that complete beginners are strongly encouraged to go through vimtutor.

Matt
  • 13,674
  • 1
  • 18
  • 27
  • 1
    Would you mind adding in that `Esc` is used to switch between modes? That's the main difference when coming from other editors and it's what tripped me up as a newbie. I'll definitely check out `vimtutor` (took a question on SO to hear that it existed at all!) – half of a glazier Apr 06 '21 at 11:05
  • `` is not involved at all in the task at hand so it is irrelevant. – romainl Apr 06 '21 at 11:35
  • 1
    @Still_learning Type command `:help` (or simply press ``) to get to main help page. Read it from top to bottom. Besides other things it also mentions `vimtutor`. – Matt Apr 06 '21 at 11:38
  • @romainl What do you use to switch between Insert mode and Normal mode? As far as I made out it was `` – half of a glazier Apr 06 '21 at 11:57
  • There is no insert mode involved, here, so no switching to normal mode and therefore no ``. – romainl Apr 06 '21 at 12:03