0

I am new to Vim. I managed to use :terminal and :vertical terminal to have a terminal side by side with my code. This works great, I can move between my code and the terminals. However, how do I go back to the last command in terminal? For example, I can't just press "Up" on the keyboard while in terminal. For the standalone terminal application, this would bring up the previous command. This doesn't work on while in Vim's terminal. The error is

"cannot make changes, modifieable is off"

.

I noticed that there are two modes when you use Terminal in Vim. There's the usual command line in Linux, and the Vim mode where you can use hjkl to move and etc.

Is this why I cant press up to use previous command? Please let me know and thank you.

P/S: This is solved. I mapped the Esc key to exit Terminal Normal Mode to Terminal Vim Mode. There were replies below but for some reasons those got deleted. Anyway, yes we can press up to get to the last command. Just remember, that we have to be in Normal terminal mode and not Vim mode.

  • Do you have any terminal mappings active (try vim --clean and see if the behavior persists; double check <C-w>:map and <C-w>:tmap while in terminal mode)? Do the arrow keys work for you outside of a :terminal (if not, they may be sending commands to the terminal window like <esc>D, which could fail)? Note that there is a way to put terminal windows in Normal mode, either by using ++noclose and stopping the process, or temporarily by sending <C-w>N (called terminal normal mode)—could this be tripping you up? – D. Ben Knoble Feb 19 at 5:28
  • There were replies here and I just figured out, not sure why the previous replies and my replies are gone. But yes, I mapped the Esc key to the <C-\ > <C-N> to switch from Terminal Mode to Vim Terminal Mode. Commenting that line out works. I will just not use Esc key as mapping then – mle0312 Feb 19 at 5:32
  • Not sure what happened to other comments either; if they were answers, they didnt belong here and may have been deleted. But yes, cursor keys typically send esc followed by other chars, so remapping esc tends to break things. – D. Ben Knoble Feb 19 at 5:33
  • This might not be what you are looking for nor a Vim solution but tmux is what you need. Easy to switch between windows, zoom-in/out. All in all, just a delight to work with once you get a hang of the mappings. – mithun_daa Feb 22 at 15:52
0

OP mentioned using tnoremap <Esc> <C-w>N in the comments: as noted in :h terminal-typing, this tends to break cursor keys, which send escape-sequences that vim interprets.

With this mapping, those sequences stop making sense.

One solution is to fiddle with 'timeoutlen' and 'ttimeoutlen' (if memory serves, it’s generally ok to make 'ttimeout' quite small). They control how vim interprets rapid key-presses and mappings.

The other solution is to disable/stop using said mapping.

1

One other thing that you can do is use <Ctrl-P> to go to the 'Previous' command. This is also works in bash terminals. You can also do <Ctrl-N> to go to the 'Next' command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.