常常需要在服务器上捣鼓东西,同时需要将内容复制到本地的需求。
1-内容是在远程终端用vim打开,如何用vim的类似指令达到快速复制到本地呢?
假设待复制的内容:
#include <iostream>
#include <cstring>
using namespace std;const int N = 1e3 + 10;int v[N], w[N];
int f[N][N];
int n, m;
int main() {cin >> n >> m;for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i];for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {f[i][j] = f[i-1][j];if (v[i] <= j)f[i][j] = max(f[i][j], f[i-1][j-v[i]] + w[i]);}}cout << f[n][m] << endl;return 0;
}
解决方案
配置vim
创建~/.vimrc并写入如下内容后使其生效:source ~/.vimrc。完成后就可以在vim里用v
选择,y
复制,然后本地正常粘贴即可。
syntax on "自动语法高亮
set number "显示行号
set cindent
set tabstop=4 "tab键,vim显示的空格数"
set shiftwidth=4 "缩进长度
set backspace=2 "允许退格键删除
set smartindent "开启新行时使用智能自动缩进
set showmatch "插入括号时,短暂地跳转到匹配的对应括号
set ruler "打开状态栏标尺
set mouse=a "在vim所有模式下开鼠标,复制文档就可以不包含行号了
set nocompatible "不与vi兼容(采用vim自己的操作命令)"
set showmode "在底部显示,当前处于命令模式还是插入模式"
set encoding=utf-8 "使用utf-8"" 函数:将寄存器内容发送到本地剪贴板
function! Osc52Yank()let buffer=system('base64 -w0', @0)let buffer=substitute(buffer, '\n$', '', '')let buffer='\e]52;c;'.buffer.'\a'silent call system('printf '.shellescape(buffer).' > /dev/tty')
endfunction" 自动在复制后调用
augroup Osc52Yankautocmd!autocmd TextYankPost * if v:event.operator ==# 'y' | call Osc52Yank() | endif
augroup END
这个方法成功的前提是你的本地终端工具要支持Osc52Yank以及允许终端应用访问剪切板,支持的终端列表参考[1]。
2-想复制的是某个命令或输出在终端上的内容,同时不想用鼠标?
假设在tmux里面,为了使得复制到本地的功能像上述般丝滑,需要配置tmux。
解决方案
配置tmux
创建 ~/.tmux.conf 并写入下面内容[2]后使其生效 tmux source ~/.tmux.conf,然后就可以ctrl + b [
进入tmux选择模式,后面和上述vim类型,v
选择,y
复制退出选择模式,然后无痛粘贴即可。
set -g mode-keys vi# 增加历史缓冲区
set -g history-limit 100000# 进入copy-mode
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi Escape send-keys -X cancelbind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"set -g mouse on# 优化翻页键
bind-key -T copy-mode-vi C-f send-keys -X page-down
bind-key -T copy-mode-vi C-b send-keys -X page-up
bind-key -T copy-mode-vi C-d send-keys -X halfpage-down
bind-key -T copy-mode-vi C-u send-keys -X halfpage-upset-option -g set-clipboard on
set-option -ga terminal-overrides ',xterm-*:clipboard'
这个方法成功的前提和上述类似。
参考
[1] https://github.com/ojroques/vim-oscyank
[2] 终端神器tmux:多任务管理大师