MacOS下如何将Python Shell输出的列值插入Vim文件作为第三列
Absolutely doable on macOS! Here's a step-by-step breakdown to get that column of numbers inserted right after your second column smoothly:
1. Check Vim's Clipboard Support
First, confirm your Vim can access the system clipboard. Open Vim and run:
:echo has('clipboard')
If it returns 1, you're all set. If 0, you’ll want to use MacVim (it has clipboard support out of the box) or install a Vim build with clipboard support via Homebrew:
brew install vim
2. Paste the Column as a Block
Assuming you’ve already copied the column values to your system clipboard (via Cmd+C), follow these steps in Vim:
- Open your target file and move your cursor to the end of the second column in the first row (this is where your third column will start).
- Press
Ctrl+Vto enter Visual Block mode (you mentioned you already know this part—perfect!). - Use the down arrow key to select all the rows where you want to insert the third column (make sure the number of selected rows matches the number of values in your clipboard).
- Press
A(capital A) to enter block append mode. This jumps you to the end of the selected block on the first row. - Add your column separator first (e.g., press
Tabfor tab-delimited columns, or a few spaces if using space separation). - Now paste from the system clipboard: press
Ctrl+R, then type+(this calls the system clipboard register). You’ll see the first value appear right away. - Press
Esc—Vim will automatically apply the pasted values and separator to all selected rows, turning your clipboard content into a clean third column!
Alternative Command-Line Method
If you prefer a more command-focused approach, use macOS’s pbpaste tool directly in Vim:
- Move your cursor to the first row where you want the third column.
- Enter Visual Block mode (
Ctrl+V), select the rows, then run:
:'<,'>s/$/\t\=system('pbpaste')->split("\n")[line('.')-line("'<")]/
This command appends a tab plus the corresponding line from your clipboard to each selected row. Adjust the \t to a space or other separator if needed.
内容的提问来源于stack exchange,提问作者user1773603

