Skip to content

Commit 79bfeec

Browse files
shadmansalehjustinmkzeertzjq
authored
feat(editor)!: insert-mode ctrl-r should work like paste neovim#35477
Problem: insert-mode ctrl-r input is treated like raw user input, which is almost never useful. This means any newlines in the input are affected by autoindent, etc., which is: - slow - usually breaks the formatting of the input Solution: - ctrl-r should be treated like a paste, not user-input. - does not affect `<c-r>=`, so `<c-r>=@x` can still be used to get the old behavior. Co-authored-by: Justin M. Keyes <[email protected]> Co-authored-by: zeertzjq <[email protected]>
1 parent b4d21f1 commit 79bfeec

File tree

10 files changed

+70
-21
lines changed

10 files changed

+70
-21
lines changed

runtime/doc/cmdline.txt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,16 @@ CTRL-R {register} *c_CTRL-R* *c_<C-R>*
142142
typing CTRL-R and the second character '"' will be displayed
143143
to indicate that you are expected to enter the name of a
144144
register.
145-
The text is inserted as if you typed it, but mappings and
146-
abbreviations are not used. Command-line completion through
147-
'wildchar' is not triggered though. And characters that end
148-
the command line are inserted literally (<Esc>, <CR>, <NL>,
149-
<C-C>). A <BS> or CTRL-W could still end the command line
150-
though, and remaining characters will then be interpreted in
151-
another mode, which might not be what you intended.
145+
When used with named or clipboard registers (A-Z,a-z,0-9,+)
146+
text is inserted literally like pasting with "p". For other
147+
registers, the text is inserted as if you typed it, but
148+
mappings and abbreviations are not used. Command-line
149+
completion through 'wildchar' is not triggered though. And
150+
characters that end the command line are inserted literally
151+
(<Esc>, <CR>, <NL>, <C-C>). A <BS> or CTRL-W could still end
152+
the command line though, and remaining characters will then be
153+
interpreted in another mode, which might not be what you
154+
intended.
152155
Special registers:
153156
'"' the unnamed register, containing the text of
154157
the last delete or yank
@@ -176,7 +179,9 @@ CTRL-R {register} *c_CTRL-R* *c_<C-R>*
176179
sure the expression evaluates to an empty
177180
string. E.g.: >
178181
<C-R><C-R>=setcmdpos(2)[-1]<CR>
179-
< See |registers| about registers.
182+
< You can use this to insert a register as
183+
typed with CTRL-R =@reg.
184+
See |registers| about registers.
180185
Implementation detail: When using the |expression| register
181186
and invoking setcmdpos(), this sets the position before
182187
inserting the resulting string. Use CTRL-R CTRL-R to set the

runtime/doc/insert.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,17 @@ CTRL-N Find next keyword (see |i_CTRL-N|).
101101
CTRL-P Find previous keyword (see |i_CTRL-P|).
102102

103103
CTRL-R {register} *i_CTRL-R*
104-
Insert the contents of a register. Between typing CTRL-R and
104+
Insert the contents of a register. Between typing CTRL-R and
105105
the second character, '"' will be displayed to indicate that
106-
you are expected to enter the name of a register.
107-
The text is inserted as if you typed it, but mappings and
108-
abbreviations are not used. If you have options like
109-
'textwidth', 'formatoptions', or 'autoindent' set, this will
110-
influence what will be inserted. This is different from what
111-
happens with the "p" command and pasting with the mouse.
106+
you are expected to enter the name of a register. When used
107+
with When used with named or clipboard registers
108+
(A-Z,a-z,0-9,+) text is inserted literally like pasting with
109+
"p". For other registers, the text is inserted as if you typed
110+
it, but mappings and abbreviations are not used. If you have
111+
options like 'textwidth', 'formatoptions', or 'autoindent'
112+
set, this will influence what will be inserted. This is
113+
different from what happens with the "p" command and pasting
114+
with the mouse.
112115
Special registers:
113116
'"' the unnamed register, containing the text of
114117
the last delete or yank
@@ -131,6 +134,8 @@ CTRL-R {register} *i_CTRL-R*
131134
special keys. E.g., you can use this to move
132135
the cursor up:
133136
CTRL-R ="\<Up>"
137+
you can use this to insert a register as
138+
typed with CTRL-R =@reg.
134139
Use CTRL-R CTRL-R to insert text literally.
135140
When the result is a |List| the items are used
136141
as lines. They can have line breaks inside

runtime/doc/news.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ DIAGNOSTICS
6868

6969
EDITOR
7070

71-
• todo
71+
|i_CTRL-R| inserts named registers (A-Z,a-z,0-9) literally like pasting instead of
72+
as typed. To get the old behavior you can use `<C-R>=@x`.
7273

