Moving lines up or down in a file

本文介绍了一种在Vim编辑器中高效移动文本行的方法,通过设置自定义快捷键,可以在正常模式、插入模式及可视模式下轻松地将当前行或选中的多行文本上下移动,并自动调整缩进。

Moving lines up or down in a file

From Vim Tips Wiki

Tip 646 Previous Next created 2004 · complexity basic · author Frank Butler · version 6.0


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 <M-j> mz:m+<CR>`z==
nnoremap <M-k> mz:m-2<CR>`z==
inoremap <M-j> <Esc>:m+<CR>==gi
inoremap <M-k> <Esc>:m-2<CR>==gi
vnoremap <M-j> :m'>+<CR>gv=`<my`>mzgv`yo`z
vnoremap <M-k> :m'<-2<CR>gv=`>my`<mzgv`yo`z

Press Alt-j to move the current line down, or press Alt-k to move the current line up. The == re-indents the line to suit its new position.

Explanation

The command :m 5 moves the current line to below line 5. If the number starts with + or -, it is relative to the current line, so :m +5 moves the current line down by 5 lines (+5 is interpreted as .+5 where . means the current line). The space after :m is not required, and the +1 can be written as + (the 1 is assumed).

See also

Comments

 TO DO 

  • A quick test shows that this is very nice, and if polished, it could be a featured tip.
  • Why does it use a mark? I only tried the normal-mode mapping, but it seemed to work fine with the mz and `z removed.
  • Need to improve my above explanation.

 

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
module four_floor_elevator_controller ( input wire clk, // 系统时钟 input wire reset, // 复位信号 input wire [3:0] call_up, // 外部向上召唤按钮 input wire [3:0] call_down, // 外部向下召唤按钮 input wire [3:0] select_floor, // 内部楼层选择按钮 output reg [1:0] current_floor = 2'd0, // 当前楼层 output reg door_open = 0, // 门状态(1为开,0为关) output reg moving_up = 0, // 上行状态(1为上行,0为停止) output reg moving_down = 0, // 下行状态(1为下行,0为停止) output reg fault = 0, // 故障提醒(1为故障,0为正常) output reg [6:0] floor_display // 数码管显示当前楼层 ); // 定义状态机状态 parameter IDLE = 2'd0, UP = 2'd1, DOWN = 2'd2; // 状态寄存器 reg [1:0] state = IDLE; // 请求寄存器 reg [3:0] up_request = 4'b0; reg [3:0] down_request = 4'b0; // 方向优先控制逻辑 always @(posedge clk or posedge reset) begin if (reset) begin state <= IDLE; current_floor <= 2'd0; door_open <= 0; moving_up <= 0; moving_down <= 0; up_request <= 4'b0; down_request <= 4'b0; fault <= 0; end else begin case (state) IDLE: begin // 检测是否有呼叫请求 if (call_up[current_floor + 1] || select_floor[current_floor + 1]) begin state <= UP; moving_up <= 1; moving_down <= 0; end else if (call_down[current_floor - 1] || select_floor[current_floor - 1]) begin state <= DOWN; moving_down <= 1; moving_up <= 0; end else if (call_up[0] || call_down[3]) begin // 方向优先控制 if (call_up[0]) begin state <= UP; moving_up <= 1; moving_down <= 0; end else if (call_down[3]) begin state <= DOWN; moving_down <= 1; moving_up <= 0; end end end UP: begin // 如果到达目标楼层 if (current_floor == 3 || select_floor[current_floor + 1]) begin state <= IDLE; moving_up <= 0; door_open <= 1; up_request[current_floor] <= 0; // 开门3秒后关闭 #30; // 假设时钟周期为10ms door_open <= 0; end else begin current_floor <= current_floor + 1; end end DOWN: begin // 如果到达目标楼层 if (current_floor == 0 || select_floor[current_floor - 1]) begin state <= IDLE; moving_down <= 0; door_open <= 1; down_request[current_floor] <= 0; // 开门3秒后关闭 #30; // 假设时钟周期为10ms door_open <= 0; end else begin current_floor <= current_floor - 1; end end endcase // 故障检测逻辑 if (door_open && (moving_up || moving_down)) begin fault <= 1; end else begin fault <= 0; end end end // 数码管显示逻辑 always @(*) begin case (current_floor) 2'd0: floor_display = 7'b1000000; // 显示"0" 2'd1: floor_display = 7'b1111001; // 显示"1" 2'd2: floor_display = 7'b0100100; // 显示"2" 2'd3: floor_display = 7'b0110000; // 显示"3" default: floor_display = 7'b1000000; // 默认显示"0" endcase end endmodule select_floor按键改为低电平有效,因为我设置的是按键
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值