How do I swap two lines in vim?
I have this:
pick 887b66f add 222 Dziewiecsil to flowers new title pick dc331cb new name of beginning commit
And I want to get this:
pick dc331cb new name of beginning commit pick 887b66f add 222 Dziewiecsil to flowers new title
Is there a way to do this in a quick way using keyboard shortcuts?
Swapping characters, words and lines | Vim Tips Wiki, Programmers often want to move a line of text up or down, or to some other position. We start by explaining the basics (cut and paste, as well as move), and Then highlight the lines that you want to comment out. To do so, place the cursor at the beginning of a line. Press SHIFT+V to highlight the whole line. After highlighting the first line, press DOWN arrow key to highlight the remaining lines one by one. Here is how the file will look like after highlighting the lines.
dd deletes the current line, then you can paste the removed line using
p. There's another way though using
m you can move lines around i.e.
:m 1 will move the current line after line 1
:m 0 will move the current line to top
:m $ will move the current line to bottom
In your example, place the cursor in the first line and type
Moving lines up or down | Vim Tips Wiki, First, you do not need to yank and delete; the latter will also put the deleted contents into the (default or specified) register. Therefore, ddp Yes. You can change just about anything you want to change in Vim. Just do. nnoremap <C-f> <C-b> nnoremap <C-b> <C-f> Add those two mappings in your vimrc file and you've both of those commands swapped. nnoremap - Maps new key combinations to existing commands without affecting any other commands that might be using the original command.
Despite the fact that question is quite old and marked as answered, I'd like to extend the answer by saying that you can use normal mode commands, which were provided by Sven Marnach with
nnoremap like so:
:nnoremap <C-Up> <Up>ddp<Up> :nnoremap <C-Down> ddp
This will allow you to move lines with Ctrl + Up and Ctrl + Down within your file. However this will overwrite
@" register, which stores your last copied string/word/letter/etc. So by adding "(reg) before
p commands we can fix this:
:nnoremap <C-Up> <Up>"add"ap<Up> :nnoremap <C-Down> "add"ap
Here we add
"a before delete and paste commands to store our line in
@a register, so your default copy register will not be overwritten. However it may overwrite contents of
@a register (who knows, but you may use it for something important in your use-case, but this step bit paranoid, you can skip it if you want to), let's fix that too:
:nnoremap <silent><C-Up> :let save_a=@a<Cr><Up>"add"ap<Up>:let @a=save_a<Cr> :nnoremap <silent><C-Down> :let save_a=@a<Cr>"add"ap:let @a=save_a<Cr>
<silent> needed to prevent echoing our commands to message-line at the bottom.)
Now we have two mappings which allow us to move lines within the file with keyboard shortcuts. You can redefine buttons, I use Alt + j/k, which would be
<A-k> for those commands. However not all terminal emulators support Alt key mappings AFAIK.
How can I easily move a line?, Can you put the logic in steps if you don't mind - for swapping two lines in vi editor. Regards Dileep Pattayath. Original Post by DILEEP410. I am using a Linux based system. In, vi or vim text editor, how do I show or hide line numbers? How do I show lines in vi? Introduction: One can make the vi or vim text editor display or hide line numbers using set number command.
vi editor - interchange two lines, swap two characters or lines * xp - swap two characters * ddp - swap two lines interact with the vim enviroment * Commands are entered on a command line 12 Answers 12. Move the cursor where you want to start. Press I. Type in the prefix you want (e.g. vendor_ ) Press esc . Press j to go down a line. Type . to repeat the last edit, automatically inserting the prefix again. Alternate quickly between j and .
Notes on Using Vim: The Basics, Step 1: select multiple lines in visual mode press V (upper case) and then use j or k to select lines. Step 2: call Swapcols like: :'<,'>Swapcols 1 3 (When you That will place the cursor on the n rather than the D. Vim also allows you to specify a cursor offset by line, or from the beginning or end of the string. To have the cursor land two lines above a matched string, for example, use /string/-2. To place the cursor two lines below the string, use /string/+2.
swapcol.vim, select the following paragraph (ones delimited by empty lines); > set nocompatible " vim-specific mode set backspace=2 " set backspace key to work like hlsearch " highlight the search set directory=c:\\temp\\ " location of swap files " restore To swap the current line with the next one, type ddp while in command mode. dd - delete line (actually called cut in other editors) and save it in register p - paste line from register