Spawn another vim terminal split by shell command while in terminal mode? - shell

I like the new command :terminal, now I try to use it for my usual terminal tasks.
When I run $xterm & in that Vim terminal mode 'split' it opens a new terminal window with WM. Can I have some shell command to run another terminal inside parent Vim?

It seems that you want a shell command that opens vim in terminal mode?
You can simply use -c while invoking vim to supply a command. For instance you could put
[[ $VIMTERM ]] || VIMTERM=true vim +terminal +start
in your .bashrc to open vim in terminal mode when opening a new shell.
If you want to create a new terminal window within vim you could first exit TERMINAL mode with CTRL-\ CTRL-n and then use :split +term or :vs +term
Edit after comments below
If you want to send keys to vim from the command line you need to use its --servername functionality. For instance, start your first instance with
vim --servername vimserv
Then once inside vim's terminal you can use
vim --servername vimserv --remote-send "<C-w>:term<CR>"

Related

How to start emacs from the terminal with an emacs command that runs soon after emacs is opened

Is there a way to launch Emacs from terminal and execute some Emacs command automatically soon after Emacs is launched (the command to be executed inside of emacs is provided along with the Emacs-launching command executed from the shell.)
What I want to do exactly is to have a command to launch Emacs and then open a new empty buffer and activate org mode inside of this buffer.
I want something that might look like this
emacs -fs --command="evil-buffer-new && org-mode"
I want the -fs flag because I want Emacs to open in full-screen in this case.
Update
--eval flag didn't work. Forget about evil-buffer-new, I have tried something as simple as:
emacs --eval="(org-mode)" txt.txt
txt.txt is an empty text file created before executing the above command (and please don't ask me why I didn't use .org file extension).
after Emacs opened, org-mode wasn't active. I had to run pp-eval-expression then (org-mode) to activate it, and then it worked.
Am I missing something here? How about rephrasing the question like this:
How to open an empty text file (having .txt file extension) with Emacs from the terminal and have org-mode activated in that buffer automatically?
See C-hig (emacs)Action Arguments or even just run emacs --help -- there are several options for loading and evaluating arbitrary code.
--command="evil-buffer-new && org-mode"
More like:
--eval="(progn (evil-buffer-new) (org-mode))"
But you'll have to figure it out for yourself, because I don't know what evil-buffer-new is specifically.
You told an empty file is created before emacs is started. But instead of an empty file could you create a file with file-local mode variable specifying the org mode ? For example with bash:
#!/bin/bash
cat <<EOF >> "$1"
; -*- mode: Org;-*-
EOF
emacs "$1" &
Now the mode is always resolved correctly with normal major mode selection procedure.

Starting st from a vim keybind failes

I am using vim as an editor for LaTeX and I use bib-files for my citations.
I want to write a function with which vim opens the currently used bib-file in a terminal which itself runs vim on the .bib-file and lets me edit it.
In the bash script I am using the line
st -t bibfile_edit_terminal_floating -e nvim $BIBFILE_PATH &
where $BIBFILE_PATH is the path of the file (which is correct and works).
When I run the line from a terminal, it opens a st as a separate process and in st opens the file in vim as I want it.
If I call the same line from vim / nvim as a command in a script, the command opens a terminal for a split second and then closes again.
I don't know what is going on.
If I use the line
st -t bibfile_edit_terminal_floating -e nvim $BIBFILE_PATH (without the ampersand at the ending), the file opens in a terminal like I want it but I cannot edit my original document as it waits for the process to finish, which is not what I want.
Please help!

Restart terminal without closing on MacOS

How to restart my current MacOS terminal session without closing the window?
In Linux I use exec bash but it does not work in this environment. I made a few changes to the .bash_profile (prompt, alias etc) I would like to see without closing it and opening again.
Just type in the command:
exec bash -l
I guess that should do it.
For zsh,
exec zsh -l
This is needed because every shell on macOS by default is a login shell.
Justing writing exec bash would replace the current shell with a non-login shell which is not the same effect as closing and re-opening the terminal.
exec would make new bash -l process replace the current shell. If exec is not used, bash -l would spawn a new shell over the current shell incrementing the $SHLVL.
For me none of the other solutions work for ZSH.
Simply source ~/.zshrc did the job actually.
Note: running exec zsh -l outputs /Users/my_username/.zprofile:3: command not found: yarn (where my_username is my username). But running only the command mentioned above does the job.
If your session is hanging (maybe your SSH connection was interrupted), you won't be able to restart by entering a command.
On iTerm, you can navigate to "Session" > "Restart Session" in the menu bar.
You can also add a key binding for this via "iTerm" > "Preferences" > "Keys" > "Key Bindings" > "+".
Keyboard Shortcut: Your choice, I use Cmd-R
Action: "Select Menu Item..." > "Restart Session"
The actual answer, assuming you interpret the question as having the same effect at the state of the terminal session as closing and reopening Terminal would, appears to be to run the executable of the used shell to start a new session:
https://unix.stackexchange.com/a/217907/137983
zsh
If you're not on Catalina where ZSH is the default shell, it's going to be:
bash
After this, all state of the previous session (like session environment variables) will be reset. Also ZSH profile should be re-sourced I think.
If you've made any changes to your .bashrc and .bash_profile, then without closing the terminal you can specify alias in your .bashrc and .bash_profile as shown below to restart the terminal:
alias rest='exec bash -l;source ~/.bashrc;source ~/.bash_profile'
This command sources the .bashrc and .bash_profile again, in the sense restarts the terminal and creates a new terminal session. It works for me. Give this a try!
So, if you wanna restart the terminal, just enter rest (short for restart) in your terminal.

Linux bash: exec programm from terminal, return to terminal after it was closed

I would like to use the mouse features of gvim, but have it behave as normal vim otherwise. For that, I have added the line set guioptions= in my .gvimrc .
I could make an alias to gvim (mapping it to exec gvim), so that when I open gvim from a terminal, it appears to open in the same window.
Is there any way to go back to the starting terminal when I close gvim? (like when you close vim)
:set mouse=a in vim did the trick.

Why does tmux erase terminal contents on editor exit?

Say I'm running tmux and use "ls" to get something on the screen. Then I enter Vim to edit some file. After exiting Vim, tmux erases whatever text is above the bash prompt (not like the clear command since the prompt stays in the same place).
Is there a way to prevent this behavior? I'm using tmux 1.3 in Terminal.app on OS X.
If your version of tmux has this option, add this line to your ~/.tmux.conf:
set-window-option -g alternate-screen on
Try putting this in your .vimrc file:
set t_ti= t_te=

Resources