How to add new line in bashrc file in ubuntu? - bash

I'm trying to update my bashrc file in ubuntu with some environment variables.
I can do this using below command.
echo 'export APP=/opt/tinyos-2.x/apps' >> ~/.bashrc
But I want to do it manually, meaning open the file with vim editor then add it.
The problem here is when I open the bashrc file the end line is "fi" and when I reach there and press insert and then enter to go to new line it stays at the same line and moves the fi word only or create A or C or B random characters.
May I know please some commands to handle this bashrc file so that I could add a new line and then my variables over there?
I've tried to look online but didn't find what am looking for.

Since you didn't specify if you have terminal access only or also GUI.
If you have terminal access only, any editor would do it.
Popular editors like nano or vim come installed by default in most Ubuntu releases.
To use nano, in your terminal, type
nano ~/.bashrc
Then press Ctrl + w +v to go to the end, add what you want to add, the Ctrl + o to save changes, Ctrl +x to exit.
You will need to log out and log back on, or run source ~/.profile to make your changes available to your bash environment.

Here's what you can do in vim.
Press the letter G. It will take you to the last line.
Press the letter O. It will allow you to insert text after the current lne.
Type in your content - export APP=/opt/tinyos-2.x/apps
Press the ESC key get out of editing mode.
Press the key :. It will allow you type commands.
Type wq followed by Enter. This will save the file and quit vim.
You are done.

As you are using Vim/Vi editor you need to use i/insert key to start editing, then for saving use escape & then wq to save and exit. For More detailed instructions please visit this link

Honestly, learning vim in a week (or even in a day) is tough. Took me more than a month to actually be productive on it as mh daily editor. But just to tell you. Go to the line after which you want to ass a new line using H-J-K-L or arrow keys. Then presss o. It'll spawn a new line below it and go into insert mode as well. Then write and press Esc. Enter :wq.

Related

how do you enable command line editing with vim keys using mac terminal?

I'm reading this book and I really do not understand what this author is talking about. It appears that you can program your mac to go forward one character by either hitting l or ^F. I do not understand the difference between emac key stroke and vim keystroke. He also says run this command and 'place it in your $HOME/.bash_profile but I cannot figure out how to place the command in the bas_profile.
The Author is talking about make you terminal console behaves like vi, this means typing set -o vi in your terminal, the console will work similar vi. So you will be able to navigate using the motion keys of vi, use INSERT mode, x to delete, etc.
You can set that permanently if you include this command in your ~/.bash_profile file.
If you are not sure what it does, I don't recommend so.

Execute a bash command in the text of a vim buffer

I know that I can get into bash while in vim via:
Ctrlz
or
:sh
or
:shell
etc.
Then use bash commands as normal, and get back out using fg.
What I am wondering is, can I execute a line of code from a script in vim straight to Bash, without having to exit vim, or having to copy it (via highlighting in visual mode for example) from vim, then going to a terminal and pasting it and hitting enter etc.?
Easiest way is to put the cursor on the line and type:
!!shreturn
This will replace the line with the output of the script. If you don't want that, simply follow up with u.
Arguably easiest way:
Yank the text you want to execute.
Open the cmdline (by pressing :)
type ! and then press ctrl-r and ", which will paste the content of the unnamed register to the cmdline (which will contain the text you wanted to execute)
press Enter

VI editor "ESC" key and "Ctrl + [" do not work correctly

