首页 > 其他分享 >在Vim中上下移动整个行

在Vim中上下移动整个行

时间:2023-06-12 23:44:27浏览次数:32  
标签:after move lines up Vim current 上下 line 移动

 

当您在中命中命令:help movevim,结果如下:

:[range]m[ove] {address} *:m* *:mo* *:move* *E134* Move the lines given by [range] to below the line given by {address}.

例如: 将当前行向下移动一行=> :m+1

例如: 将数字100的行移到数字80 =>的行下方 :100 m 80

例如: 将数字100的行移到数字200 =>的行下方 :100 m 200

例如: 将编号为200的行移动到[200,>]的下方[100,120]之内 :100,120 m 200

 

 

 

向上移动一行: ddkP

向下移动一行: ddp

 

 

yy     p

 

在Vim中上下移动整个行 (qastack.cn)

 

关于vi:在Vim中上下移动整个行 | 码农家园 (codenong.com)

 

key bindings - How can I easily move a line? - Vi and Vim Stack Exchange

 

Moving lines up or down | Vim Tips Wiki | Fandom

Moving lines up or down

EDIT   Tip 646 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0


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 show how to append to a register. An amazing trick with the redo register is then presented (useful for reordering up to nine lines), and there is a set of mappings so you can press a key to move the current line, or a block of selected lines, up or down.

Contents

Cut and paste

Cut the line that you want to move by typing dd, or visually select some lines (press V then move the cursor) and type d to cut the selected block.

Then move the cursor, and paste the text at the new position (press p to paste after the line with the cursor, or P to paste before).

You can cut several lines, or blocks of text, by appending to a register using an uppercase letter, for example:

  • Move to the first line you wish to move; type "add to delete it to register a.
  • Move to another line; type "Add to delete it and append to the same register.
  • Move to another line; type "Add to delete it and append to the same register.
    • or type "." for the same effect
  • Select a range of lines vith V; type "Ad to delete the entire range and append it to the same register.
  • Continue in this fashion.
  • Move to the wanted destination.
  • Press "ap to paste after the line with the cursor, or "aP to paste before.

Move command

You can move a line, or a block of lines, with the :m command. Examples:

:m 12 move current line to after line 12
:m 0 move current line to before first line
:m $ move current line to after last line
:m 'a move current line to after line with mark a (see using marks)
:m 'a-1 move current line to before line with mark a
:m '}-1 move current line to the end of the current paragraph

For clarity, a space is shown after the :m commands above, but that space is not required.

To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary ranges with the move command. Examples:

:5,7m 21 move lines 5, 6 and 7 to after line 21
:5,7m 0 move lines 5, 6 and 7 to before first line
:5,7m $ move lines 5, 6 and 7 to after last line
:.,.+4m 21 move 5 lines starting at current line to after line 21
:,+4m14 same (. for current line is assumed)

Reordering up to nine lines

The following example lines can be moved to a different order by deleting each line in turn (starting with the line that will be first when the move is complete):

line 3
line 9
line 8
line 1
line 5
line 7
line 2
line 6
line 4

Move the cursor to "line 1" and type dd to delete the line. Go to "line 2" and press . to repeat (delete another line). Repeat this on "line 3", and so on, until everything has been deleted in order.

Now type "1P to paste the contents of register 1 before the cursor.

Repeat with the dot command, eight times:

........

The first dot command pastes register 2, and the next pastes register 3, and so on. The result is that all the lines are pasted, in the correct order.

You have to press . eight times (using a count like 8. will insert the same line eight times). See :help redo-register

Mappings to move lines

The following mappings in your vimrc provide a quick way to move lines of text up or down. The mappings work in normal, insert and visual modes, allowing you to move the current line, or a selected block of lines.

nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.

After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.

Explanation

The command :m .+1 (which can be abbreviated to :m+) moves the current line to after line number .+1 (current line number + 1). That is, the current line is moved down one line.

The command :m .-2 (which can be abbreviated to :m-2) moves the current line to after line number .-2 (current line number − 2). That is, the current line is moved up one line.

