How to clear all the content of a file in emacs? - emacs

This question may sound stupid. But I do search online and couldn't find a answer. So what's the emacs command to clear all the content in a file? I want to clear my error_log file.

Rather than erase-buffer, you can use C-x h C-w.

Is M-x erase-buffer what you are looking for? Note that it is disabled by default because new users find it confusing and you'll be asked about enabling it temporarily or permanently the first time you run it. You can then bind it to a keyboard shortcut for convenience.

Related

How do you avoid key binding collisions between VS Code and vscodevim?

I have some shoulder pain that I am trying to get rid of by using vscodevim.
My biggest problem with vscodevim so far is that is sometimes collides with VS code shortcuts. For example I often save all my files with Ctrl+K S. But with vscodevim enables, instead that key combination does something else.
I feel tired about the prospect of having to build a configuration with my own key VS Code key bindings, that don't conflict with vsvim. I hope there is a better solution.
So I ask you, my better knowing colleagues, how do you solve this problem?
There is no easy way out for avoiding conflicts between vscode shortcut and vscodevim bindings. I suggest you to use vim keybindings instead of vscode shortcuts wherever possible. Vscodevim can prove to be a good gateway to vim provided you are open to relearning how you use vscode.
Instead of building your own configuration that doesn't collide with vscodevim you could redirect the effort in finding a way to do the same thing in vim. You can search web or use vim help.
Vim help is really comprehensive and easy to read. If you have vim installed you can use :help or :h followed by a specific subject, for example if you need to read about navigation type :h navigation.
Now you have two choices and I will try to use example you provided i.e. saving a file :
Use bindings available for saving file in vim.
Vim provides so many shortcuts just for saving and quitting files, in vim try :help write and :help quit to know more. Many of those shortcuts works in vscodevim too.
To save file :
:w to save a single file
:wa to write all buffers (in vscode all modified files).
Remove the bindings from vscodevim and use vscode shortcut.
You can delegate the key combination back to vscode. I suggest you do this only if there is any vscode shortcut that is absolutely necessary for you and it conflicts with vscodevim as doing this may result in some related vim bindings to not work. To know what Ctrl+k does in vim type :h ctrl-k in vim.
If you want to use Ctrl+K S for saving file do following:
Open vscode settings and search for vim handlekeys or directly open vscode's settings.json:
add following "vim.handleKeys": {"<C-k>": false}
I don't know if this a better solution but I was in your situation and tried different ways to solve it. Finally I found it was much easier to use vim keybindings than trying to coerce vscodevim.
I set up a "toggleVim" keybinding, like so: https://stackoverflow.com/a/61652769/1054322
When I want to use a hotkey that clashes with vim, I just turn off vim.
I installed "vscode neovim" instead of "vim".
Open the vscode setting, you can search "vscode-neovim: Use ctrl keys for insert Mode", disable it.
So you can use "ctrl k+ ctrl c" to add a line comment in insert mode. Also other vscode "ctrl" keys could be used in this mode.

Automatically opening a file in emacs by specifying the file name

I created a TAGS file for emacs in my django project using the following command on my Linux machine
ctags -eR *
I can now jump to a symbol definition using M-. and specifying the symbol name.In my project i have py,html and css files so is there also a way i can make emacs automatically open a file, if i specify the file name ?.
Thank You
I think you are looking for project management. There are few packages to manage project directory in emacs. The best one may be ede. but Its not easy to setup. It does have some learning curve and its limitations.
Thankfully there are many easy ones. like eproject. https://github.com/jrockway/eproject/wiki
anyway you can also check out emacswiki page for more details. http://www.emacswiki.org/ProjectSettings
http://www.emacswiki.org/emacs/find-file-in-tags.el
IDO (Interactive Do) mode does this. If you activate it, C-x C-f searches for files matching what you are typing, interactively. Beware though, it may take some time to get used to it.
[edit] The search is based on files or directories you've recently visited, and you can use M-s to force a search.
From the comments, I figured that you are looking for has nothing to do with tags, you just want a better find-file that makes good automatic guesses for the path given only the file name.
For this, I use the entirely awesome ido-mode: http://emacswiki.org/emacs/InteractivelyDoThings

emacs copy kill-ring to system clipboard in nowindow mode

I'm running "emacs -nw" under terminator program in Ubuntu. My issue, I couldn't get kill-ring to system clipboard with that. However, things works if run in X11 mode. Could you please help me with that?.
Thanks in advance..
Emacs in "nw" mode is not linked to any of the X11 libraries. Because of this, you have no access to the X11 clipboard. In other words, this just isn't possible by itself. There are utilities that allow command-line access to and from the X11 clipboard. You would have to write some elisp code to tie kill-ring activity to whatever command-line tools you may use.
One such program you might look at is xclip. Getting the selection from the kill-ring into the clipboard won't be as hard as getting from the clipboard into the kill-ring. This is because you have control over the event when you add to the kill-ring. But emacs has no knowledge of when X11 updates the clipboard. So you may have to have a custom command rather than the typical yanking from kill-ring, one that uses xclip to fetch the current clipboard content.
This seems to me like something that someone would have already solved. I don't know enough elisp to tackle it by any measure, or I'd dig into it myself. I did find this blog post that points to an elisp library that wraps xclip. I don't know if that will help or not.
Randy