7374
EVENTS
7475

runtime/doc/vim_diff.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ Commands:
316316
Editor:
317317
- |prompt-buffer| supports multiline input/paste, undo/redo, and o/O normal
318318
commands.
319+
- |i_CTRL-R| inserts named registers (A-Z,a-z,0-9) literally like pasting instead of
320+
as typed. To get the old behavior you can use `<C-R>=@x`.
319321

320322
Events (autocommands):
321323
- Fixed inconsistent behavior in execution of nested autocommands #23368

src/nvim/edit.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2854,6 +2854,8 @@ static void ins_reg(void)
28542854
vim_beep(kOptBoFlagRegister);
28552855
need_redraw = true; // remove the '"'
28562856
} else {
2857+
yankreg_T *reg = get_yank_register(regname, YREG_PASTE);
2858+
28572859
if (literally == Ctrl_O || literally == Ctrl_P) {
28582860
// Append the command to the redo buffer.
28592861
AppendCharToRedobuff(Ctrl_R);
@@ -2862,7 +2864,11 @@ static void ins_reg(void)
28622864

28632865
do_put(regname, NULL, BACKWARD, 1,
28642866
(literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
2865-
} else if (insert_reg(regname, NULL, literally) == FAIL) {
2867+
} else if (reg->y_size > 1 && is_literal_register(regname)) {
2868+
AppendCharToRedobuff(Ctrl_R);
2869+
AppendCharToRedobuff(regname);
2870+
do_put(regname, NULL, BACKWARD, 1, PUT_CURSEND);
2871+
} else if (insert_reg(regname, NULL, !!literally) == FAIL) {
28662872
vim_beep(kOptBoFlagRegister);
28672873
need_redraw = true; // remove the '"'
28682874
} else if (stop_insert_mode) {

src/nvim/ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static inline int op_reg_index(const int regname)
152152
static inline bool is_literal_register(const int regname)
153153
FUNC_ATTR_CONST
154154
{
155-
return regname == '*' || regname == '+';
155+
return regname == '*' || regname == '+' || ASCII_ISALNUM(regname);
156156
}
157157

158158
EXTERN LuaRef repeat_luaref INIT( = LUA_NOREF); ///< LuaRef for "."

test/functional/editor/mode_insert_spec.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,36 @@ describe('insert-mode', function()
8383
{5:-- INSERT --} |
8484
]])
8585
end)
86+
87+
it('inserts named registers literally', function()
88+
local screen = Screen.new(50, 6)
89+
-- regular text without special charecter command
90+
command('let @a = "test"')
91+
feed('i<C-R>a<ESC>')
92+
screen:expect([[
93+
tes^t |
94+
{1:~ }|*4
95+
|
96+
]])
97+
98+
-- text with backspace character gets written literally by default
99+
command('let @a = "test\\<C-H>"')
100+
feed('cc<C-R>a<ESC>')
101+
screen:expect([[
102+
test{18:^^H} |
103+
{1:~ }|*4
104+
|
105+
]])
106+
107+
-- =@reg<CR> can be used to get effect of keypress
108+
command('let @a = "test\\<C-H>"')
109+
feed('cc<C-R>=@a<CR><ESC>')
110+
screen:expect([[
111+
te^s |
112+
{1:~ }|*4
113+
|
114+
]])
115+
end)
86116
end)
87117

88118
describe('Ctrl-O', function()

test/old/testdir/test_autocmd.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ func Test_Cmdline()
20902090

20912091
let g:log = []
20922092
let @r = 'abc'
2093-
call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
2093+
call feedkeys(":0\<C-R>=@r\<CR>1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
20942094
call assert_equal([
20952095
\ '0',
20962096
\ '0a',

test/old/testdir/test_cmdline.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ func Test_cmdline_paste()
905905

906906
" Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
907907
let @a = "xy\<C-H>z"
908-
call feedkeys(":\"\<C-R>a\<CR>", 'xt')
908+
call feedkeys(":\"\<C-R>=@a\<CR>\<CR>", 'xt')
909909
call assert_equal('"xz', @:)
910910
call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
911911
call assert_equal("\"xy\<C-H>z", @:)

test/old/testdir/test_ins_complete.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ func Test_complete_reginsert()
15231523
exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
15241524
call assert_equal('a123', getline(5))
15251525
let @r = "\<C-P>\<C-P>"
1526-
exe "normal GCa\<C-P>\<C-R>r"
1526+
exe "normal GCa\<C-P>\<C-R>=@r\<CR>"
15271527
call assert_equal('a12', getline(5))
15281528
exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
15291529
call assert_equal('a1234x', getline(5))

0 commit comments

Comments
 (0)