Programing

Vim에서 삽입 모드로 들어 가지 않고 커서가있는 곳에 줄 바꿈을 삽입하려면 어떻게해야합니까?

lottogame 2020. 10. 17. 08:59
반응형

Vim에서 삽입 모드로 들어 가지 않고 커서가있는 곳에 줄 바꿈을 삽입하려면 어떻게해야합니까?


삽입 모드로 들어 가지 않고 커서가 Vim에있는 곳에 줄 바꿈을 삽입 할 수 있습니까? 다음은 예입니다 ( [x]커서가 켜져 있음을 의미 함 x).

if (some_condition) {[ ]return; }

가끔 더 많은 코드를 입력하고 싶을 수도 있습니다. 따라서를 눌러 i삽입 모드로 들어가고을 눌러 Enter줄 바꿈을 삽입 한 다음 추가 공백을 삭제합니다. 다음으로 일반 모드로 들어가서 닫는 중괄호 앞에 커서를 놓은 다음 동일한 작업을 수행하여 자체 줄에 표시합니다.

나는 이것을 한동안 해왔지만 확실히 더 좋은 방법이 있습니까?


예를 들어 rEnter, 단일 문자 (공백)를 Enter로 바꾸는 데 사용할 수 있습니다 . 그런 fspace.다음 다음 공간으로 이동하고 마지막 명령을 반복합니다.

자동 들여 쓰기 설정에 따라 위의 내용은 return 문을 제대로 들여 쓰기 할 수도 있고 그렇지 않을 수도 있습니다. 그렇지 않은 경우 sEnterTabEsc대신을 사용 하여 공백을 개행으로 바꾸고, 줄을 들여 쓴 다음 삽입 모드를 종료하십시오. '.'를 사용할 수 없도록 두 번째 공백을 다른 명령으로 바꿔야합니다. 이 경우.


삽입 모드가 아닌 상태 에서 'g'를 누를 때마다 커서에 개행 문자를 삽입하는 매크로를 만드는 방법은 다음과 같습니다 .

vim 내에서 다음을 입력하십시오.

:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]

어디:

  • [Ctrl + V] 는 Ctrl 키를 누른 상태에서 'v'를 누르는 것을 의미 합니다.
  • [Enter] 는 Enter 키를 누르는 것을 의미 합니다.
  • [Esc] 는 Esc 키를 누르는 것을 의미 합니다.

마지막 Enter 키를 누를 때까지 vim 창 하단에 다음이 표시됩니다.

:map g i^M^[

설명:

[Ctrl + V]는 "다음 문자 인용"을 의미합니다. 명령에 개행 문자와 이스케이프 문자를 포함 할 수 있습니다.

따라서 'g'키를 시퀀스에 매핑합니다.

i [Enter] [Escape]

이것은 커서 앞에 개행삽입하고 삽입 모드를 종료하는 vim입니다 .

조정 :

  • 사용하는 명령에 아직 연결되지 않은 문자로 'g'를 바꿀 수 있습니다.
  • 명령, 더 추가 f}i^M^[O -이됩니다 F 산업사 }내가 다른 줄 바꿈 nsert, 다음 삽입 모드에서 탈출 O 펜 더 많은 코드를 입력 할 빈 줄.
  • 명령을 .vimrc 또는 .exrc 파일에 추가하여 영구적으로 만들 수 있습니다. 처음부터 콜론을 생략하면 명령이 "map"으로 시작됩니다.

즐겨!


Ctrl + Enter를 눌러 커서에서 줄을 끊는 간단한 매핑 :

:nmap <c-cr> i<cr><Esc>

기본적으로 '삽입'모드로 들어가고 줄 바꿈을 삽입하고 일반 모드로 돌아갑니다.

나중에 사용할 수 있도록 .vimrc 파일에 넣으십시오.