Change the initial directory on emacs, and also some tips for a starter

How can I change the initial directory on emacs? I started using it today and there are a lot of things to learn!
Right now I'm using M-X cd and then I change the directory that I use for C-x C-f, but in a daily work I'm going to heavily use one particular directory so I need to make the change permanent.
Also if you have some tips for a starter it will be nice.
To set a default directory, simply put
(cd "/default/path/")
in your ~/.emacs file.
Emacs has a steep learning curve, but with constant use the most common commands will soon become second nature. As you discover more, emacs may become your favorite text editor, file manager, and email reader :)
To learn emacs, I'd start by pressing C-h t to start plowing through the excellent tutorial. Have a pad of paper beside you and write down the commands as you learn them. You'll use the crib sheet at first, but after a while, as I mentioned before, your fingers will learn the key combinations without your brain conciously intervening.
Good luck and have fun!
Default directory is determined by:
(setq default-directory "~/")
in this case, set to the users home dir, but it could be somewhere else.
If this variable is not set, the initial load point for find-file will be the directory where the emacs executable lives.
EDIT: on windows, a global HOME environment variable can be defined (HOME=c:\path\to\user\home\directory), ensuring that Emacs translates ~ to the correct value. This makes the Emacs settings usable across platforms.
Start up emacs only after changing to that "initial directory", effectively setting the current directory for emacs.
This is a solution specific to Windows.
Create a shortcut to emacs.exe" called GNU Emacs
Open Properties on the GNU Emacs shortcut
Set Start in to %USERPROFILE%\Documents
This solution works for multi-user workstations whether they are on or off Active Directory.
This shortcut will start Emacs in the current user's Documents folder.
Print out the Gnu Emacs Reference card and keep it by the desk.

How to change emacs config in Lisp In A Box

I have been a programmer for a decade now, but I believe this is the first time I've ever asked a question on a forum. I just can't figure this out and can't find the answer already online.
I am trying to turn on CUA mode so that emacs is more bearable for a windows user (normal copy paste functions). I am running Windows 7 and installed emacs through the Lisp In A Box package. I understand that I need to add a line to my .emacs file or init.el file. I'm not sure which, but I can't find either in my Lip In A Box install directory. The emacs package install also did not come with any tutorials or help files, so its really hard to pick this up.
I am stuck, any help is greatly appreciated!
The .emacs can be found by looking at the answers to this similar question.
Regarding documentation and tutorials, it looks like the link you provided for "Lisp in a Box" says:
If you are new to Emacs, it is
recommended that you read the Emacs
Tutorial which you can access from
with Emacs by going to the Help menu,
or by typing Control-h, letting go,
and hitting t. A more extensive manual
is also available from the Help menu,
or on the web at
http://www.gnu.org/software/emacs/manual/.
Which makes it sound like the manual is there, and certainly the tutorial (I made bold the directions to get to the tutorial).
As far as other places to get information, there is a collection of screencasts on the wiki.
Your question doesn't specify whether or not you what to add to your .emacs to activate CUA mode. You can check out the CUA mode documentation on the wiki (which has links to the manual). The minimal installation is just adding this to your .emacs: (cua-mode t).
For GNU/Emacs, you can choose to use any one of the following three file names as the start-up configuration file:
${HOME}/.emacs
${HOME}/.emacs.el
${HOME}/.emacs.d/init.el
It would probably be a good idea to decide on one of the three options and then stick to it - the first one seems to be the most widely used one. In any case, ${HOME} stands for your home directory -- which is likely to be different from the Lisp In A Box install directory!
Coming from a Unix tradition, Emacs understands ~ (tilde) as an abbreviation for your home directory, so you can visit the .emacs file by typing:
C-x C-f ~/.emacs [ENTER]
(Note that the capital C is Emacs standard notation for a combination of the CTRL key and a second key, i.e. here you press CTRL-x CTRL-f which stands for "find-file" and will then ask you for a file name in the bottom part of the Frame (aka mini-buffer).)
If these are your first customizations, you will just see an empty buffer. Enter
;; start CUA mode every time Emacs starts
(cua-mode t)
and save the buffer with C-x C-s.
Next time you start Emacs, CUA mode should be turned on automatically.
What the others have told you is true: Simply adding (cua-mode t) to your dotfile would be sufficient. HOWEVER: Lisp in a box' Emacs doesn't load this file by default.
Therefore, be sure to edit the shortcut so that it does load the dotfile. This is important, because otherwise you would get weird behavior, where you would add the correct line to the dotfile, start emacs, and then not get cua mode. That would suck.
The reason it does this is to ensure that it starts a vanilla emacs everytime, instead of finding, say C:/_emacs and loading that instead, giving you another user's customizations and confusing you.
The flag for not loading an init file is -q or --no-init-file. Also make sure that --no-site-file is not there.
(I realize that this is an old post, but I found this while looking for something related, and I don't want people walking away frustrated over something that doesn't work.)