비주얼 모드의 텍스트 블록을 Vim의 파일에 어떻게 저장할 수 있습니까?
제목은 매우 설명 적입니다. 혹시라도 예를 들어 보겠습니다.
START BLOCK1
something
END BLOCK1
START BLOCK2
something
somenthing...
END BLOCK2
- 나는
BLOCK1
비주얼 모드를 선택 - 눌러서 잡아 당겨 y
- 얀크
BLOCK1
파일을 다른 파일에 어떻게 저장할 수 있습니까?
라인 비주얼 또는 블록 비주얼 모드에서 저장할 텍스트를 선택하고
:w new.txt
그것은 당신이 입력하는 것이지만 실제로 위에있는 것을 정확하게 볼 수는 없습니다 . 를 누르면 :
선택 정보가 자동으로 채워지는 명령 줄로 이동합니다. 다음과 같이 보일 것입니다.
:'<,'>
나머지 ( w new.txt
)를 계속 입력 하면
:'<,'>w new.txt
... Enter를 누르십시오.
블록을 선택하면 :'<,'>w other-file
, 선택한 블록 만에 쓸 수 있습니다 other-file
. :
비주얼 모드에서 치면 '<,'>
이미 명령 줄에 배치되어야 하므로 실제로 :w other-file
.
이 작업을 수행하는 더 간단한 방법이있을 수 있지만 새 버퍼 (또는 탭)를 만든 다음 p
. 로 새 버퍼를 만들 :new
거나로 새 탭을 만들 수 있습니다 :tabnew
. 를 사용하여 정상적으로 파일에 버퍼 / 탭을 쓸 수 있습니다 :w filename
.
주석에 언급 된 @dronus와 같이 :w !pbcopy
제안은 전체 행을 복사하기 때문에 올바르게 복사되지 않습니다. 한 줄에있는 URL 만 복사하려면 복사 할 수 없습니다. 다음 .vimrc
은 CTRL-C를 누를 때마다 vim에서 선택한 줄이 클립 보드에 복사되도록 파일에 추가 할 수있는 줄입니다 .
map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>
이것이하는 일에 대한 자세한 내용을 읽으려면 내 블로그 에서 이에 대해 읽을 수 있습니다.
@rmeador가 제안한 것과 동일한 구현입니다.
@songz의 솔루션과 유사하게 ": new"를 사용하여 이렇게하는 것이 좋습니다.
vmap <C-c> y:new ~/.vimbuf<CR>VGp:x<CR>:!pbcopy < ~/.vimbuf<CR><CR>
@chenkaie의 변형을 기반으로하면 나에게 잘 작동합니다.
let mapleader = "," let g:mapleader = "," vmap <leader>y y:new ~/.vbuf<CR>VGp:x<CR> nmap <leader>p :r ~/.vbuf<CR>
Vim은 시각적 선택을 가져와 파일에 저장합니다.
function! Get_visual_selection()
#Get the position of left start visual selection
let [line_start, column_start] = getpos("'<")[1:2]
#Get the position of right end visual selection
let [line_end, column_end] = getpos("'>")[1:2]
#gotta catch them all.
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
#edge cases and cleanup.
let lines[-1] = lines[-1][: column_end - 2]
let lines[0] = lines[0][column_start - 1:]
return join(lines, "\n")
endfunction
function Save_visually_selected_text_to_file()
let selected_text = Get_visual_selection()
call writefile(split(selected_text, "\n"), "/tmp/something.txt")
endfunction
#the c-u does a union of all lines in visual selection.
#this goes in the vimrc
vnoremap <F10> :<c-u>call Save_visually_selected_text_to_file()<cr>
위에서 선택한 답변 외에도
마우스로 (1)을 선택하면
and the problem of only copying whole lines mentioned by the comment of @dronus to it, when just wanted to partly copy lines (2):
(1) At my Debian based DietPi (Raspberry PI)system, vim acts a little different like in the preferred solution above when using the mouse to enter and select 'VISUAL MODE' on my Ubuntu 16.04 work station. Then
- y to yank it
but if I type ':' for command, it will not show up with the
'<,'>
where I can just simply add my
w new.txt
after it. So I just typed it by myself and it did work:
'<,'>w new.txt
and it copies the whole line(s) yanked content to my file 'new.txt', whereas '<,' seem to mean 'copy selected lines and '>' redirect it to the write command's referenced file.
(2) And to the problem of not pasting part of the line(s), like in @dronus comment mentioned, this solution (the selected one, first alternative) worked for me:
Edit the first file, yanking the text you want. Then open your second file from within vi (:e /path/to/other/file) and paste it (by typing p). Save it (like above ':w new.txt'.
It will then just copy the part of the lines marked up by mouse or 'y' with the cursors.
[EDIT] On my Ubuntu system: Sometimes selecting by mouse does NOT enter 'VISUAL MODE' in vim. Then the normal copy/paste can be selected using the context menu... I have not found the reason why Ubuntu changed it behaviour from a 'client acting behaviour' to a 'host' one (withUbuntu hosting the ssh bash window to my 'Client')...
'IT박스' 카테고리의 다른 글
Angular에서 2 개의 모델을 하나의 입력 필드에 바인딩하는 방법은 무엇입니까? (0) | 2020.10.29 |
---|---|
파이썬 프로그램을 실행 가능하게 만들기 위해 Linux에서 무엇을 사용합니까? (0) | 2020.10.29 |
JavaScript : 2 일 전 날짜를 계산하는 방법은 무엇입니까? (0) | 2020.10.29 |
자바 스크립트 캔버스로 이미지 크기 조정 (부드럽게) (0) | 2020.10.29 |
지정된 행 수에 따라 CSV 파일을 분할하는 방법은 무엇입니까? (0) | 2020.10.28 |