After visually selecting some lines, entering :m '>+1 moves the selected lines to after line number '>+1 (one line after the last selected line; '> is a mark assigned by Vim to identify the selection end). That is, the block of selected lines is moved down one line.

The == re-indents the line to suit its new position. For the visual-mode mappings, gv reselects the last visual block and = re-indents that block.

See also

 

 

标签:after,move,lines,up,Vim,current,上下,line,移动
From: https://www.cnblogs.com/sinferwu/p/17476374.html

相关文章

  • WMIC远程执行命令横向移动
    WMIC远程执行命令横向移动目录WMIC远程执行命令横向移动一、wmic介绍二、wmic常用命令三、wmic配合powershell上线CS四、Wmiexec配合powershell上线CS五、wmiexec.vbs配合powershell上线CS六、Invoke-WMIExec.ps1配合powershell上线CS七、Invoke-WMIMethod.ps1上线CS一、wmic介......
  • Qt打包程序移动到新环境时提示QAxBase::setControl: requested control Excel.Applica
    在新环境中运行程序时发现程序异常结束,查看日志内如如下,异常原因应该是我是程序中对Excel表格操作产生的。Warning:File:()Line:(0)QAxBase::setControl:requestedcontrolExcel.Applicationcouldnotbeinstantiated(2023-06-1217:40:58)Warning:File:()Line:(0)QAx......
  • Qt打包程序移动到新环境时提示 QMYSQL driver not loaded
    Qt版本是:Qt6.3.2MySQL版本是:mysql8.0.33运行时日志提示如下:Warning:File:()Line:(0)QSqlDatabase:QMYSQLdrivernotloaded(2023-06-1217:16:56)Warning:File:()Line:(0)QSqlDatabase:availabledrivers:QSQLITEQMARIADBQMYSQLQODBCQPSQL(2023-06-1217:16:56)......
  • 【vim】批量替换文件内容
    一、场景  由于测试时,需要替换配置,手动替换很慢,所以就需要批量替换。 二、操作方法打开需要修改的文件进入命令模式,输入:%s/要替换的内容/替换后的内容/g,其中%表示对整个文件进行替换,g表示全局替换。按下回车键,Vim会自动替换所有匹配的内容。你也可以使用正则表......
  • iPhone两秒出图,目前已知的最快移动端Stable Diffusion模型来了
    前言 近日,Snap研究院推出最新高性能StableDiffusion模型,通过对网络结构、训练流程、损失函数全方位进行优化,在iPhone14Pro上实现2秒出图(512x512),且比SD-v1.5取得更好的CLIPscore。这是目前已知最快的端上StableDiffusion模型!本文转载自机器之心仅用于学术分享......
  • vim使用
    在Vim中选中多行、删除多行和定位到指定位置的方法如下:选中多行:进入命令模式:按下Esc键确保处于普通模式。移动光标到起始行:使用上下方向键或j、k进行移动。进入可视块选择模式:按下大写字母V。移动光标到结束行:使用上下方向键或j、k进行移动。选中多行后,你可以进行剪切、复......
  • 一致性哈希算法——算法解决的核心问题是当slot数发生变化时,能够尽量少的移动数据
    一致性哈希算法摘自:http://blog.codinglabs.org/articles/consistent-hashing.html算法简述一致性哈希算法(ConsistentHashing)最早在论文《ConsistentHashingandRandomTrees:DistributedCachingProtocolsforRelievingHotSpotsontheWorldWideWeb》中被提出。简单来......
  • odoo context上下文用法总结
    环境odoo-14.0.post20221212.tarcontext用法总结获取上下文>>>self.env.context#返回字典数据,等价于self._context{'lang':'en_US','tz':'Europe/Brussels'}>>>self._context{'lang':'en_US',&......
  • 【帮转】网商银行大前端信息科技移动渠道部 2024届校招实习
    我们追求极致,但一直不够完美,因为还缺少独一无二的你。如果你是前端界的切图仔,脚本小子,天才极客、绝世高手、Github达人...,请给我们投简历吧。这里有通往华山之巅的缆车,师兄们已在山顶等你,优秀将会是你一辈子甩不掉的标签。不得不提醒下,加入我们后,你可能需要学会婉拒大量的异性表......
  • 传递攻击-横向移动
    传递攻击-横向移动目录传递攻击-横向移动一、哈希传递攻击(PTH)二、哈希传递条件三、哈希传递攻击步骤四、密钥传递攻击(PTK)五、KB2871997补丁六、密钥传递攻击步骤七、票据传递攻击(PTT)八、票据传递攻击两种方式九、票据传递攻击步骤一、哈希传递攻击(PTH)哈希传递攻击(PassTheHash......