I am a complete beginner with VI editor.
I had a chance of using the VI editor by typing git commit instead of git commit -m "my comment" when commenting my commit to my git repository.
I learned that I would go into "edit" mode by pressing i in the VI command line. And I also learned that I go back to the command mode by pressing either ESC key or Ctrl + [ key combination. However, neither keys get me out of the "edit" mode.
What happens is that pressing either keys do the same thing, something that seems like accumulating ESC key. Each time I press either key, I see something like below in the bottom line of the screen:
Pressed once:
ESC-
Pressed twice:
ESC ESC-
Pressed three times:
ESC ESC ESC
Since I know that the command to save and exit is :wq, when I press ESC one time and followed by :, the command line immediately turns into to Eval:. It seems like ESC + : is the key combination for such command.
This seems weird. What would be the problem and how could I fix it?
Thanks in advance for your help.
Edit
I found the below codes from .gitconfig file in my root directory. Is this causing the problem?
[core]
autocrlf = input
safecrlf = true
editor = emacs
SOLVED! My problem was due to the [core] editor part of the global settings of .gitconfig. Once I changed it to editor = vi I was able to exit out using VI's shortcuts. Thanks everybody for helping me out.
It is possible that there is something in your ~/.vimrc file that is causing problems. You might try moving your .vimrc file to a temporary one, then start vim basically without a configuration and see if the problem persists. If it does then put the file back and start commenting things out until you find it.
It is also possible that your terminal is passing through weird keystrokes or something.

Change in the hostname/user in Terminal in Leopard OSX

My terminal previously showed subalcharla$ at the command line.
The terminial is now showing subalcharla#subal-charlas-macbook ~ $.
How do I go back to the original setting?
What is the difference between the two?
How did this get changed without my doing so?
At the end of ~/.profile add the line
export PS1='\u$ '
to get your old prompt back.
To do this you can type
nano ~/.profile
which will bring up a text editor. Press down until you get to the bottom of the file. Hit Enter to create a new line, and paste in
export PS1='\u$ '
Press Control+X to exit the editor and say "yes" when asked if you want to save. Now restart your terminal and your prompt should be restored.
The first prompt you gave shows your username, the second shows your username and hostname. There is no error and the functionality of your bash shell is not changed by changing the prompt.
Something must have changed your PS1 environment variable, maybe a system update or the installation of software. It's probably benign though.
I don't know how it got changed, but it's controlled by some symbol definitions. Use "man bash" in the terminal and search for the section called "PROMPTING". There are symbols named PS1-to-4 that it uses to construct the prompt.

How can I invoke VIM with C-X e for long, complex, tricky commands?

I found an awesome tip here. You can "[r]apidly invoke an editor to write a long, complex, or tricky command". However, when I press the key combination above, I get Emacs open. I would like to switch it to Vim. How can I invoke Vim with C-X e?
[1. Problem SOLVED by Brian Cambell]
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
[2. Problem SOLVED thanks to Pax]
I was unable to get the tricky command back to Bash. The errors were:
> Error detected while processing BufRead Auto commands for "*":
> E117: Unknown function: JumpToLastPosition
Quotes in .vimrc solved the second problem. I am still unsure about the part in my .vimrc:
" augroup misc
" autocmd!
" autocmd BufReadPost * call JumpToLastPosition()
" autocmd FileChangedShell * call WarningMsg("File changed outside of vim")
" augroup end
[3. Problem]
What do the above part in .vimrc do?
On most Linux installs (all the ones I tested), bash recognizes both the Emacs and Vi command history keys (or you can use "set -o vi" to force it).
So, you can just use the vi-mode "<ESC>v" to to enter visual mode, this will start editing in a Vim session.
To run the command, you just save and exit from Vim ("ZZ" or ":wq"). To cancel the command, you need to delete the contents, save and exit ("1GdGZZ").
In addition to running it by exiting, you can also save it while in the editor to another location (":w /tmp/myscript").
Keep in mind that visual mode will work with the currently selected line so you don't have to start with a blank command ("<ESC>v"). You can use the normal vi-mode tools to select a line from the history first and then enter visual mode ("<ESC>kv" for last command, "<ESC>/grep<ENTER>nnv" for third-last grep command and so on).
Using this method has the advantage of not changing the "EDITOR" variable which may be used for other things (unless you want Vim for everything, which is a very sensible position to take IMNSHO).
Update:
Regarding your error, posted after the question:
JumpToLastPosition() is the function called by Vim for all files to put the cursor where it was when you last edited the file. I'm going to assume you're actually getting this error when the editing starts, not when you exit, since this is the auto function following a buffer read.
Can you start a "normal" vim session ("vim xx.txt" and then "vim xx") without this error occurring? You may find you get the same problem (and possibly only on the last one).
If you do have the same problem, you need to look at your startup files. It's possible the autocmd for BufRead is broken somehow. Have a look inside your vimrc and you filetype.vim files to see where that function is called and/or defined (I suspect it's called but not defined and that may be a mismatch between the two files or one of them has been damaged).
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
The link that you provided contains the answer:
Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR.
You need to set the EDITOR environment variable (like #Brian Campbell)

Resources