일반적으로 한 줄 블록을 세 줄로 확장하는 경우 대체를 시도하십시오. 여는 브래킷을 브래킷 / 리턴으로 변경하고 닫는 브래킷을 리턴 / 브래킷으로 변경하십시오.

대괄호를 대괄호 / 반환으로 대체하는 명령은 다음과 같습니다.

:s/{/{\r/

이것을 자주 사용하고 싶기 때문에 다음과 같이 전체 시퀀스를 사용하지 않는 키 입력에 매핑 할 수 있습니다.

:map <F7> :s/{/{\r/ ^M :s/}/\r}/ ^M

^ M 이 표시된 곳에 [Ctrl-V]입력 한 다음 Enter 를 누릅니다 .

이제 샘플 라인의 아무 곳에 나 커서를두고 매핑 된 키를 누르면 캐리지 리턴이 추가됩니다.

:help map-which-keys매핑 할 사용하지 않는 키 입력 선택에 대한 조언을 확인하십시오 .


K다른 것에 매핑해도 괜찮다고 가정하고 (원하는 다른 키 선택) 마커 '를 임시 마커로 사용하는 것은 괜찮다고 가정합니다.

:nmap K m'a<CR><Esc>`'

이제 일반 모드에서 줄 바꿈이 발생하려는 문자 위에 K를 누르면 줄이 분할되고 커서가 있던 자리에 그대로 둡니다.


Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.

If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)


Basically, when you split a line you either want to just insert a carriage return, or in the case that you're on a space, replace that with a carriage return. Well, why settle for one or the other? Here's my mapping for K:

"Have K split lines the way J joins lines
nnoremap <expr>K getline('.')[col('.')-1]==' ' ? "r<CR>" : "i<CR><Esc>"

I use the ternary operator to condense the two actions into one key map. Breaking it down, <expr> means the key map's output can dynamic and in this case hinges on the condition getline('.')[col('.')-1]==' ' which is the long winded way to ask vim if the character under the cursor is a space. Finally, the familiar ternary operator ? : either replaces the space with linebreak (r<CR>) or inserts a new one (i<CR><Esc>)

Now you have a lovely sister key map to the J command.


Set this key mapping in your vimrc

:map <C-m> i<CR><Esc>h

Then press Ctrl+m if you want to use it in your vim.


IMHO, the built-in mapping gs is not a useful mapping (put vim to sleep), one could use this for splitting:

nmap gs i<CR><ESC>

In Vrapper you can use gql which will split a line without entering insert mode, but may not always maintain indentation.


In fact you need the following combined operations:

  1. Press v to enter Visual Mode
  2. Select the line you want to split
  3. Press : to enter in Command Mode
  4. s/\s/\r/g
  5. Done

If you have the input:

aaa bbb ccc ddd

and want to output

aaa
bbb
ccc
ddd

You can use the command

f r<ENTER>;.;.

I found this to be the most faithful implementation of what I'd expect the opposite behaviour to J

nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w

It does the simplistic new line at cursor, takes care of any trailing whitespace on the previous line if there are any present and then returns the cursor to the correct position.

i <cr> <esc> - this is one of the most common solutions suggested, it doesn't delete non-whitespace characters under your cursor but it also leaves you with trailing whitespace

^mw - goto start of new line and create a mark under w

gk - go up one line

:silent! s/\v +$//<cr> - regex replace any whitespace at the end of the line

:noh<cr> - Clear any search highlighting that the regex might have turned on

`w - return the the mark under w

Essentially combines the best of both r<esc><cr> and i<cr><esc>

Note: I have this bound to S which potentially overwrites a useful key but it is a synonym for cc and since I don't use it as often as I do splits I am okay with overwriting it.


This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:

:map g ^f{malr<CR>`a%hr<CR>`a

This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.

참고URL : https://stackoverflow.com/questions/237383/how-do-i-insert-a-linebreak-where-the-cursor-is-without-entering-into-insert-mod

반응형